Download presentation
Presentation is loading. Please wait.
1
COMP 110 Objects and References Tabitha Peck M.S. February 27, 2008 MWF 3-3:50 pm Philips 367 1
2
Announcements Lab 5 due Friday Program 3 due Tuesday at midnight Recitation - work on Program 3 Midterm - next Friday 2
3
Questions? 3
4
Today in COMP 110 Strings, Classes, and Memory Property.java updated on web Objects and References 4
5
Strings “Strings are constant; their values cannot be changed after they are created.” Java.sun.com s1 = s2; s1 = “how are you?” System.out.print(s2); Creates new variable in memory named s1. You can no longer access initial s1. Can’t change s2 because strings are constant. Nothing cool going on here. 5
6
Strings Declaring and initializing strings String s1 = “a string”; String s2 = new String(“a string”); How to set two strings equal (in value) String s3 = new String(s2); 6
7
Objects in Memory 5 3.14159 2078 1056 Name: Tom Age: 20 int num float num2 Person p1 Person p2 Name: Jane Age: 23 p2 = p1; 2078 p1.setName(“Joe”); Name: Joe Age: 23 p2.print(); p1.print(); 7
8
Implementation: Private instance variables Private constants Private Methods Bodies of all methods Method definitions Well Encapsulated Interface: Comments Headings of public methods Public defined constants Programmer 8
9
Guidelines Comments before class definition (this is your header) Instance variables are private Provide public accessor and mutator methods Pre and post comments before methods Make helping methods private /**/ for user-interface comments and // for implementation comments 9
10
Calling methods int answer = math.add(4, 6); answer = math.add(answer, 7); public int add(int a, int b) { return (a + b); } 10
11
Global vs. Local Variables are local to methods Instance variables are global for all methods in a class 11
12
int i = 5; if (i > 2) { int j = 3; System.out.println(j); } System.out.println(j); cannot find symbol symbol : variable j 12
13
Methods with objects as arguments How is an object stored in memory? What is passed as the argument? What happens if you modify an object in your method? 13
14
Friday Program 3 14
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.