You are on page 1of 7

Lesson 70

Arguments By Reference
and By Value

PRESENTED BY

Keith Vassallo icemalta.com


Passing Primitives
- When passing arguments to methods, primitives are always passed by value.
- This means a copy of the primitive’s value is sent to the method, not the
primitive itself.
public class SomeClass {
public static void main(String[] args) {
int value = 5;
System.out.println("Value from main: " + value);
square(value);
System.out.println("Value from main after calling method: " + value);
}

public static void square(int value) {


value *= value;
System.out.println("Value from method: " + value);

icemalta.com
}
}

- The square method is working on a copy of value.


- The original primitive remains unchanged.
Passing Objects
- Objects are passed by reference.
- This means that when you pass an object to a method, a reference to the
original object is passed, and not a copy.

public class SomeClass {


public static void main(String[] args) {
Dog d = new Dog();
d.age = 4;
System.out.println("Value from main: " + d.age);
ageDog(d);
System.out.println("Value from main after calling method: " + d.age);
}

public static void ageDog(Dog someDog) {


someDog.age+=1;

icemalta.com
System.out.println("Value from method: " + someDog.age);
}
}
Passing Objects
- The original reference, d, points to a location in memory.
- When we pass d to a method, the method receives that memory location,
and assigns it a reference, someDog.
- Both references now point to the same memory location.

d someDog

icemalta.com
Terminology
- We use some terms to make a difference between the reference of an
object, and the object itself.

Dog d = new Dog();

Referant

icemalta.com
d Reference
Great work, you’ve completed this lesson!

Next up: Creating and Using


Constructors.

icemalta.com
© 2011-2017 Institute of Computer Education Ltd.

The contents of this document are copyright to the Institute of Computer Education Ltd, unless otherwise stated, and must not be reproduced without permission.

Every effort has been made to trace all of the copyright holders, but if any have been inadvertently overlooked the Institute of Computer Education Ltd. will be pleased to make the necessary arrangements at the first
opportunity. Please contact us directly. While the Institute of Computer Education Ltd. has taken all reasonable care in the preparation of this work, the Institute of Computer Education Ltd. makes no representation,
express or implied, with regard to the accuracy of the information contained in this work and cannot accept any legal responsibility or liability for any errors or omissions from the work or the consequences thereof.
The reader assumes sole responsibility for the selection of these materials to achieve its intended results. Products and services that are referred to in this work may be either trademarks and/or registered
trademarks of their respective owners. The editors and author/s make no claim to these trademarks. icemalta.com

You might also like