Methods Again Parameter Passage Copyright © 1997 - 2009 Curt Hill
The Handle Scheme Objects are referred to by a handle They are not valid until a value is assigned to them Either from An existing object A use of new The use of an existing object makes another handle that points at the same object Copyright © 1997 - 2009 Curt Hill
Recall Triangle The last version of the triangle had two turtles They moved independently Consider the next screens Copyright © 1997 - 2009 Curt Hill
Example static void triangle( Turtle t, int len){ t.forward(len); t.turn(120); } Copyright © 1997 - 2009 Curt Hill
Called by public static void main( String [] a){ World w = new World(400,240); Turtle yertle = new Turtle(w); yertle.setPenWidth(3); triangle(yertle,80); Turtle sam=new Turtle(100,100,w); sam.setPenWidth(4); triangle(sam,40); } Copyright © 1997 - 2009 Curt Hill
Gives Results Copyright © 1997 - 2009 Curt Hill
Independence The two turtles move independently since they are two objects We will get a different result if we assign one to the other instead of creating a second Copyright © 1997 - 2009 Curt Hill
Called by public static void main( String [] a){ World w = new World(400,240); Turtle yertle = new Turtle(w); yertle.setPenWidth(3); triangle(yertle,80); Turtle sam = yertle; sam.setPenWidth(4); triangle(sam,40); } Copyright © 1997 - 2009 Curt Hill
Gives Results Copyright © 1997 - 2009 Curt Hill
What was changed? The middle statement Turtle sam=new Turtle(100,100,w); Was changed to Turtle sam = yertle; Instead of sam being independent, a new turtle, it is a handle to the same old turtle Copyright © 1997 - 2009 Curt Hill
The difference Turtle yertle = new Turtle(w); Turtle instance yertle Turtle sam = new Turtle(100,100,w); sam Turtle instance Turtle yertle = new Turtle(w); Turtle instance yertle Turtle sam = yertle; sam Copyright © 1997 - 2009 Curt Hill
Another Example public static void main( String [] a){ World w = new World(400,240); Turtle yertle = new Turtle(w); yertle.setPenWidth(3); Turtle sam = yertle; sam.forward(50); yertle.turn(90); sam.turn(45); yertle.forward(90); } Copyright © 1997 - 2009 Curt Hill
Results Copyright © 1997 - 2009 Curt Hill
Commentary It is just one turtle in this case yertle and sam are synonyms or aliases for exactly the same object Any method on sam affects the common object which yertle also points at Once we understand this we can understand parameter passage Copyright © 1997 - 2009 Curt Hill
Parameter Passage Mechanisms How are parameters passed in Java? In general there are several ways to do this Java always passes parameters by value This just means a copy is made and given to the method This has a different effect for primitives and objects Copyright © 1997 - 2009 Curt Hill
Passing Primitives Call by value is the way a primitive is passed to a method A copy of the primitive is made and given to the method If the method changes the primitive, the change is made to the copy, not to the original Thus the actual parameter is never changed Consider triangle again Copyright © 1997 - 2009 Curt Hill
Triangle Changed static void triangle( Turtle t, int len){ t.forward(len); t.turn(120); len = 10; // Set len } Copyright © 1997 - 2009 Curt Hill
Called by public static void main(String [] a){ World w = new World(400,240); Turtle yertle = new Turtle(w); yertle.setPenWidth(3); Turtle sam = new Turtle(100,100,w); sam.setPenWidth(4); int dist = 80; triangle(yertle,dist); triangle(sam,dist); System.out.println(dist); } Copyright © 1997 - 2009 Curt Hill
Gives Results Copyright © 1997 - 2009 Curt Hill
Commentary Even though triangle changed the value of its parameter it had no effect on the outside It changed a copy, rather than the original It works a little different with objects As we have and will see Copyright © 1997 - 2009 Curt Hill
Passing Objects When an object is passed as a parameter a copy of the handle is passed As we saw with assignment we then have two handles pointing at the same object These are synonyms or aliases Copyright © 1997 - 2009 Curt Hill
Triangle revisited When the triangle method was called it moved the turtle and changed its direction The copy of the handle still pointed at the same object Unlike passing a primitive (like int) we can actually change the object Copyright © 1997 - 2009 Curt Hill
Finally The net effect is that we can change the characteristics of a passed object In the turtle: direction, position etc. A copy of a handle still points at the object A primitive cannot be changed in any significant way Copyright © 1997 - 2009 Curt Hill