You are on page 1of 21

CS 401: Introduction to Advanced Studies (Data Structures)

Vijay K. Gurbani, Ph.D., Illinois Institute of Technology


Lecture 1: Introduction

CS 401
vgurbani@iit.edu

Memory layout in modern


computers

CS 401
vgurbani@iit.edu

Java: Pass by value or pass by


reference?

CS 401
vgurbani@iit.edu

Java: Pass by reference or value?


MyPoint p1 = new MyPoint(10, 20);
MyPoint p2 = new MyPoint(30, 40);

public static void swap1(MyPoint arg1, MyPoint arg2) {


MyPoint temp;
temp = arg1;
arg1 = arg2;
arg2 = temp;
}

swap1(p1, p2);
p1 = ??
p2 = ??
CS 401
vgurbani@iit.edu

Java: Pass by reference or value?


MyPoint p1 = new MyPoint(10, 20);
MyPoint p2 = new MyPoint(30, 40);

public static void swap1(MyPoint arg1, MyPoint arg2) {


MyPoint temp;
temp = arg1;
arg1 = arg2;
arg2 = temp;
}

swap1(p1, p2);
p1 = MyPoint[x=10,y=20]
p2 = MyPoint[x=30,y=40]
CS 401
vgurbani@iit.edu

References: Tracing swap1()


MyPoint p1 = new MyPoint(10,20);
x = 10
java.lang.Integer@fe9e47
p1

y = 20
java.lang.Integer@4947d3
MyPoint@bc9f58
x = 30
java.lang.Integer@1ad4242

MyPoint p2 = new MyPoint(30,40);

p2

y = 40
java.lang.Integer@1d91382
MyPoint@10a945c

CS 401
vgurbani@iit.edu

References: Tracing swap1()


MyPoint p1 = new MyPoint(10,20);
x = 10
java.lang.Integer@fe9e47
p1

y = 20
java.lang.Integer@4947d3

arg1
MyPoint@bc9f58
x = 30
java.lang.Integer@1ad4242
MyPoint p2 = new MyPoint(30,40);

p2

y = 40
java.lang.Integer@1d91382

arg2
MyPoint@10a945c
CS 401
vgurbani@iit.edu

References: Tracing swap1()


MyPoint p1 = new MyPoint(10,20);
x = 10
java.lang.Integer@fe9e47
p1

y = 20
java.lang.Integer@4947d3

arg1
MyPoint@bc9f58
x = 30

temp

java.lang.Integer@1ad4242
MyPoint p2 = new MyPoint(30,40);

p2

y = 40
java.lang.Integer@1d91382

arg2
MyPoint@10a945c
CS 401
vgurbani@iit.edu

References: Tracing swap1()


MyPoint p1 = new MyPoint(10,20);
x = 10
java.lang.Integer@fe9e47
p1

y = 20
java.lang.Integer@4947d3

arg1
MyPoint@bc9f58
x = 30

temp

java.lang.Integer@1ad4242
MyPoint p2 = new MyPoint(30,40);

p2

y = 40
java.lang.Integer@1d91382

arg2
MyPoint@10a945c
CS 401
vgurbani@iit.edu

References: Tracing swap1()


MyPoint p1 = new MyPoint(10,20);
x = 10
java.lang.Integer@fe9e47
p1

y = 20
java.lang.Integer@4947d3

arg1
MyPoint@bc9f58
x = 30

temp

java.lang.Integer@1ad4242
MyPoint p2 = new MyPoint(30,40);

p2

y = 40
java.lang.Integer@1d91382

arg2
MyPoint@10a945c
CS 401
vgurbani@iit.edu

10

References

All the assignments etc. work as long as you


are in the function scope.
Once you are out of function scope, then things
don't work.
Ok, so ... let's see swap2().

CS 401
vgurbani@iit.edu

11

Java: Pass by reference or value?


MyPoint p1 = new MyPoint(10, 20);
MyPoint p2 = new MyPoint(30, 40);
public static void swap2(MyPoint arg1, MyPoint arg2) {
Integer x, y;
x = arg1.x;
y = arg1.y;
arg1.x = arg2.x;
arg1.y = arg2.y;
arg2.x = x;
arg2.y = y;
}
swap2(p1, p2);
p1 = ??
p2 = ??
CS 401
vgurbani@iit.edu

12

Java: Pass by reference or value?


MyPoint p1 = new MyPoint(10, 20);
MyPoint p2 = new MyPoint(30, 40);
public static void swap2(MyPoint arg1, MyPoint arg2) {
Integer x, y;
x = arg1.x;
y = arg1.y;
arg1.x = arg2.x;
arg1.y = arg2.y;
arg2.x = x;
arg2.y = y;
}
swap2(p1, p2);
p1 = MyPoint[x=30,y=40]
p2 = MyPoint[x=10,y=20]
CS 401
vgurbani@iit.edu

