0% found this document useful (0 votes)
19 views15 pages

Discussion 2 - Scope, Pass-by-Value, Static

Uploaded by

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

Discussion 2 - Scope, Pass-by-Value, Static

Uploaded by

DAR RYL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Discussion 2: Scope, Pass-by-

Value, Static
Christine Zhou
Agenda
- Announcements
- Worksheet #1
- Worksheet #2
- Worksheet #3 (if we can!)
Administrivia
● Lab 1 and Lab 2 due this Friday 2/1 at midnight.
○ Note, all labs are due Friday at midnight the week they are
assigned.
● Proj0 NBody also due Friday at midnight.
○ If you haven’t started please start ASAP!
● Office hours and exam prep/LOST sections start this week! See
calendars on the course website.
● Pre-semester Advising: @414 or email me!
● Discussion survey: http://tinyurl.com/cz-disc2-sp19
○ Good for me to collect your emails (in case I have announcements or mess up
during discussion 😅)
Review: Defining classes
// class definition
public class IntList {

// instance variables
int head;
IntList tail;

// constructors, which will create IntList objects


public IntList() {...}
public IntList(int value, IntList tail) {...}

// you can create methods that use the instance variables!


public void insert(int val) {...}
public void removeLast() {...}
}
Review: Java’s Types

Primitives: References:
● byte, short, long, int, double, ● Everything else!
float, char, boolean ● Objects, arrays, Strings, etc.
Review: Box and Pointer Diagrams
● Box and Pointers are the 61B equivalent of 61A’s environment
diagram
● They are crucial to helping us keep track of what goes on in a
program: use them! We didn’t specify what
int x = 29; int[] arr = new int[3]; integers are going in the
array, so Java initializes
it with the default value
int x int[ ] arr 0

29 0 0 0

Primitives go in the box Reference types are referenced via pointers


Java is pass by value
Golden Rule of Equals AKA pass-by-value
● When you do “a = b”, you always copy the bits (whatever is inside
the box) of b into a.
● For primitives: Directly copy the value inside b to a.
● For Reference types: Copy the memory address of b into a.
○ Also can say “copies the pointer in b to a”
int[ ] arr1
int x = 29; int[] arr1 = new int[3];
int x int y 0 0 0
int y = x; int[] arr2 = arr1;
29 29 int[ ] arr2
1)Pass by What?

Java Visualizer
Review: Static Variables/Methods
public class Human {
● Static == belong to the class public String name;
public int age;
● Shared by all instances of the class public static int population;

● Doesn’t need to be called from any public void happyBirthday() {...}


public static void getPopulation() {...}
particular instance and doesn’t need
public static void main(String[] args) {
access instance variables. Human christine = new Human();
● Should be accessed via dot notation }
}

with the class name, not instance


(though instance does work, just bad
style)
○ Human.population is better than
christine.population
2) Static Methods and Variables

Java Visualizer
Review: IntLists
● IntLists are the equivalent of 61A’s “Linked List” with one
restriction: it can only hold integers.
● Each IntList consists of 2 elements:
○ an integer first
○ an IntList rest

IntList l first rest

1 2 3
3) Practice with Linked Lists
The golden rule of equals applies here!
Golden Rule of Equals AKA pass-by-value
● When you do “a = b”, you copy the bits (whatever is inside the box) of b into a.
● For primitives: Directly copy the value inside b to a.
● For Reference types: Copy the memory address of b into a. “Copies the
pointer in b to a”

Java Visualizer
Components of recursion:
4) Extra: Squaring a List 1. Base case
2. Recursive call
3. Composition

public static IntList square(IntList L) {

public static IntList squareDestructive(IntList L) {


Components of recursion:
4) Extra: Squaring a List 1. Base case
2. Recursive call
3. Composition
Components of recursion:
4) Extra: Squaring a List 1. Base case
2. Recursive call
3. Composition

You might also like