Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Parameter Passing Revisited Overview l Parameter passing l Passing parameters by value l Passing parameters by reference l Some Examples l Preview: Data.

Similar presentations


Presentation on theme: "1 Parameter Passing Revisited Overview l Parameter passing l Passing parameters by value l Passing parameters by reference l Some Examples l Preview: Data."— Presentation transcript:

1 1 Parameter Passing Revisited Overview l Parameter passing l Passing parameters by value l Passing parameters by reference l Some Examples l Preview: Data modeling

2 2 Parameter Passing Revisited l In Java variables of primitive types are Passed by value while objects are passed by reference l “Passing by value” means that the argument’s evaluated value is copied, and is passed to the method. »Inside the method this copy can be modified at will, and does not affect the original argument l 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 are made to 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 3 Parameter Passing : Passing Values import java.io.*; import TextIO; class ParameterPassing { static TextIO inputStream = new TextIO(System.in); static void modifyParameter (int num) { num *= num + 3; System.out.println("Value inside method modifyParameter() is: ” + num); } public static void main (String[] args) throws IOException { int n, n1; System.out.println("Enter an integer:"); n = inputStream.readInt(); System.out.println("Value before invoking modifyParameter() is: " + n); modifyParameter(n); System.out.println("Value after invoking modifyParameter() is: " + n); }

4 4 Parameter Passing : Passing Values public class ParameterPassing2 { static void f(char y) { y = 'z'; } public static void main(String[] args) { char x ; x = 'a'; System.out.println("1: x: " + x); f(x); System.out.println("2: x: " + x); }

5 5 A Brief Overview of Objects l Before considering passing objects as parameters, let us have a flavor of what objects are. l Real-world objects share two characteristics: they have state and they all have behavior. l Software objects are modeled after real-world objects in that they, too, have state and behavior. A software object maintains its state in variables and implements its behavior with methods. l Definition: An object is a software bundle of variables and related methods through whose execution the variables are updated. An object is a run-time entity.

6 6 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); }

7 7 Parameter Passing : Passing Objects class ParameterPassing4 { public static void main(String [] args) { int i; StringBuffer sb = new StringBuffer(“Hello “); double d = 5.5; System.out.println("After initialization"); System.out.println(sb.toString()); System.out.println("d = " + d); System.out.println("Calling changeParams.."); changeParams(sb, d); System.out.println("Back from changeParams");

8 8 Parameter Passing : Passing Objects Parameterpassing4 continued: System.out.println(sb.toString()); System.out.println("d = " + d); } private static void changeParams(StringBuffer s,double dd){ int i; dd = 3.14; sb.add(“World”); System.out.println(“sb = “ + sb.toString()); System.out.println("dd = " + dd); }

9 9 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?

10 10 Parameter Passing : Passing Objects class Test { static int i = 10; public static void main(String[] args) { String str = "I think, therefore I am. I think."; 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 = "First things first, but not necessarily in that order."; one = two; one.age = 34; one.name = “Ali"; }


Download ppt "1 Parameter Passing Revisited Overview l Parameter passing l Passing parameters by value l Passing parameters by reference l Some Examples l Preview: Data."

Similar presentations


Ads by Google