Scope, Pass by Value, Static (Discussion 2) (Phạm vi, truyền tham trị và từ khóa static trong Java) - Christine Zhou

UN
Uni24h
0 lượt tải
15
0

Nội dung tài liệu

Trang 3

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

Trang 4

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() {...} }

Trang 5

Review: Java’s Types Primitives: References: ● byte, short, long, int, double, ● Everything else! float, char, boolean ● Objects, arrays, Strings, etc.

Trang 6

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 0 int x int[ ] arr 29 0 0 0 Primitives go in the box Reference types are referenced via pointers

Trang 7

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

Trang 9

Review: Static Variables/Methods public class Human { ● Static == belong to the class public String name; ● Shared by all instances of the class public int age; public static int population; ● Doesn’t need to be called from any public void happyBirthday() {...} particular instance and doesn’t need public static void getPopulation() {...} access instance variables. public static void main(String[] args) { 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

Trang 11

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

Trang 12

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

Trang 13

4) Extra: Squaring a List Components of recursion: 1. Base case 2. Recursive call 3. Composition public static IntList square(IntList L) { public static IntList squareDestructive(IntList L) {

Hiển thị 13 trang đầu. Tải tài liệu để xem đầy đủ.

Bình luận (0)

Đăng nhập để tham gia bình luận

Chưa có bình luận nào. Hãy là người đầu tiên!