Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank.

Similar presentations


Presentation on theme: "Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank."— Presentation transcript:

1 Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

2 Parameter Passing – Primitive Type Java passes all parameters to a method by value. The actual parameters (the values that are passed in) are assigned to the formal parameters (declared in the method header). This means that the value of the actual parameter is copied into the formal parameter.

3 Parameter Passing – Primitive Type In case of primitive parameter types, the formal parameter is a copy of the actual parameter. In case of a reference type parameter (to an object), the formal parameter is an alias of the actual parameter.

4 Parameter Passing –Primitive Type Example public class PrimitiveParameter { public void changeValue (int parameter) {parameter = 20;} public static void main (String args[]) { int parameter = 10; PrimitiveParameter test = new PrimitiveParameter(); test.changeValue(parameter);} }

5 Parameter Passing –Primitive Type Example In the main method: –The “parameter” is declared and assigned the value 10 (the actual parameter). –After the new class instance creation the method changeValue is called. As a result of the call a copy of formal parameter is made and sent to the method.

6 Parameter Passing – Primitive Type Example In the changeValue method: –The method refers to the formal parameter, not to the actual parameter. –The formal parameter is changed to 20. –When the method is left the formal parameter is destroyed and does not exist anymore. In the main method: –The “parameter” is still 10.

7 Parameter Passing – Reference Type When an object is passed to a method, Java passes a reference to that object. The value that is copied is the address of the actual object. Therefore it is important to understand that when a formal parameter object is used it modifies the actual object state permanently.

8 Parameter Passing – Reference Type Example public class RefParam { public void changeObject (Car parameter) {parameter.changeParameter();}... RefParam change = new RefParam(); parameter = new Car(); change.changeParameter(parameter); }

9 Parameter Passing – Reference Type Example In code section: –When the changeObject method is called, a formal object parameter is made and sent. In changeObject method: –The changeParameter method calls the actual object’s method. –This causes the state change of the object.

10 Parameter Passing – Reference Type Example In code section: –The formal parameter does not exist anymore, but the object still exists in its changed state. –In case the value of the formal parameter is changed it points then to a different object.

11 Parameter Passing – Applet Parameters are also passed from HTML files to Java applets. –The HTML file passes parameters as string types, therefore they need to be converted if used as another type. –The parameters are initialized in applet tag section of HTML file and retrieved in init() method of Java applet.

12 Parameter Passing – Applet Example An HTML file code section:

13 Parameter Passing – Applet Example The Java applet code section: init() { String FontName = getParameter( “ fontName ” ); String FontSize = getParameter( “ fontSize ” ); String Leading = getParameter( “ leading ” ); String PaidUp = getParameter( “ accountEn ” );... }

14 Methods and Parameters Methods are the cornerstone of OO programming, the basic unit of action Parameters allow the caller of the Method to affect its outcome by providing a value or values when it is invoked Without parameters, adjusting the Method outcome based upon invoker state would be quite cumbersome, if not impossible

15 Primitive Parameters Primitive types: boolean, byte, char, short, int, long, float, double In Java, all primitives are passed by value. This means a copy of the value is passed into the method Modifying the primitive parameter in the method does NOT change its value outside the method

16 Array Parameters Java has native support for Arrays as parameters A Java array is self-aware of its own length, unlike C/C++. All array information array can be passed as a single parameter, no length parameter is needed. Arrays are passed in the same way as Objects

17 Object Parameters Anything that is not a primitive or array falls under the umbrella of Object Objects can be passed natively, just like primitives or arrays In Java, handles to anything that is not primitive or an array is a reference. Does this mean Java passes Object parameters by Reference?

18 Objects are passed by Value It is often misstated that Object parameters are passed by Reference. While it is true that the parameter is a reference to an Object, the reference itself is passed by Value. This means modifications to the Object will be seen externally, but pointing the reference to another object will not.

19 Modify a Parameter Value … Point p = new Point(10, 20); addTen(p); System.out.println(p.x + “, “ + p.y); … void addTen(Point p) { p.x = p.x + 10; p.y = p.y + 10; }

20 Modify a Parameter Value The output of the System.out.println will be 20, 30 What if you do not want an outside method modifying your parameter? Ways to handle this will be explored later

21 Modify a Parameter Reference … Point p1 = new Point(10, 20); Point p2 = new Point(30, 40); System.out.println(p1.x + “, “ + p1.y); swap(p1, p2); System.out.println(p1.x + “, “ + p1.y); …

22 Modify a Parameter Reference // Does not work in Java! void swap(Point p1, Point p2) { Point tmp = p1; p1 = p2; p2 = tmp; }

23 Modify a Parameter Reference The output of BOTH System.out.println calls will be 10, 20 Note that this differs greatly from reference passing in C++. If modified for syntax (including the & modifier), the previous method would have performed a swap operation if written in C++

24 Parameter Protection Since methods can modify the Objects they receive as parameters, we can not be sure of the object state after it is used as a parameter, other than it is still a reference to the same Object. What about final parameters, does that protect the parameter from modification?

25 final Parameters The final keyword can be used as a modifier in the formal parameter list This asserts (at the compiler level) that the actual parameter reference will not be pointed to a different object. Unfortunately, this does not protect our parameter’s internal values

26 Parameter Protection Check your object for equality before and after the method call –expensive –tedious –error prone Use an Immutable object –often self-identifying and documenting

27 Immutable Parameters The only guarantee that a parameter’s internal values can not be changed is if an immutable object, or some construct that enforces the concept of immutability is used Care must be taken for containers –Contents must be immutable –Iterator must not allow add/remove operations

28 References Mary Campione. The Java Tutorial Third Edition, book. Addison-Wesley, December 22, Holzner, S., (2001). Java 2 Black Book. Scottsdale: Coriolis. Patric Naughton. The Java Handbook, book. Green Hills Publishing, October, Xiaoping Jia. Object-Oriented Software Development Using Java Second Edition, book. Addison-Wesley, 2002.


Download ppt "Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank."

Similar presentations


Ads by Google