0% found this document useful (0 votes)
2 views67 pages

(61B SP25) Lecture 3 - Lists 1 - References, Recursion, and Lists

Uploaded by

lin1016344779
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views67 pages

(61B SP25) Lecture 3 - Lists 1 - References, Recursion, and Lists

Uploaded by

lin1016344779
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Lecture 3

Primitive Types, Reference Types, and


Linked Data Structures
CS61B, Spring 2025 @ UC Berkeley
Slides credit: Josh Hug
1
In-class Questions Start Today

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.

Can answer either using links like this: https://www.yellkey.com/garden or using


QR codes. Let’s try. Answer however you’d like.

Note: These in-class questions don’t count for anything.


Goals: Building a List
Primitive Types
Reference Types
Parameter Passing

Goals: Building a Instantiation of Arrays, == vs. equals


IntList and Linked Data Structures
List
Lecture 3, CS61B, Spring 2025
Lists

In lecture 2, we saw how we can create a List in Java.


● Unlike Python, lists are not built directly into the Java language.

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

Walrus a = new Walrus(1000, 8.3); int x = 5;


Walrus b; int y;
b = a; y = x;
b.weight = 5; x = 2;
System.out.println(a); System.out.println("x is: " + x);
System.out.println(b); System.out.println("y is: " + y);
Will the change to b affect a? Will the change to x affect y?
A. Yes A. Yes
B. No B. No
weight: 5, tusk size: 8.30 x is: 2
weight: 5, tusk size: 8.30 y is: 5

Answer: Visualizer
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 61B.
○ Covered in much more detail in 61C.

Note: Precise representations may vary from machine to machine.


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 you access a variable that is uninitialized.

int x;
double y;
x = -1431195969;
y = 567213.112;
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 you access a variable that is uninitialized.

int x;
double y;
x = -1431195969;
y = 567213.112;
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 you access a variable that is uninitialized.

int x;
double y;
x = -1431195969;
y = 567213.112;
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 you access a variable that is uninitialized.

int x;
double y;
x = -1431195969;
y = 567213.112;
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;
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


Goals: Building a List
Primitive Types
Reference Types
Parameter Passing
Instantiation of Arrays, == vs. equals
IntList and Linked Data Structures
Reference Types
Lecture 3, CS61B, Spring 2025
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.


Class Instantiations

When we instantiate an Object (e.g. Dog, Walrus, Planet):


● 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 static class Walrus {


public int weight; new Walrus(1000, 8.3);
public double tuskSize; Demo Link

public Walrus(int w, double ts) {


32 bits
weight = w;
tuskSize = ts; 64 bits
}
}
Class Instantiations

When we instantiate an Object (e.g. Dog, Walrus, Planet):


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

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

When we declare a variable of any reference type (Walrus, Dog, Planet):


● 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).
Walrus someWalrus;
someWalrus = null;
64 bits

Walrus someWalrus;
someWalrus = new Walrus(1000, 8.3);
96 bits
64 bits
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

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!

a and b are 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 and b are 64 bits


Goals: Building a List
Primitive Types
Reference Types
Parameter Passing

Parameter Instantiation of Arrays, == vs. equals


IntList and Linked Data Structures
Passing
Lecture 3, CS61B, Spring 2025
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);
}
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);
}
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);
}
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);
}
The Golden Rule of Equals (and Parameter Passing)

Given variables b and a:


● b = a copies all the bits from a into b. This is also called pass by value.

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: 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.
Test Your Understanding: www.yellkey.com/involve

Does the call to doStuff(walrus, x) have an affect on walrus and/or main’s x?


A. Neither will change.
B. walrus will lose 100 lbs, but main’s x will not change.
C. walrus will not change, but main’s x will decrease by 5.
D. Both will decrease.
public static void main(String[] args) {
Walrus walrus = new Walrus(3500, 10.5);
int x = 9;
doStuff(walrus, x);
System.out.println(walrus);
System.out.println(x);
}
public static void doStuff(Walrus W, int x) {
W.weight = W.weight - 100;
Visualization: x = x - 5;
http://goo.gl/ngsxkq
}
Goals: Building a List
Primitive Types
Reference Types
Instantiation of Parameter Passing

