Lecture 8
Box and pointer notation
Many slides were borrowed from Josh Hug
Reminders
Expected output?
public class Example {
public static void test() {
int a;
int b;
System.out.print(a + b);
}
public static void main(String[] args) {
test();
}
}
}
A: 0
B: 01
C: 1
D: Error
Expected output?
public class Example {
int a;
int b;
public static void test() {
System.out.print(a + b);
}
public static void main(String[] args) {
test();
}
}
A: 0
B: 01
C: 1
D: Error
Expected output?
public class Example {
static int a;
static int b;
public static void test() {
System.out.print(a + b);
}
public static void main(String[] args) {
test();
}
}
A: 0
B: 01
C: 1
D: Error
Discussion Question
Will the change to b affect a?
A. Yes
B. No
Answer: Visualizer
Aquarium a = new Aquarium(100, 9.5);
Aquarium b;
b = a;
b.price = 50;
System.out.println(a);
System.out.println(b);
Discussion Question
Will the change to b affect a?
A. Yes
B. No
Answer: Visualizer
Aquarium a = new Aquarium(100, 9.5);
Aquarium b;
b = a;
b.price = 50;
System.out.println(a);
System.out.println(b);
price: 50, tank volume: 9.50
price: 50, tank volume: 9.50
Discussion Question
Will the change to x affect y?
A. Yes
B. No
Answer: Visualizer
int x = 5;
int y;
y = x;
x = 2;
System.out.println("x is: " + x);
System.out.println("y is: " + y);
Discussion Question
Will the change to x affect y?
A. Yes
B. No
Answer: Visualizer
int x = 5;
int y;
y = x;
x = 2;
System.out.println("x is: " + x);
System.out.println("y is: " + y);
x is: 2
y is: 5
Bits
Your computer stores information in “memory”.
Each Java type has a different way to interpret the bits:
Note: Precise representations may vary from machine to machine.
Declaring a Variable (Simplified)
When you declare a variable of a certain type in Java:
int x;
double y;
x = -1431195969;
y = 567213.112;
Declaring a Variable (Simplified)
When you declare a variable of a certain type in Java:
int x;
double y;
x = -1431195969;
y = 567213.112;
Declaring a Variable (Simplified)
When you declare a variable of a certain type in Java:
int x;
double y;
x = -1431195969;
y = 567213.112;
Declaring a Variable (Simplified)
When you declare a variable of a certain type in Java:
int x;
double y;
x = -1431195969;
y = 567213.112;
Simplified Box Notation
We’ll use simplified box notation from here on out:
int x;
double y;
x = -1431195969;
y = 567213.112;
The Golden Rule of Equals (GRoE)
References
Reference Types
There are 8 primitive types in Java:
Everything else, including arrays, is a reference type.
Class Instantiations
When we instantiate an Object (e.g. Car, Aquarium, Queue):
public class Aquarium {
public int price;
public double volume;
public Aquarium(int p, double v) {
price = p;
volume = v;
}
}
new Aquarium(50, 9.5);
32 bits
64 bits
Class Instantiations
When we instantiate an Object (e.g. Dog, Aquarium, Queue):
new Aquarium(50, 9.5);
32 bits
64 bits
....00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111101000010000000010000010011001100110011001100110011001100110011001101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000....
Green is price, blue is volume.
(In reality, total Aquarium size is slightly larger than 96 bits.)
Class Instantiations
Can think of new as returning the address of the newly created object.
32 bits
64 bits
new Aquarium(50, 9.5);
2384723423
....00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111101000010000000010000010011001100110011001100110011001100110011001101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000....
2384723423th bit
Reference Type Variable Declarations
When we declare a variable of any reference type (Aquarium, Dog, Queue):
Aquarium someAqua;
someAqua = null;
64 bits
Aquarium someAqua;
someAqua = new Aquarium(50, 9.5);
96 bits
64 bits
someAqua
someAqua
Reference Type Variable Declarations
The 64 bit addresses are meaningless to us as humans, so we’ll represent:
This is sometimes called “box and pointer” notation.
64 bits
someAqua
64 bits
96 bits
Reference Types Obey the Golden Rule of Equals
Just as with primitive types, the equals sign copies the bits.
Aquarium a;
a = new Aquarium(100, 9.5);
Aquarium b;
b = a;
a is 64 bits
Reference Types Obey the Golden Rule of Equals
Just as with primitive types, the equals sign copies the bits.
Aquarium a;
a = new Aquarium(100, 9.5);
Aquarium b;
b = a;
a is 64 bits
The Aquarium shown is 96 bits.
Reference Types Obey the Golden Rule of Equals
Just as with primitive types, the equals sign copies the bits.
Aquarium a;
a = new Aquarium(100, 9.5);
Aquarium b;
b = a;
a is 64 bits
The Aquarium shown is 96 bits.
Reference Types Obey the Golden Rule of Equals
Just as with primitive types, the equals sign copies the bits.
Aquarium a;
a = new Aquarium(100, 9.5);
Aquarium b;
b = a;
Note: b is currently undefined, not null!
a and b are 64 bits
The Aquarium shown is 96 bits.
Reference Types Obey the Golden Rule of Equals
Just as with primitive types, the equals sign copies the bits.
Aquarium a;
a = new Aquarium(100, 9.5);
Aquarium b;
b = a;
a and b are 64 bits
The Aquarium shown is 96 bits.
Parameter Passing
The Golden Rule of Equals (and Parameter Passing)
Given variables b and a:
Passing parameters obeys the same rule: Simply copy the bits to the new scope.
public static double average(double a, double b) {
return (a + b) / 2;
}
public static void main(String[] args) {
double x = 5.5;
double y = 10.5;
double avg = average(x, y);
}
The Golden Rule of Equals (and Parameter Passing)
Given variables b and a:
Passing parameters obeys the same rule: Simply copy the bits to the new scope.
public static double average(double a, double b) {
return (a + b) / 2;
}
public static void main(String[] args) {
double x = 5.5;
double y = 10.5;
double avg = average(x, y);
}
The Golden Rule of Equals (and Parameter Passing)
Given variables b and a:
Passing parameters obeys the same rule: Simply copy the bits to the new scope.
public static double average(double a, double b) {
return (a + b) / 2;
}
public static void main(String[] args) {
double x = 5.5;
double y = 10.5;
double avg = average(x, y);
}
The Golden Rule of Equals (and Parameter Passing)
Given variables b and a:
Passing parameters obeys the same rule: Simply copy the bits to the new scope.
public static double average(double a, double b) {
return (a + b) / 2;
}
public static void main(String[] args) {
double x = 5.5;
double y = 10.5;
double avg = average(x, y);
}
This is also called pass by value.
The Golden Rule: Summary
There are 9 types of variables in Java:
In box-and-pointer notation, each variable is drawn as a labeled box and values are shown in the box.
The golden rule:
Test Your Understanding:
Does the call to changePrice(aqua, p) have an affect on aqua and/or main’s p?
public static void main(String[] args) {
Aquarium aqua = new Aquarium(150, 10.5);
int p = 9;
changePrice(aqua, p);
System.out.println(aqua);
System.out.println(p);
}
public static void changePrice(Aquarium a, int p) {
a.price = a.price - 50;
p = p - 5;
}
Answer: run the code
Instantiation of Arrays
Declaration and Instantiation of Arrays
Arrays are also Objects. As we’ve seen, objects are (usually) instantiated using the new keyword.
int[] a;
new int[]{0, 1, 2, 95, 4};
Declaration
Assignment of Arrays
int[] a = new int[]{0, 1, 2, 95, 4};
Note: Instantiated objects can be lost!
Declaration, instantiation, and assignment.
Declaration
Instantiation
Assignment
Discussion Question
What gets printed?
(assume it is in main)
int[] bikes = new int[5];
System.out.println(bikes);
A) 0 0 0 0 0
B) null null null null null
C) [I@32x34 (memory address)
D) Unknown
E) Compiler error
Primitive Arrays
Memory
Practice worksheet