90 likes | 225 Views
Parameter Passing Revisited. Overview Parameter passing Passing parameters by value Passing parameters by reference Some Examples Preview: Introduction to Recursion. Parameter Passing Revisited.
E N D
Parameter Passing Revisited Overview • Parameter passing • Passing parameters by value • Passing parameters by reference • Some Examples • Preview: Introduction to Recursion
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
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); } }
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); } }
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); } }
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; }
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); } }
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?
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"; } }