1 of 45

Lecture 8

Box and pointer notation

Many slides were borrowed from Josh Hug

2 of 45

Reminders

  • Mic
  • PA02 feedback should be on canvas.

3 of 45

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

4 of 45

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

5 of 45

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

6 of 45

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

7 of 45

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

8 of 45

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

9 of 45

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

10 of 45

Bits

Your computer stores information in “memory”.

  • Information is stored in memory as a sequence of ones and zeros.
    • Example: 72 stored as 01001000
    • Example: 205.75 stored as … 01000011 01001101 11000000 00000000
    • Example: The letter H stored as 01001000 (same as the number 72)
    • Example: True stored as 00000001

Each Java type has a different way to interpret the bits:

  • 8 primitive types in Java: byte, short, int, long, float, double, boolean, char
  • We won’t discuss the precise representations in much detail in this class.

Note: Precise representations may vary from machine to machine.

11 of 45

Declaring a Variable (Simplified)

When you declare a variable of a certain type in Java:

  • Your computer sets aside exactly enough bits to hold a thing of that type.
    • Example: Declaring an int sets aside a “box” of 32 bits.
    • Example: Declaring a double sets aside a box of 64 bits.
  • Java creates an internal table that maps each variable name to a location.
  • Java does NOT write anything into the reserved boxes.
    • For safety, Java will not let access a variable that is uninitialized.

int x;

double y;

x = -1431195969;

y = 567213.112;

12 of 45

Declaring a Variable (Simplified)

When you declare a variable of a certain type in Java:

  • Your computer sets aside exactly enough bits to hold a thing of that type.
    • Example: Declaring an int sets aside a “box” of 32 bits.
    • Example: Declaring a double sets aside a box of 64 bits.
  • Java creates an internal table that maps each variable name to a location.
  • Java does NOT write anything into the reserved boxes.
    • For safety, Java will not let access a variable that is uninitialized.

int x;

double y;

x = -1431195969;

y = 567213.112;

13 of 45

Declaring a Variable (Simplified)

When you declare a variable of a certain type in Java:

  • Your computer sets aside exactly enough bits to hold a thing of that type.
    • Example: Declaring an int sets aside a “box” of 32 bits.
    • Example: Declaring a double sets aside a box of 64 bits.
  • Java creates an internal table that maps each variable name to a location.
  • Java does NOT write anything into the reserved boxes.
    • For safety, Java will not let access a variable that is uninitialized.

int x;

double y;

x = -1431195969;

y = 567213.112;

14 of 45

Declaring a Variable (Simplified)

When you declare a variable of a certain type in Java:

  • Your computer sets aside exactly enough bits to hold a thing of that type.
    • Example: Declaring an int sets aside a “box” of 32 bits.
    • Example: Declaring a double sets aside a box of 64 bits.
  • Java creates an internal table that maps each variable name to a location.
  • Java does NOT write anything into the reserved boxes.
    • For safety, Java will not let access a variable that is uninitialized.

int x;

double y;

x = -1431195969;

y = 567213.112;

15 of 45

Simplified Box Notation

We’ll use simplified box notation from here on out:

  • Instead of writing memory box contents in binary, we’ll write them in human readable symbols.

int x;

double y;

x = -1431195969;

y = 567213.112;

16 of 45

The Golden Rule of Equals (GRoE)

Given variables y and x:

  • y = x copies all the bits from x into y.

�Example from earlier: Link

17 of 45

References

18 of 45

Reference Types

There are 8 primitive types in Java:

  • byte, short, int, long, float, double, boolean, char

Everything else, including arrays, is a reference type.

19 of 45

Class Instantiations

When we instantiate an Object (e.g. Car, Aquarium, Queue):

  • Java first allocates a box of bits for each instance variable of the class and fills them with a default value (e.g. 0, null).
  • The constructor then usually fills every such box with some other value.

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

20 of 45

Class Instantiations

When we instantiate an Object (e.g. Dog, Aquarium, Queue):

  • Java first allocates a box of bits for each instance variable of the class and fills them with a default value (e.g. 0, null).
  • The constructor then usually fills every such box with some other value.

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.)

21 of 45

Class Instantiations

Can think of new as returning the address of the newly created object.

  • Addresses in Java are 64 bits.
  • Example (rough picture): If object is created in memory location 2384723423, then new returns 2384723423.

32 bits

64 bits

new Aquarium(50, 9.5);

2384723423

....00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111101000010000000010000010011001100110011001100110011001100110011001101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000....

2384723423th bit

22 of 45

Reference Type Variable Declarations

When we declare a variable of any reference type (Aquarium, Dog, Queue):

  • Java allocates exactly a box of size 64 bits, no matter what type of object.
  • These bits can be either set to:
    • Null (all zeros).
    • The 64 bit “address” of a specific instance of that class (returned by new).

Aquarium someAqua;

someAqua = null;

64 bits

Aquarium someAqua;

