Download presentation
Presentation is loading. Please wait.
1
Methods Again Parameter Passage
Copyright © Curt Hill
2
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 © Curt Hill
3
Recall Triangle The last version of the triangle had two turtles
They moved independently Consider the next screens Copyright © Curt Hill
4
Example static void triangle( Turtle t, int len){ t.forward(len);
t.turn(120); } Copyright © Curt Hill
5
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 © Curt Hill
6
Gives Results Copyright © Curt Hill
7
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 © Curt Hill
8
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 © Curt Hill
9
Gives Results Copyright © Curt Hill
10
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 © Curt Hill
11
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 © Curt Hill
12
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 © Curt Hill
13
Results Copyright © Curt Hill
14
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 © Curt Hill
15
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 © Curt Hill
16
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 © Curt Hill
17
Triangle Changed static void triangle( Turtle t, int len){
t.forward(len); t.turn(120); len = 10; // Set len } Copyright © Curt Hill
18
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 © Curt Hill
19
Gives Results Copyright © Curt Hill
20
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 © Curt Hill
21
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 © Curt Hill
22
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 © Curt Hill
23
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 © Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.