(61B SP25) Lecture 3 - Lists 1 - References, Recursion, and Lists
(61B SP25) Lecture 3 - Lists 1 - References, Recursion, and Lists
We’ll be doing some in-class questions today. This is for two reasons:
● More fun, engaging, and useful to actually do something during lecture.
● Gives me feedback on how well students understand content.
import java.util.List;
import java.util.LinkedList;
List<String> L = new LinkedList<>();
L.add("a");
L.add("b");
Today, we’ll begin our 3 lecture journey towards building our own list
implementation.
● We’ll exploit recursion to allow our list to grow infinitely large.
● But first we need to solve… the mystery of the walrus.
Goals: Building a List
Primitive Types
Reference Types
Parameter Passing
Instantiation of Arrays, == vs. equals
IntList and Linked Data Structures
Primitive Types
Lecture 3, CS61B, Spring 2025
Quiz, www.yellkey.com/true and https://www.yellkey.com/exactly
Answer: Visualizer
Bits
int x;
double y;
x = -1431195969;
y = 567213.112;
Declaring a Variable (Simplified)
int x;
double y;
x = -1431195969;
y = 567213.112;
Declaring a Variable (Simplified)
int x;
double y;
x = -1431195969;
y = 567213.112;
Declaring a Variable (Simplified)
int x;
double y;
x = -1431195969;
y = 567213.112;
Simplified Box Notation
int x;
double y;
x = -1431195969;
y = 567213.112;
The Golden Rule of Equals (GRoE)
....00000000000000000000000000000000000000000
000000000000000000000000000000000000000000000 new Walrus(1000, 8.3);
000000000000000000000000000000000000000000000
000000000000000000000000000000000000001111101
000010000000010000010011001100110011001100110
011001100110011001101000000000000000000000000
000000000000000000000000000000000000000000000 32 bits
000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000 64 bits
000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000 Green is weight, blue is tuskSize.
000000000000000000000000000000000000000000000
000000000000000000000000.... (In reality, total Walrus size is slightly larger than 96 bits.)
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.
2384723423th bit
....00000000000000000000000000000000000000000 2384723423
000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000
000000000000000000000000000000000000001111101 new Walrus(1000, 8.3);
000010000000010000010011001100110011001100110
011001100110011001101000000000000000000000000
000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000 32 bits
000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000 64 bits
000000000000000000000000000000000000000000000
000000000000000000000000....
Reference Type Variable Declarations
Walrus someWalrus;
someWalrus = new Walrus(1000, 8.3);
96 bits
64 bits
Reference Type Variable Declarations
64 bits
64 bits
96 bits
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.
Walrus a;
a = new Walrus(1000, 8.3);
Walrus 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.
● 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.
Walrus a;
a = new Walrus(1000, 8.3);
Walrus b;
b = a;
The Walrus shown is 96 bits.
a is 64 bits
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.
Walrus a;
a = new Walrus(1000, 8.3);
Walrus b;
b = a;
The Walrus shown is 96 bits.
Note: b is currently
undefined, not null!
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.
Walrus a;
a = new Walrus(1000, 8.3);
Walrus b;
b = a;
The Walrus shown is 96 bits.
Passing parameters obeys the same rule: Simply copy the bits to the new scope.
Passing parameters obeys the same rule: Simply copy the bits to the new scope.
Passing parameters obeys the same rule: Simply copy the bits to the new scope.
Passing parameters obeys the same rule: Simply copy the bits to the new scope.
Passing parameters obeys the same rule: Simply copy the bits to the new scope.
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.
int[] a; Declaration
● Declaration creates a 64 bit box intended only for storing a reference to an int
array. No object is instantiated.
In Java, the == operator literally compares the bits in the two memory boxes.
For example, the code below would, perhaps surprisingly, print false.
In Java, the == operator literally compares the bits in the two memory boxes.
}
Coding Demo: Adding to End of IntList
IntList.java
}
}
Coding Demo: Adding to End of IntList
IntList.java
}
}
Coding Demo: Adding to End of IntList
IntList.java
}
} Java Visualizer
Coding Demo: Adding to Start of IntList
IntList.java
}
Coding Demo: Adding to Start of IntList
IntList.java
}
Coding Demo: Adding to Start of IntList
IntList.java
}
} Java Visualizer
Coding Demo: IntList size
IntList.java
}
Coding Demo: IntList size
IntList.java
}
Coding Demo: IntList size
IntList.java
}
Coding Demo: IntList size
IntList.java
} Java Visualizer
Coding Demo: IntList iterativeSize
IntList.java
}
Coding Demo: IntList iterativeSize
IntList.java
}
Coding Demo: IntList iterativeSize
IntList.java
}
Coding Demo: IntList iterativeSize
IntList.java
}
Coding Demo: IntList iterativeSize
IntList.java
}
Coding Demo: IntList iterativeSize
IntList.java
}
Coding Demo: IntList iterativeSize
IntList.java
Java Visualizer
}
Challenge
Write a method int get(int i) that See the video online for a solution:
returns the ith item in the list. https://www.youtube.com/watch?v=qnmxD_21DNk
}
Coding Demo: IntList get
IntList.java
}
Coding Demo: IntList get
IntList.java
}
Coding Demo: IntList get
IntList.java
Java Visualizer
}
Question: yellkey.com
For further practice with IntLists, fill out the code for the methods listed below in
the lists1/exercises/ExtraIntListPractice.java in lectureCode github directory.
● public static IntList incrList(IntList L, int x)
○ Returns an IntList identical to L, but with all values incremented by x.
○ Values in L cannot change!
L
Q
● public static IntList dincrList(IntList L, int x)
○ Returns an IntList identical to L, but with all values incremented by x.
○ Not allowed to use ‘new’ (to save memory).3
L
Q