45
Understanding Parameter Passing

Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Embed Size (px)

Citation preview

Page 1: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Understanding Parameter Passing

Page 2: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Parameters are Passed by Value

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.

Page 3: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Parameters are Passed by Value

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.

Java passes all parameters to a method by value.

Page 4: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Parameters are Passed by Value

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.

Java passes all parameters to a method by value. This means that the current value of the actual

parameter is copied into the formal parameter in the method header.

Page 5: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

An ExampleUsing Primitive Data Types

public class PrimitiveParamTest {

public void changeValue (int parameter)

{

parameter = 100;

System.out.println("Inside method changeValue,parameter value changed to " + parameter);

}

public static void main (String args[])

{

int parameter = 25;

PrimitiveParamTest test = new PrimitiveParamTest();

System.out.println("Initial parameter value is " + parameter);

test.changeValue(parameter);

System.out.println("After method changeValue call,parameter value is " + parameter);

}

}

Using the code at left, we will demonstrate how primitive data types are handled as parameters in method calls.

Page 6: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

An ExampleUsing Primitive Data Types

public class PrimitiveParamTest {

public void changeValue (int parameter)

{

parameter = 100;

System.out.println("Inside method changeValue,parameter value changed to " + parameter);

}

public static void main (String args[])

{

int parameter = 25;

PrimitiveParamTest test = new PrimitiveParamTest();

System.out.println("Initial parameter value is " + parameter);

test.changeValue(parameter);

System.out.println("After method changeValue call,parameter value is " + parameter);

}

}

The program begins by assigning the value 25 to the integer variable parameter.

25parameter

value

Output :

Page 7: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

An ExampleUsing Primitive Data Types

public class PrimitiveParamTest {

public void changeValue (int parameter)

{

parameter = 100;

System.out.println("Inside method changeValue,parameter value changed to " + parameter);

}

public static void main (String args[])

{

int parameter = 25;

PrimitiveParamTest test = new PrimitiveParamTest();

System.out.println("Initial parameter value is " + parameter);

test.changeValue(parameter);

System.out.println("After method changeValue call,parameter value is " + parameter);

}

}

The test object is created.

25parameter

value

Output :

Page 8: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

An ExampleUsing Primitive Data Types

public class PrimitiveParamTest {

public void changeValue (int parameter)

{

parameter = 100;

System.out.println("Inside method changeValue,parameter value changed to " + parameter);

}

public static void main (String args[])

{

int parameter = 25;

PrimitiveParamTest test = new PrimitiveParamTest();

System.out.println("Initial parameter value is " + parameter);

test.changeValue(parameter);

System.out.println("After method changeValue call,parameter value is " + parameter);

}

}

The value of parameter is displayed.

25parameter

value

Output :

Initial parameter value is 25

Page 9: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

An ExampleUsing Primitive Data Types

public class PrimitiveParamTest {

public void changeValue (int parameter)

{

parameter = 100;

System.out.println("Inside method changeValue,parameter value changed to " + parameter);

}

public static void main (String args[])

{

int parameter = 25;

PrimitiveParamTest test = new PrimitiveParamTest();

System.out.println("Initial parameter value is " + parameter);

test.changeValue(parameter);

System.out.println("After method changeValue call,parameter value is " + parameter);

}

}

The PrimitiveParamTest object method changeValue is called.

25parameter

value

Output :

Initial parameter value is 25

Page 10: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

An ExampleUsing Primitive Data Types

