1 / 9

Parameter Passing Revisited

Parameter Passing Revisited. Overview Parameter passing Passing parameters by value Passing parameters by reference Some Examples Preview: Introduction to Recursion. Parameter Passing Revisited.

marnin
Download Presentation

Parameter Passing Revisited

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Parameter Passing Revisited Overview • Parameter passing • Passing parameters by value • Passing parameters by reference • Some Examples • Preview: Introduction to Recursion

  2. Parameter Passing Revisited • In Java variables of primitive types are Passed by value while objects are passed by reference • “Passing by value” means that the argument’s evaluated value is copied, and is passed to the method. • Inside the method, any modification to the variable does not not affect the original argument • Passing by reference means that a reference to (i.e., the address of) the argument is passed to the method. • Using the reference, the called method is actually directly accessing the argument, not a copy of it • Any changes the method makes to the parameter also affects the actual object used as the argument • So after you return from the method, that object will retain any new values set in the method

  3. Parameter Passing : Passing Values import java.io.*; class ParameterPassing { static void modifyParameter (int num) { num = num * num + 3; System.out.println("Value inside method is: " + num); } public static void main (String[] args) throws IOException { BufferedReader stdin= new BufferedReader( new InputStreamReader(System.in)); int n, n1; System.out.println("Enter an integer:"); n = Integer.parseInt(stdin.readLine()); System.out.println("Value before invoking modifyParameter() is: " + n); modifyParameter(n); System.out.println("Value after invoking modifyParameter() is: " + n); } }

  4. Parameter Passing : Passing Values import java.io.*; class SwapPrimitives { static void swap(int first, int second) { int temp=first; first=second; second=temp; } public static void main (String[] args) throws IOException { BufferedReader stdin= new BufferedReader( new InputStreamReader(System.in)); int n1, n2; System.out.print("Enter first integer: ”); n1=Integer.parseInt(stdin.readLine()); System.out.print("Enter Second integer: ”); n2=Integer.parseInt(stdin.readLine()); System.out.println("Values before swapping are: "+n1+" and "+n2); swap(n1, n2); System.out.println("Values before swapping are: "+n1+" and"+n2); } }

  5. Parameter Passing : Passing Objects class Letter { char c; } public class ParameterPassing3 { static void f(Letter y) { y.c = 'z'; } public static void main(String[] args) { Letter x = new Letter(); x.c = 'a'; System.out.println("1: x.c: " + x.c); f(x); System.out.println("2: x.c: " + x.c); } }

  6. Parameter Passing : Passing Objects class MyInteger { int n; public MyInteger(int number) { n=number; } } import java.io.*; public class SwapObjects { static void swap(MyInteger first, MyInteger second) { int temp; temp=first.n; first.n=second.n; second.n=temp; }

  7. Parameter Passing : Passing Objects public static void main (String[] args) throws IOException { BufferedReader stdin= new BufferedReader( new InputStreamReader(System.in)); int n1, n2; System.out.print("Enter first integer: ”); n1=Integer.parseInt(stdin.readLine.readInt()); System.out.print("Enter Second integer: ”); n2=Integer.parseInt(stdin.readLine.readInt()); MyInteger intObject1 = new MyInteger(n1); MyInteger intObject2 = new MyInteger(n2); System.out.println("Values Before swapping are: "+n1+" and "+n2); swap(intObject1, intObject2); System.out.println("Values After swapping are: "+intObject1.n+" and "+intObject2.n); } }

  8. Parameter Passing : Passing Objects class Person { int age; String name; public void print(){ System.out.println(age + " " + name); } public Person(int a, String b) { age = a; name = b; } } What will the following output?

  9. Parameter Passing : Passing Objects class Test { static int i = 10; public static void main(String[] args) { String str = "First Message."; Person p1 = new Person(21, “Khalid"); Person p2 = new Person(20,“Amr"); mixUp(i, str, p1, p2); System.out.println("i: " + i + " str: " + str); p1.print(); p2.print(); } static void mixUp(int i, String str, Person one, Person two) { i++; str = "Second Message."; one = two; one.age = 34; one.name = “Ali"; } }

More Related