someAqua = new Aquarium(50, 9.5);

96 bits

64 bits

someAqua

someAqua

23 of 45

Reference Type Variable Declarations

The 64 bit addresses are meaningless to us as humans, so we’ll represent:

  • All zero addresses with “null”.
  • Non-zero addresses as arrows.

This is sometimes called “box and pointer” notation.

64 bits

someAqua

64 bits

96 bits

24 of 45

Reference Types Obey the Golden Rule of Equals

Just as with primitive types, the equals sign copies the bits.

  • In terms of our visual metaphor, we “copy” the arrow by making the arrow in the b box point at the same instance as a.

Aquarium a;

a = new Aquarium(100, 9.5);

Aquarium b;

b = a;

a is 64 bits

25 of 45

Reference Types Obey the Golden Rule of Equals

Just as with primitive types, the equals sign copies the bits.

  • In terms of our visual metaphor, we “copy” the arrow by making the arrow in the b box point at the same instance as a.

Aquarium a;

a = new Aquarium(100, 9.5);

Aquarium b;

b = a;

a is 64 bits

The Aquarium shown is 96 bits.

26 of 45

Reference Types Obey the Golden Rule of Equals

Just as with primitive types, the equals sign copies the bits.

  • In terms of our visual metaphor, we “copy” the arrow by making the arrow in the b box point at the same instance as a.

Aquarium a;

a = new Aquarium(100, 9.5);

Aquarium b;

b = a;

a is 64 bits

The Aquarium shown is 96 bits.

27 of 45

Reference Types Obey the Golden Rule of Equals

Just as with primitive types, the equals sign copies the bits.

  • In terms of our visual metaphor, we “copy” the arrow by making the arrow in the b box point at the same instance as a.

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.

28 of 45

Reference Types Obey the Golden Rule of Equals

Just as with primitive types, the equals sign copies the bits.

  • In terms of our visual metaphor, we “copy” the arrow by making the arrow in the b box point at the same instance as a.

Aquarium a;

a = new Aquarium(100, 9.5);

Aquarium b;

b = a;

a and b are 64 bits

The Aquarium shown is 96 bits.

29 of 45

Parameter Passing

30 of 45

The Golden Rule of Equals (and Parameter Passing)

Given variables b and a:

  • b = a copies all the bits from a into b.

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

}

31 of 45

The Golden Rule of Equals (and Parameter Passing)

Given variables b and a:

  • b = a copies all the bits from a into b.

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

}

32 of 45

The Golden Rule of Equals (and Parameter Passing)

Given variables b and a:

  • b = a copies all the bits from a into b.

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

}

33 of 45

The Golden Rule of Equals (and Parameter Passing)

Given variables b and a:

  • b = a copies all the bits from a into b.

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.

34 of 45

The Golden Rule: Summary

There are 9 types of variables in Java:

  • 8 primitive types (byte, short, int, long, float, double, boolean, char).
  • The 9th type is references to Objects (an arrow). References may be null.

In box-and-pointer notation, each variable is drawn as a labeled box and values are shown in the box.

  • Addresses are represented by arrows to object instances.

The golden rule:

  • b = a copies the bits from a into b.
  • Passing parameters copies the bits.

35 of 45

Test Your Understanding:

Does the call to changePrice(aqua, p) have an affect on aqua and/or main’s p?

  1. Neither will change.
  2. aqua will be cheaper by 50$, but main’s p will not change.
  3. aqua will not change, but main’s p will decrease by 5.
  4. Both will decrease.

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

36 of 45

Instantiation of Arrays

37 of 45

Declaration and Instantiation of Arrays

Arrays are also Objects. As we’ve seen, objects are (usually) instantiated using the new keyword.

  • Animal p = new Animal(0, 0, 0, 0, 0, “blah.png”);
  • int[] x = new int[]{0, 1, 2, 95, 4};

int[] a;

  • Declaration creates a 64 bit box intended only for storing a reference to an int array. No object is instantiated.

new int[]{0, 1, 2, 95, 4};

  • Instantiates a new Object, in this case an int array.

Declaration

38 of 45

Assignment of Arrays

int[] a = new int[]{0, 1, 2, 95, 4};

  • Creates a 64 bit box for storing an int array address. (declaration)
  • Creates a new Object, in this case an int array. (instantiation)
  • Puts the address of this new Object into the 64 bit box named a. (assignment)

Note: Instantiated objects can be lost!

  • If we were to reassign a to something else, we’d never be able to get the original Object back!

Declaration, instantiation, and assignment.

Declaration

Instantiation

Assignment

39 of 45

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

40 of 45

Primitive Arrays

41 of 45

Memory

  • Primitive type variables are not initialized to 0
  • Compiler error if program uses variable before initialized
  • Primitive arrays are initialized to 0
    • What are char[ ], int[ ], float[ ], double[ ] initialized to?

42 of 45

43 of 45

44 of 45

45 of 45

Practice worksheet