Understanding Parameter Passing Parameters are Passed by Value
Understanding Parameter Passing
Parameters are Passed by Value z It is important to understand how parameter passing works. When you make changes to a parameter passed to a method, a separate copy of the value is passed. Any changes made to the parameter passed into the method will have no effect on the actual parameter.
Parameters are Passed by Value z It is important to understand how parameter passing works. When you make changes to a parameter passed to a method, a separate copy of the value is passed. Any changes made to the parameter passed into the method will have no effect on the actual parameter. z Java passes all parameters to a method by value.
Parameters are Passed by Value z It is important to understand how parameter passing works. When you make changes to a parameter passed to a method, a separate copy of the value is passed. Any changes made to the parameter passed into the method will have no effect on the actual parameter. z Java passes all parameters to a method by value. z This means that the current value of the actual parameter is copied into the formal parameter in the method header.
An Example Using Primitive Data Types public class Primitive. Param. Test { public void change. Value (int parameter) { parameter = 100; System. out. println("Inside method change. Value, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; Primitive. Param. Test test = new Primitive. Param. Test(); System. out. println("Initial parameter value is " + parameter); test. change. Value(parameter); System. out. println("After method change. Value call, parameter value is " + parameter); } } Using the code at left, we will demonstrate how primitive data types are handled as parameters in method calls.
An Example Using Primitive Data Types public class Primitive. Param. Test { public void change. Value (int parameter) { parameter = 100; System. out. println("Inside method change. Value, parameter value changed to " + parameter); } The program begins by assigning the value 25 to the integer variable parameter. public static void main (String args[]) { int parameter = 25; Primitive. Param. Test test = new Primitive. Param. Test(); System. out. println("Initial parameter value is " + parameter); value parameter test. change. Value(parameter); System. out. println("After method change. Value call, parameter value is " + parameter); } } Output : 25
An Example Using Primitive Data Types public class Primitive. Param. Test { The test object is created. public void change. Value (int parameter) { parameter = 100; System. out. println("Inside method change. Value, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; Primitive. Param. Test test = new Primitive. Param. Test(); System. out. println("Initial parameter value is " + parameter); value parameter test. change. Value(parameter); System. out. println("After method change. Value call, parameter value is " + parameter); } } Output : 25
An Example Using Primitive Data Types public class Primitive. Param. Test { public void change. Value (int parameter) { parameter = 100; System. out. println("Inside method change. Value, parameter value changed to " + parameter); } The value of parameter is displayed. public static void main (String args[]) { int parameter = 25; Primitive. Param. Test test = new Primitive. Param. Test(); System. out. println("Initial parameter value is " + parameter); value parameter test. change. Value(parameter); System. out. println("After method change. Value call, parameter value is " + parameter); } } Output : Initial parameter value is 25 25
An Example Using Primitive Data Types public class Primitive. Param. Test { public void change. Value (int parameter) { parameter = 100; System. out. println("Inside method change. Value, parameter value changed to " + parameter); } The Primitive. Param. Test object method change. Value is called. public static void main (String args[]) { int parameter = 25; Primitive. Param. Test test = new Primitive. Param. Test(); System. out. println("Initial parameter value is " + parameter); value parameter test. change. Value(parameter); System. out. println("After method change. Value call, parameter value is " + parameter); } } Output : Initial parameter value is 25 25
An Example Using Primitive Data Types public class Primitive. Param. Test { public void change. Value (int parameter) { parameter = 100; System. out. println("Inside method change. Value, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; Primitive. Param. Test test = new Primitive. Param. Test(); System. out. println("Initial parameter value is " + parameter); As a result of the method call, a copy of parameter is made. This copy is called the formal parameter. The formal parameter is sent to the method. value formal parameter test. change. Value(parameter); System. out. println("After method change. Value call, parameter value is " + parameter); } } Output : Initial parameter value is 25 25 25
An Example Using Primitive Data Types public class Primitive. Param. Test { public void change. Value (int parameter) { parameter = 100; System. out. println("Inside method change. Value, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; Primitive. Param. Test test = new Primitive. Param. Test(); System. out. println("Initial parameter value is " + parameter); When the method refers to parameter, it uses the formal parameter. The formal parameter is changed to 100. value formal parameter 100 parameter test. change. Value(parameter); System. out. println("After method change. Value call, parameter value is " + parameter); } } Output : Initial parameter value is 25 25
An Example Using Primitive Data Types public class Primitive. Param. Test { public void change. Value (int parameter) { parameter = 100; System. out. println("Inside method change. Value, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; Primitive. Param. Test test = new Primitive. Param. Test(); System. out. println("Initial parameter value is " + parameter); The value of the formal parameter is displayed. value formal parameter 100 parameter 25 test. change. Value(parameter); System. out. println("After method change. Value call, parameter value is " + parameter); } } Output : Initial parameter value is 25 Inside method change. Value, parameter value changed to 100
An Example Using Primitive Data Types public class Primitive. Param. Test { public void change. Value (int parameter) { parameter = 100; System. out. println("Inside method change. Value, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; Primitive. Param. Test test = new Primitive. Param. Test(); System. out. println("Initial parameter value is " + parameter); As the method is left, the formal parameter is destroyed. value formal parameter 100 parameter 25 test. change. Value(parameter); System. out. println("After method change. Value call, parameter value is " + parameter); } } Output : Initial parameter value is 25 Inside method change. Value, parameter value changed to 100
An Example Using Primitive Data Types public class Primitive. Param. Test { public void change. Value (int parameter) { parameter = 100; System. out. println("Inside method change. Value, parameter value changed to " + parameter); } Control is returned to the calling method. The formal parameter no longer exists. public static void main (String args[]) { int parameter = 25; Primitive. Param. Test test = new Primitive. Param. Test(); System. out. println("Initial parameter value is " + parameter); value parameter 25 test. change. Value(parameter); System. out. println("After method change. Value call, parameter value is " + parameter); } } Output : Initial parameter value is 25 Inside method change. Value, parameter value changed to 100
An Example Using Primitive Data Types public class Primitive. Param. Test { public void change. Value (int parameter) { parameter = 100; System. out. println("Inside method change. Value, parameter value changed to " + parameter); } The value of parameter is displayed. public static void main (String args[]) { int parameter = 25; Primitive. Param. Test test = new Primitive. Param. Test(); System. out. println("Initial parameter value is " + parameter); value parameter 25 test. change. Value(parameter); System. out. println("After method change. Value call, parameter value is " + parameter); } } Output : Initial parameter value is 25 Inside method change. Value, parameter value changed to 100 After method change. Value call, parameter value is 25
Passing Objects as Parameters z. How does Java pass objects by value?
Passing Objects as Parameters z. How does Java pass objects by value? z. When an object is passed to a method, we are actually passing a reference to that object.
Passing Objects as Parameters z. How does Java pass objects by value? z. When an object is passed to a method, we are actually passing a reference to that object. z. The value that is copied is the address of the object.
Referencing Objects Since a reference to an object actually contains the address of the object, the name of the object can been viewed as a “pointer” to the actual object. Remember that an object doesn’t “exist” until it is instantiated. Shape my. Object; my. Object null
Instantiating Objects The object comes into “existence” when it is instantiated. For example the following code will create the object below: my. Object = new Shape(); my. Object
Copying Objects When an object is assigned to another object, a copy of the reference to the object is made, not a copy of the actual object. Shape my. Copy = my. Object; my. Object my. Copy
Copying Objects In this example, my. Copy is an alias for my. Object. Both my. Object and my. Copy refer to the same object. my. Object my. Copy
Passing Objects as Parameters In the example involving primitive data types, when an int type is passed as a parameter, a copy, known as the formal parameter is used by the called method. When an object is passed to a method, the formal parameter used is an alias, which is a copy of the reference to an object. my. Object
Passing Objects as Parameters If the following method is called : foo. some. Method(my. Object); A copy or alias of my. Object is created and used as the formal parameter. my. Object
Passing Objects as Parameters If the following method is called : foo. some. Method(my. Object); A copy or alias of my. Object is created and used as the formal parameter. my. Object The formal parameter
Passing Objects as Parameters If the following method is called : foo. some. Method(my. Object); A copy or alias of my. Object is created and used as the formal parameter. my. Object The formal parameter Inside a method, if the formal parameter calls class methods that modify the state of the object, the object will remain in that state when the program flow returns to the instruction following the method call.
Passing Objects as Parameters my. Object Given the following code fragments: public class Foo { public void change. Object(Shape my. Object) { my. Object. change. Shape(); }. . . Foo changer = new Foo(); my. Object = new Shape(); changer. change. Object(my. Object);
Passing Objects as Parameters my. Object Given the following code fragments: public class Foo { public void change. Object(Shape my. Object) { my. Object. change. Shape(); }. . . Foo changer = new Foo(); my. Object = new Shape(); changer. change. Object(my. Object); When the change. Shape method is called, a formal parameter is created.
Passing Objects as Parameters my. Object Given the following code fragments: public class Foo { public void change. Object(Shape passed. Object) { passed. Object. change. Shape(); }. . . } The formal parameter . . . Foo changer = new Foo(); my. Object = new Shape(); changer. change. Object(my. Object); When the change. Shape method is called, a formal parameter is created. This formal parameter is referred to as passed. Object in the change. Object method.
Passing Objects as Parameters my. Object Given the following code fragments: public class Foo { public void change. Object(Shape passed. Object) { passed. Object. change. Shape(); }. . . } The formal parameter . . . Foo changer = new Foo(); my. Object = new Shape(); changer. change. Object(my. Object); When passed. Object. change. Shape() is called, the formal parameter, passed. Object, calls the object’s method change. Shape().
Passing Objects as Parameters my. Object Given the following code fragments: public class Foo { public void change. Object(Shape passed. Object) { passed. Object. change. Shape(); }. . . } The formal parameter . . . Foo changer = new Foo(); my. Object = new Shape(); changer. change. Object(my. Object); This causes the object to change its state (in this case, its shape).
Passing Objects as Parameters my. Object Given the following code fragments: public class Foo { public void change. Object(Shape passed. Object) { passed. Object. change. Shape(); }. . . } The formal parameter . . . Foo changer = new Foo(); my. Object = new Shape(); changer. change. Object(my. Object); When the change. Object method ends, the formal parameter ceases to exist.
Passing Objects as Parameters my. Object Given the following code fragments: public class Foo { public void change. Object(Shape passed. Object) { passed. Object. change. Shape(); }. . . Foo changer = new Foo(); my. Object = new Shape(); changer. change. Object(my. Object); When the change. Object method ends, the formal parameter ceases to exist. BUT, the object still exists in its changed state.
Changing object formal parameters If an object formal parameter can change an object’s state by calling the object’s methods, what happens when the value of the formal parameter is changed?
Changing object formal parameters public class Foo { public void change. Object(Shape passed. Object) { The code from the previous example is changed slightly. Shape new. Object = new Shape(); passed. Object = new. Object; passed. Object. change. Shape(); }. . . }
Changing object formal parameters public class Foo { If my. Object is passed to the change. Object method, public void change. Object(Shape passed. Object) { Shape new. Object = new Shape(); passed. Object = new. Object; passed. Object. change. Shape(); }. . . } my. Object
Changing object formal parameters public class Foo { If my. Object is passed to the change. Object method, public void change. Object(Shape passed. Object) { Shape new. Object = new Shape(); passed. Object = new. Object; passed. Object. change. Shape(); a formal parameter passed. Object is created. }. . . } my. Object passed. Object
Changing object formal parameters public class Foo { Another object, new. Object is created. public void change. Object(Shape passed. Object) { Shape new. Object = new Shape(); passed. Object = new. Object; passed. Object. change. Shape(); }. . . } my. Object passed. Object new. Object
Changing object formal parameters public class Foo { Another object, new. Object is created. public void change. Object(Shape passed. Object) { Shape new. Object = new Shape(); passed. Object = new. Object; The value of passed. Object is changed to new. Object. passed. Object. change. Shape(); }. . . } my. Object passed. Object new. Object
Changing object formal parameters public class Foo { public void change. Object(Shape passed. Object) { The formal parameter passed. Object, calls the method change. Shape(). Shape new. Object = new Shape(); passed. Object = new. Object; passed. Object. change. Shape(); }. . . } my. Object passed. Object new. Object
Changing object formal parameters public class Foo { public void change. Object(Shape passed. Object) { This causes the object to change its state (in this case, its shape). Shape new. Object = new Shape(); passed. Object = new. Object; passed. Object. change. Shape(); }. . . } my. Object passed. Object new. Object
Changing object formal parameters public class Foo { public void change. Object(Shape passed. Object) { When the change. Object method ends, the formal parameter passed. Object ceases to exist. Shape new. Object = new Shape(); passed. Object = new. Object; passed. Object. change. Shape(); }. . . } my. Object passed. Object new. Object
Changing object formal parameters public class Foo { public void change. Object(Shape passed. Object) { When the change. Object method ends, the formal parameter passed. Object ceases to exist. Shape new. Object = new Shape(); passed. Object = new. Object; passed. Object. change. Shape(); my. Object remains unchanged. }. . . } my. Object new. Object
Summary z Java passes all parameters to a method by value. z This means that the current value of the actual parameter is copied into the formal parameter in the method header. z Since a reference to an object actually contains the address of the object, the name of the object can been viewed as a “pointer” to the actual object.
Summary z When an object is passed to a method, the formal parameter used is an alias, which is a copy of the reference to an object. z An object’s formal parameter can change an object’s state by calling the class’ methods, but, y If the value of the formal parameter is changed, the actual object remains unchanged.
- Slides: 45