Download presentation
Presentation is loading. Please wait.
Published byΒηθζαθά Παπαφιλίππου Modified over 5 years ago
1
CIS 110: Introduction to Computer Programming
Lecture 18 Reference semantics (§ ) 7/17/2019 CIS 110 (11fa) - University of Pennsylvania
2
CIS 110 (11fa) - University of Pennsylvania
Outline Reference semantics Array traversals 7/17/2019 CIS 110 (11fa) - University of Pennsylvania
3
CIS 110 (11fa) - University of Pennsylvania
Reference Semantics 7/17/2019 CIS 110 (11fa) - University of Pennsylvania
4
CIS 110 (11fa) - University of Pennsylvania
Review: pass by copy public static void change(int x) { x = 5; } public static void main(String[] args) { int x = 0; change(x); System.out.println(x); // Prints 0 x The x variables are distinct. We pass a copy of the contents of x to change. Primitive variables contain their values directly. 7/17/2019 CIS 110 (11fa) - University of Pennsylvania
5
The twist: arrays behave differently!
public static void change(int[] x) { x[0] = 5; } public static void main(String[] args) { int x[] = { 0 }; change(x); System.out.println(x[0]); // Prints 5 x Array variables (generally object variables) contain references to the arrays rather than the arrays themselves. 7/17/2019 CIS 110 (11fa) - University of Pennsylvania
6
Passing array parameters (1)
public static void change(int[] x) { x[0] = 5; } public static void main(String[] args) { int x[] = { 0 }; change(x); System.out.println(x[0]); // Prints 5 x 7/17/2019 CIS 110 (11fa) - University of Pennsylvania
7
Passing array parameters (2)
public static void change(int[] x) { x[0] = 5; } public static void main(String[] args) { int x[] = { 0 }; change(x); System.out.println(x[0]); // Prints 5 x x 5 7/17/2019 CIS 110 (11fa) - University of Pennsylvania
8
Passing array parameters (3)
public static void change(int[] x) { x[0] = 5; } public static void main(String[] args) { int x[] = { 0 }; change(x); System.out.println(x[0]); // Prints 5 x 5 7/17/2019 CIS 110 (11fa) - University of Pennsylvania
9
Pass by value vs. pass by reference
public static void change(int[] x) { x[0] = 5; } public static void main(String[] args) { int x[] = { 0 }; change(x); System.out.println(x[0]); // Prints 5 x 5 7/17/2019 CIS 110 (11fa) - University of Pennsylvania
10
Pass by value vs. pass by reference
For primitive types we pass copies of the contents of the variables. For reference types, we pass references to the objects the variables refer to. public static void change(int x) { x = 5; } x 5 x 5 x 5 public static void change(int[] x) { x[0] = 5; } x 7/17/2019 CIS 110 (11fa) - University of Pennsylvania
11
Alternative view: we copy references
Alternatively, we always copy the contents of variables along. But we copy references of variables of object type. Either viewpoint is valid --- pick the one that makes the most sense! public static void change(int x) { x = 5; } x 5 x 5 x 5 public static void change(int[] x) { x[0] = 5; } x 7/17/2019 CIS 110 (11fa) - University of Pennsylvania
12
Alternative view: we copy references
Alternatively, we always copy the contents of variables along. But we copy references of variables of object type. public static void change(int x) { x = 5; } x 5 public static void change(int[] x) { x[0] = 5; } x 5 7/17/2019 CIS 110 (11fa) - University of Pennsylvania
13
CIS 110 (11fa) - University of Pennsylvania
Array Traversals See Traversals.java, ExtractDigit.java 7/17/2019 CIS 110 (11fa) - University of Pennsylvania
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.