Arrays, == vs. Instantiation of Arrays, == vs. equals


IntList and Linked Data Structures
equals
Lecture 3, CS61B, Spring 2025
Declaration and Instantiation of Arrays, == vs. equals

Arrays are also Objects. As we’ve seen, objects are (usually)


instantiated using the new keyword.
● Planet p = new Planet(0, 0, 0, 0, 0, “blah.png”);
● int[] x = new int[]{0, 1, 2, 95, 4};

int[] a; Declaration

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

Instantiation (HW0 covers this syntax)


new int[]{0, 1, 2, 95, 4};
● Instantiates a new Object, in this case an int array.
● Object is anonymous!
Minor technical note: I’m abusing the term instantiate a little by applying it to arrays.
Assignment of Arrays
Declaration, instantiation,
int[] a = new int[]{0, 1, 2, 95, 4}; and assignment.
● 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!
Assignment Instantiation
Declaration
Arrays.equals vs. ==

In Java, the == operator literally compares the bits in the two memory boxes.

For example, the code below would, perhaps surprisingly, print false.

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


int[] y = new int[]{0, 1, 2, 95, 4};
System.out.println(x == y); #false
Arrays.equals vs. ==

In Java, the == operator literally compares the bits in the two memory boxes.

By contrast, the code below would print true.

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


int[] y = new int[]{0, 1, 2, 95, 4};
int[] z = y;
System.out.println(z == y); #true

To compare the content of two arrays, we can use Arrays.equals.


Arrays.equals(x, y); #true
Goals: Building a List
Primitive Types
Reference Types
Parameter Passing

IntList and Linked Instantiation of Arrays, == vs. equals


IntList and Linked Data Structures
Data Structures
Lecture 3, CS61B, Spring 2025
IntList

Let’s define an IntList as an object containing two member variables:


● int first;
● IntList rest;

And define two versions of the same method:


● size()
● iterativeSize()
Coding Demo: Adding to End of IntList
IntList.java

public class IntList {


public int first;
public IntList rest;

}
Coding Demo: Adding to End of IntList
IntList.java

public class IntList {


public int first;
public IntList rest;

public static void main(String[] args) {


IntList L = new IntList();
L.first = 5;
L.rest = null;

}
}
Coding Demo: Adding to End of IntList
IntList.java

public class IntList {


public int first;
public IntList rest;

public static void main(String[] args) {


IntList L = new IntList();
L.first = 5;
L.rest = null;

L.rest = new IntList();


L.rest.first = 10;

}
}
Coding Demo: Adding to End of IntList
IntList.java

public class IntList {


public int first;
public IntList rest;

public static void main(String[] args) {


IntList L = new IntList();
L.first = 5;
L.rest = null;

L.rest = new IntList();


L.rest.first = 10;

L.rest.rest = new IntList();


L.rest.rest.first = 15;

}
} Java Visualizer
Coding Demo: Adding to Start of IntList
IntList.java

public class IntList {


public int first;
public IntList rest;

}
Coding Demo: Adding to Start of IntList
IntList.java

public class IntList {


public int first;
public IntList rest;

public IntList(int f, IntList r) {


first = f;
rest = r;
}

}
Coding Demo: Adding to Start of IntList
IntList.java

public class IntList {


public int first;
public IntList rest;

public IntList(int f, IntList r) {


first = f;
rest = r;
}

public static void main(String[] args) {


IntList L = new IntList(15, null);
L = new IntList(10, L);
L = new IntList(5, L);

}
} Java Visualizer
Coding Demo: IntList size
IntList.java

