{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This preview shows pages 1–7. 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/
Image of page 1

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

View Full Document Right 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; }
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
Image of page 3

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

View Full Document Right 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 not copied; 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 value 6 5 4 3 2 1 0 index 7 a1 a2
Image of page 4
5 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); DrawingPanel panel2 = panel1; // same window panel2.setBackground(Color.CYAN); panel1 panel2
Image of page 5

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

View Full Document Right Arrow Icon
6 References as fields • Objects can store references to other objects as fields.
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern