09-linked_nodes - CSE 143 Lecture 9 References and Linked...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 143 Lecture 9 References and Linked Nodes reading: 16.1; 3.3 slides created by Marty Stepp http://www.cs.washington.edu/143/
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 A swap method? • Does the following swap method work? Why or why not? public static void main(String[] args) { int a = 7; int b = 35; // swap a with b swap(a, b); System.out.println(a + " " + b); } public static void swap(int a, int b) { int temp = a; a = b; b = temp; }
Background image of page 2
3 Value semantics value semantics : Behavior where values are copied when assigned to each other or passed as parameters. – When one primitive is assigned to another, its value is copied. – Modifying the value of one variable does not affect others. int x = 5; int y = x ; // x = 5, y = 5 y = 17; // x = 5, y = 17 x = 8; // x = 8, y = 17
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Reference semantics reference semantics : Behavior where variables actually store the address of an object in memory. – When one reference variable is assigned to another, the object is notcopied; both variables refer to the same object. int[] a1 = {4, 5, 2, 12, 14, 14, 9}; int[] a2 = a1 ; // refers to same array as a1 a2[0] = 7; System.out.println( a1[0] ); // 7 9 14 14 12 2 5 4 value 6 5 4 3 2 1 0 index 7 a1 a2
Background image of page 4
References and objects • In Java, objects and arrays use reference semantics. Why? – efficiency. Copying large objects slows down a program. – sharing. It's useful to share an object's data among methods. DrawingPanel panel1 = new DrawingPanel(80, 50);
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/04/2011 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

Page1 / 20

09-linked_nodes - CSE 143 Lecture 9 References and Linked...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online