public class IntList {


public int first;
public IntList rest;

public IntList(int f, IntList r) {


first = f;
rest = r;
}

public static void main(String[] args) {


IntList L = new IntList(15, null);
L = new IntList(10, L);
L = new IntList(5, L);

System.out.println(L.size()); // should print out 3


}
}
Coding Demo: IntList size
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the size of the list using... recursion! */


public int size() {

}
Coding Demo: IntList size
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the size of the list using... recursion! */


public int size() {
if (rest == null) {

}
Coding Demo: IntList size
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the size of the list using... recursion! */


public int size() {
if (rest == null) {
return 1;
}

}
Coding Demo: IntList size
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the size of the list using... recursion! */


public int size() {
if (rest == null) {
return 1;
}
return 1 + this.rest.size();
}

} Java Visualizer
Coding Demo: IntList iterativeSize
IntList.java

public class IntList {


public int first;
public IntList rest;

public IntList(int f, IntList r) {


first = f;
rest = r;
}

public static void main(String[] args) {


IntList L = new IntList(15, null);
L = new IntList(10, L);
L = new IntList(5, L);

System.out.println(L.iterativeSize()); // should also print out 3


}
}
Coding Demo: IntList iterativeSize
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the size of the list using no recursion! */


public int iterativeSize() {

}
Coding Demo: IntList iterativeSize
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the size of the list using no recursion! */


public int iterativeSize() {
IntList p = this;

}
Coding Demo: IntList iterativeSize
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the size of the list using no recursion! */


public int iterativeSize() {
IntList p = this;
int totalSize = 0;

}
Coding Demo: IntList iterativeSize
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the size of the list using no recursion! */


public int iterativeSize() {
IntList p = this;
int totalSize = 0;
while (p != null) {

}
Coding Demo: IntList iterativeSize
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the size of the list using no recursion! */


public int iterativeSize() {
IntList p = this;
int totalSize = 0;
while (p != null) {
totalSize += 1;

}
Coding Demo: IntList iterativeSize
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the size of the list using no recursion! */


public int iterativeSize() {
IntList p = this;
int totalSize = 0;
while (p != null) {
totalSize += 1;
p = p.rest;
}

}
Coding Demo: IntList iterativeSize
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the size of the list using no recursion! */


public int iterativeSize() {
IntList p = this;
int totalSize = 0;
while (p != null) {
totalSize += 1;
p = p.rest;
}
return totalSize;
}

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

● For simplicity, OK to assume the


item exists. public class IntList {
● Front item is the 0th item. public int first;
public IntList rest;
public IntList(int f, IntList r) {
Ways to work: first = f;
● Paper (best) rest = r;
}
● Laptop (see lectureCode repo)
○ exercises/lists1/IntList.java /** Return the size of this IntList. */
● In your head (worst) public int size() {
if (rest == null) {
return 1;
}
return 1 + this.rest.size();
L.get(0): 5
L.get(1): 10 ...
Coding Demo: IntList get
IntList.java

public class IntList {


public int first;
public IntList rest;

public IntList(int f, IntList r) {


first = f;
rest = r;
}

public static void main(String[] args) {


IntList L = new IntList(15, null);
L = new IntList(10, L);
L = new IntList(5, L);

System.out.println(L.get(1)); // should print out 10


}
}
Coding Demo: IntList get
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the ith item of this IntList. */


public int get(int i) {

}
Coding Demo: IntList get
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the ith item of this IntList. */


public int get(int i) {
if (i == 0) {

}
Coding Demo: IntList get
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the ith item of this IntList. */


public int get(int i) {
if (i == 0) {
return first;
}

}
Coding Demo: IntList get
IntList.java

public class IntList {


public int first;
public IntList rest;

/** Return the ith item of this IntList. */


public int get(int i) {
if (i == 0) {
return first;
}
return rest.get(i - 1);
}

Java Visualizer
}
Question: yellkey.com

What is your comfort level with recursive data structure code?


A. Very comfortable.
B. Comfortable.
C. Somewhat comfortable.
D. I have never done this.
ExtraIntListPractice.java

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

You might also like