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

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

Embed Size (px)

Citation preview

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

Parameter Passing

Jason Fullowan, Mariusz G. Kedziora, George Blank

Page 2: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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.

Page 3: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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.

Page 4: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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

Page 5: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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.

Page 6: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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.

Page 7: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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.

Page 8: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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

}

Page 9: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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.

Page 10: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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.

Page 11: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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.

Page 12: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

Parameter Passing – Applet Example

• An HTML file code section:<applet code=Testing width=40 height=40>

<param name=fontName value=Universal>

<param name=fontSize value=14>

<param name=leading value=2>

<param name=accountEn value=true>

</applet>

Page 13: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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”);

...

}

Page 14: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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

Page 15: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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

Page 16: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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

Page 17: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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?

Page 18: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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.

Page 19: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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;

}

Page 20: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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

Page 21: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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

Page 22: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

Modify a Parameter Reference

// Does not work in Java!

void swap(Point p1, Point p2) {

Point tmp = p1;

p1 = p2;

p2 = tmp;

}

Page 23: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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++

Page 24: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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?

Page 25: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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

Page 26: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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

Page 27: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

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

Page 28: Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank

References

• Mary Campione. The Java Tutorial Third Edition, book. Addison-Wesley, December 22, 1998.

• Holzner, S., (2001). Java 2 Black Book. Scottsdale: Coriolis.

• Patric Naughton. The Java Handbook, book. Green Hills Publishing, October, 1996.

• Xiaoping Jia. Object-Oriented Software Development Using Java Second Edition, book. Addison-Wesley, 2002.