13

References: Tracing swap2()


MyPoint p1 = new MyPoint(10,20);
x = 10
java.lang.Integer@fe9e47
p1

y = 20
java.lang.Integer@4947d3
MyPoint@bc9f58
x = 30
java.lang.Integer@1ad4242

MyPoint p2 = new MyPoint(30,40);

p2

y = 40
java.lang.Integer@1d91382
MyPoint@10a945c

CS 401
vgurbani@iit.edu

14

References: Tracing swap2()


MyPoint p1 = new MyPoint(10,20);
x = 10
java.lang.Integer@fe9e47
p1
arg1

y = 20
java.lang.Integer@4947d3
MyPoint@bc9f58
x = 30
java.lang.Integer@1ad4242

MyPoint p2 = new MyPoint(30,40);

p2
arg2

y = 40
java.lang.Integer@1d91382
MyPoint@10a945c

CS 401
vgurbani@iit.edu

15

References: Tracing swap2()


MyPoint p1 = new MyPoint(10,20);
x = 10
java.lang.Integer@fe9e47
p1
arg1

y = 20

x
y

java.lang.Integer@4947d3
MyPoint@bc9f58
x = 30
java.lang.Integer@1ad4242

MyPoint p2 = new MyPoint(30,40);

p2
arg2

y = 40
java.lang.Integer@1d91382
MyPoint@10a945c

x = arg1.x;
y = arg1.y;
CS 401
vgurbani@iit.edu

16

References: Tracing swap2()


MyPoint p1 = new MyPoint(10,20);
x = 30
java.lang.Integer@1ad4242
y = 40
p1

java.lang.Integer@1d91382

x
y

x = 10
java.lang.Integer@fe9e47
y = 20
java.lang.Integer@4947d3

arg1
MyPoint@bc9f58

MyPoint p2 = new MyPoint(30,40);

p2
arg2
MyPoint@10a945c

arg1.x = arg2.x;
arg1.y = arg2.y;
CS 401
vgurbani@iit.edu

17

References: Tracing swap2()


MyPoint p1 = new MyPoint(10,20);
x = 30
java.lang.Integer@1ad4242
p1
arg1

x
y

y = 40
java.lang.Integer@1d91382
MyPoint@bc9f58

x = 10
java.lang.Integer@fe9e47
MyPoint p2 = new MyPoint(30,40);

p2
arg2

y = 20
java.lang.Integer@4947d3
MyPoint@10a945c

arg2.x = x;
arg2.y = y;
CS 401
vgurbani@iit.edu

18

So, what is the answer? Pass by


value or pass by reference?
Java is pass-by-value.
The short answer is that Java manipulates objects using references but it passes object references to
methods using the value of the object reference (which will be a memory location).
The long answer is in the Java specification, see Section 8.4.1 in [1]. I quote (emphasis is mine):
"When the method or constructor is invoked (Section 15.12), the values of the actual argument
expressions initialize newly created parameter variables, each of the declared type, before execution
of the body of the method or constructor."
And from James Gosling, the creator of Java (again, emphasis is mine):
Some people will say incorrectly that objects are passed by reference. In programming language
design, the term pass by reference properly means that when an argument is passed to a function, the
invoked function gets a reference to the original value, not a copy of its value. If the function modifies
its parameter, the value in the calling code will be changed because the argument and parameter use
the same slot in memory. The Java programming language does not pass objects by reference;
it passes object references by value. Because two copies of the same reference refer to the same
actual object, changes made through one reference variable are visible through the other. There is
exactly one parameter passing mode pass by value and that helps keep things simple.
[1] http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.1
CS 401
vgurbani@iit.edu

19

OK, then ... explain Swap3()


MyPoint p1 = new MyPoint(10, 20);
MyPoint p2 = new MyPoint(30, 40);

public static void swap3(MyPoint arg1, MyPoint arg2) {


MyPoint temp = new MyPoint(arg1.x, arg1.y);
arg1.x = arg2.x;
arg1.y = arg2.y;
arg2 = temp;
}

swap3(p1, p2);
p1 = ??
p2 = ??
CS 401
vgurbani@iit.edu

20

Swap3()
MyPoint p1 = new MyPoint(10, 20);
MyPoint p2 = new MyPoint(30, 40);

public static void swap3(MyPoint arg1, MyPoint arg2) {


MyPoint temp = new MyPoint(arg1.x, arg1.y);
arg1.x = arg2.x;
arg1.y = arg2.y;
arg2 = temp;
}

swap3(p1, p2);
p1 = MyPoint[x=30,y=40]
p2 = MyPoint[x=30,y=40]

Ideas on why the answer is what


comes up?
CS 401
vgurbani@iit.edu

21

You might also like