public class PrimitiveParamTest {

public void changeValue (int parameter)

{

parameter = 100;

System.out.println("Inside method changeValue,parameter value changed to " + parameter);

}

public static void main (String args[])

{

int parameter = 25;

PrimitiveParamTest test = new PrimitiveParamTest();

System.out.println("Initial parameter value is " + parameter);

test.changeValue(parameter);

System.out.println("After method changeValue call,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.

25parameter

value

Output :

Initial parameter value is 25

25formal parameter

Page 11: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

An ExampleUsing Primitive Data Types

public class PrimitiveParamTest {

public void changeValue (int parameter)

{

parameter = 100;

System.out.println("Inside method changeValue,parameter value changed to " + parameter);

}

public static void main (String args[])

{

int parameter = 25;

PrimitiveParamTest test = new PrimitiveParamTest();

System.out.println("Initial parameter value is " + parameter);

test.changeValue(parameter);

System.out.println("After method changeValue call,parameter value is " + parameter);

}

}

When the method refers to parameter, it uses the formal parameter. The formal parameter is changed to 100.

25parameter

value

Output :

Initial parameter value is 25

100formal parameter

Page 12: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

An ExampleUsing Primitive Data Types

public class PrimitiveParamTest {

public void changeValue (int parameter)

{

parameter = 100;

System.out.println("Inside method changeValue,parameter value changed to " + parameter);

}

public static void main (String args[])

{

int parameter = 25;

PrimitiveParamTest test = new PrimitiveParamTest();

System.out.println("Initial parameter value is " + parameter);

test.changeValue(parameter);

System.out.println("After method changeValue call,parameter value is " + parameter);

}

}

The value of the formal parameter is displayed.

25parameter

value

Output :

Initial parameter value is 25

Inside method changeValue, parameter value changed to 100

100formal parameter

Page 13: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

An ExampleUsing Primitive Data Types

public class PrimitiveParamTest {

public void changeValue (int parameter)

{

parameter = 100;

System.out.println("Inside method changeValue,parameter value changed to " + parameter);

}

public static void main (String args[])

{

int parameter = 25;

PrimitiveParamTest test = new PrimitiveParamTest();

System.out.println("Initial parameter value is " + parameter);

test.changeValue(parameter);

System.out.println("After method changeValue call,parameter value is " + parameter);

}

}

As the method is left, the formal parameter is destroyed.

25parameter

value

Output :

Initial parameter value is 25

Inside method changeValue, parameter value changed to 100

100formal parameter

Page 14: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

An ExampleUsing Primitive Data Types

public class PrimitiveParamTest {

public void changeValue (int parameter)

{

parameter = 100;

System.out.println("Inside method changeValue,parameter value changed to " + parameter);

}

public static void main (String args[])

{

int parameter = 25;

PrimitiveParamTest test = new PrimitiveParamTest();

System.out.println("Initial parameter value is " + parameter);

test.changeValue(parameter);

System.out.println("After method changeValue call,parameter value is " + parameter);

}

}

Control is returned to the calling method. The formal parameter no longer exists.

25parameter

value

Output :

Initial parameter value is 25

Inside method changeValue, parameter value changed to 100

Page 15: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

An ExampleUsing Primitive Data Types

public class PrimitiveParamTest {

public void changeValue (int parameter)

{

parameter = 100;

System.out.println("Inside method changeValue,parameter value changed to " + parameter);

}

public static void main (String args[])

{

int parameter = 25;

PrimitiveParamTest test = new PrimitiveParamTest();

System.out.println("Initial parameter value is " + parameter);

test.changeValue(parameter);

System.out.println("After method changeValue call,parameter value is " + parameter);

}

}

The value of parameter is displayed.

25parameter

value

Output :

Initial parameter value is 25

Inside method changeValue, parameter value changed to 100

After method changeValue call, parameter value is 25

Page 16: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Passing Objects as Parameters

How does Java pass objects by value?

Page 17: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Passing Objects as Parameters

How does Java pass objects by value?

When an object is passed to a method, we are actually passing a reference to that object.

Page 18: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Passing Objects as Parameters

How does Java pass objects by value?

When an object is passed to a method, we are actually passing a reference to that object.

The value that is copied is the address of the object.

Page 19: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

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

null

Page 20: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Instantiating Objects

The object comes into “existence” when it is instantiated. For example the following code will create the object below:

myObject = new Shape();

myObject

Page 21: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

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 myCopy = myObject;

myCopy myObject

Page 22: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Copying Objects

In this example, myCopy is an alias for myObject. Both myObject and myCopy refer to the same object.

myCopy myObject

Page 23: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

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.

myObject

Page 24: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Passing Objects as Parameters

If the following method is called :

foo.someMethod(myObject);A copy or alias of myObject is created and used as the formal parameter.

myObject

Page 25: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Passing Objects as Parameters

If the following method is called :

foo.someMethod(myObject);A copy or alias of myObject is created and used as the formal parameter.

myObject

The formal parameter

Page 26: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Passing Objects as Parameters

If the following method is called :

foo.someMethod(myObject);A copy or alias of myObject is created and used as the formal parameter.

myObject

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.

Page 27: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Passing Objects as Parameters

myObjectGiven the following code fragments:

public class Foo {

public void changeObject(Shape myObject) {

myObject.changeShape();

}

. . .

}

. . .

Foo changer = new Foo();

myObject = new Shape();

changer.changeObject(myObject);

Page 28: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Passing Objects as Parameters

myObjectGiven the following code fragments:

public class Foo {

public void changeObject(Shape myObject) {

myObject.changeShape();

}

. . .

}

. . .

Foo changer = new Foo();

myObject = new Shape();

changer.changeObject(myObject);

When the changeShape method is called, a formal parameter is created.

Page 29: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Passing Objects as Parameters

myObject

The formal parameter

Given the following code fragments:

public class Foo {

public void changeObject(Shape passedObject) {

passedObject.changeShape();

}

. . .

}

. . .

Foo changer = new Foo();

myObject = new Shape();

changer.changeObject(myObject);

When the changeShape method is called, a formal parameter is created.

This formal parameter is referred to as passedObject in the changeObject method.

Page 30: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Passing Objects as Parameters

myObject

The formal parameter

Given the following code fragments:

public class Foo {

public void changeObject(Shape passedObject) {

passedObject.changeShape();

}

. . .

}

. . .

Foo changer = new Foo();

myObject = new Shape();

changer.changeObject(myObject);

When passedObject.changeShape() is called, the formal parameter, passedObject, calls the object’s method changeShape().

Page 31: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Passing Objects as Parameters

myObject

The formal parameter

Given the following code fragments:

public class Foo {

public void changeObject(Shape passedObject) {

passedObject.changeShape();

}

. . .

}

. . .

Foo changer = new Foo();

myObject = new Shape();

changer.changeObject(myObject);

This causes the object to change its state (in this case, its shape).

Page 32: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Passing Objects as Parameters

myObject

The formal parameter

Given the following code fragments:

public class Foo {

public void changeObject(Shape passedObject) {

passedObject.changeShape();

}

. . .

}

. . .

Foo changer = new Foo();

myObject = new Shape();

changer.changeObject(myObject);

When the changeObject method ends, the formal parameter ceases to exist.

Page 33: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Passing Objects as Parameters

myObjectGiven the following code fragments:

public class Foo {

public void changeObject(Shape passedObject) {

passedObject.changeShape();

}

. . .

}

. . .

Foo changer = new Foo();

myObject = new Shape();

changer.changeObject(myObject);

When the changeObject method ends, the formal parameter ceases to exist.

BUT, the object still exists in its changed state.

Page 34: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

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?

Page 35: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Changing object formal parameters

public class Foo {

public void changeObject(Shape passedObject) {

Shape newObject = new Shape();

passedObject = newObject;

passedObject.changeShape();

}

. . .

}

The code from the previous example is changed slightly.

Page 36: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Changing object formal parameters

public class Foo {

public void changeObject(Shape passedObject) {

Shape newObject = new Shape();

passedObject = newObject;

passedObject.changeShape();

}

. . .

}

If myObject is passed to the changeObject method,

myObject

Page 37: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Changing object formal parameters

public class Foo {

public void changeObject(Shape passedObject) {

Shape newObject = new Shape();

passedObject = newObject;

passedObject.changeShape();

}

. . .

}

If myObject is passed to the changeObject method,

a formal parameter passedObject is created.

myObjectpassedObject

Page 38: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Changing object formal parameters

public class Foo {

public void changeObject(Shape passedObject) {

Shape newObject = new Shape();

passedObject = newObject;

passedObject.changeShape();

}

. . .

}

Another object, newObject is created.

myObjectpassedObject newObject

Page 39: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Changing object formal parameters

public class Foo {

public void changeObject(Shape passedObject) {

Shape newObject = new Shape();

passedObject = newObject;

passedObject.changeShape();

}

. . .

}

Another object, newObject is created.

The value of passedObject is changed to newObject.

myObjectpassedObject newObject

Page 40: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Changing object formal parameters

public class Foo {

public void changeObject(Shape passedObject) {

Shape newObject = new Shape();

passedObject = newObject;

passedObject.changeShape();

}

. . .

}

The formal parameter passedObject, calls the method changeShape().

myObjectpassedObject newObject

Page 41: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Changing object formal parameters

public class Foo {

public void changeObject(Shape passedObject) {

Shape newObject = new Shape();

passedObject = newObject;

passedObject.changeShape();

}

. . .

}

This causes the object to change its state (in this case, its shape).

myObjectpassedObject newObject

Page 42: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Changing object formal parameters

public class Foo {

public void changeObject(Shape passedObject) {

Shape newObject = new Shape();

passedObject = newObject;

passedObject.changeShape();

}

. . .

}

When the changeObject method ends, the formal parameter passedObject ceases to exist.

myObjectpassedObject newObject

Page 43: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Changing object formal parameters

public class Foo {

public void changeObject(Shape passedObject) {

Shape newObject = new Shape();

passedObject = newObject;

passedObject.changeShape();

}

. . .

}

When the changeObject method ends, the formal parameter passedObject ceases to exist.

myObject remains unchanged.

myObjectnewObject

Page 44: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Summary

Java passes all parameters to a method by value. This means that the current value of the actual

parameter is copied into the formal parameter in the method header.

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.

Page 45: Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a

Summary

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.

An object’s formal parameter can change an object’s state by calling the class’ methods, but, If the value of the formal parameter is changed, the

actual object remains unchanged.