Using Objects CS 101 E Aaron Bloomfield Announcements
Using Objects CS 101 -E Aaron Bloomfield
Announcements Midterm 1 is a week from this Wednesday n TESTS ARE IN CHM 402!!!! n Schedule: n This week: chapter 3 n Next Monday: review for midterm n n Bring in questions!!!! n Homework done via Code. Lab n n Use code VIRGIN-7592 -1043 There is a lab due this week
Warn your grandparents! n Historically, this class has been lethal to grandparents of students in the class n n More often grandmothers This happens most around test time n Although occasionally around the times a big assignment is due
What is a reference n References are like pointers in C/C++ But they are not the exact same thing! n C++ has references also (in addition to pointers) n n A reference is a memory address
References 1 n Consider: int j = 5; String s = “Hello world”; n Note that there is no “new” here Java translates that last line into: String s = new String (“Hello world”);
References 2 n What’s happening in memory Takes up 32 bits (4 bytes) of memory int j String s Takes up 32 bits (4 bytes) of memory 5 Takes up 12 bytes of memory n Hello world Primitive types are never references; only objects
References 3 n Consider our Circle class Circle c = new Circle(); Declares a reference Creates a new Circle object in memory; to a Circle object Returns a reference to it Circle c 0 x 0 d 4 fe 1 a 8 At memory location 0 x 0 d 4 fe 1 a 8 radius = 1. 0 Pi = 3. 1415926536
References 4 n Consider: Circle c 1 = new Circle(); Circle c 2 = new Circle(); c 1. radius = 5; c 2 = c 1; c 2. radius = 7; System. out. println (c 1. radius); What happens to this? Circle c 1 radius = 5. 0 7. 0 1. 0 radius = 1. 0 Circle c 2 Pi = 3. 1415926536
Java’s garbage collection n If an object in memory does not have a reference pointing to it, Java will automagically delete the object n This is really cool! n In C/C++, you had to do this by yourself
References and memory n Most modern computers are 32 -bit computers n n n This means that a 32 -bit machine cannot access more than 4 Gb of memory! n n This means that a reference takes up 32 bits 232 = 4 Gb Well, without doing some “tricks”, at least Most machines come with 1 Gb memory these days Will come with 4 Gb in a year or so 64 -bit machines will have 16 exabytes of memory n n Giga, Tera, Peta, Exa That’s 16 billion Gb!
The null reference n Sometimes you want a reference to point to nothing n Use the null reference: Circle c = null; n The null reference is equivalent to a memory address of zero (0 x 0000) n No user program can exist there
The null reference n Consider: Circle c = null; c. radius = 0. 0; n What happens?
What happens in Windows…
The null reference n Consider: Circle c = null; c. radius = 0. 0; n This is called accessing (or following) a null pointer/reference n What happens? Java: java. lang. Null. Pointer. Exception n C/C++: Segmentation fault (core dumped) n
So what is a null reference good for? n Let’s say you had a method that returned a Circle when passed some parameters n Normally it returns a valid Circle n But what if it can’t? How to deal with that? n Return a null reference
A bit of humor…
Variable initialization n Recall that Java will NOT initialize a variable in a method But it does initialize a field of a class n Consider: n String s; n n n If s is a variable in a method, it is not initialized If s is a field in a class, it is initialized to null Save yourself the hassle, and always initialize your variables and fields
Getting classy n Your current job n n Gain experience creating and manipulating objects from the standard Java types Why n Prepares you for defining your own classes and creating and manipulating the objects of those classes
Values versus objects n Numbers n n Objects n n Have values but they do not have behaviors Have attributes (fields) and behaviors (methods) System. in n References an Input. Stream n n n Attribute: keyboard Behaviors: reading System. out n References an Output. Stream n n Attribute: monitor Behaviors: printing
Other Java object types n String n Rectangle n Color n JFrame
Consider n Statements int peas. Per. Pod = 8; String message = "Don't look behind the door!“ n How do we represent these definitions according to the notions of Java?
A bit of humor…
Representation
Shorthand representation peas. Per. Pod message 8 "Don't look behind the door!"
Examples n Consider String a = "excellence“; String b = a; n What is the representation?
Uninitialized versus null n Consider (in a method): String day. Of. Week; Scanner in. Stream; n What is the representation?
Uninitialized versus null n Consider (in a class): String day. Of. Week; Scanner in. Stream; n What is the representation? day. Of. Week null in. Stream null
Uninitialized versus null n Consider String font. Name = null; Scanner file. Stream = null; n What is the representation?
Assignment n Consider String word 1 = "luminous"; String word 2 = "graceful"; word 1 = word 2; n Initial representation
Using objects n Consider Scanner stdin = new Scanner(System. in); System. out. print("Enter your account name: "); String response = stdin. next. Line(); n Suppose the user interaction is Enter your account name: artiste stdin Scanner
A bit of humor…
String representation n Consider n String alphabet = "abcdefghijklmnopqrstuvwxyz"; n Standard shorthand representation n Truer representation
String representation n Consider String alphabet = "abcdefghijklmnopqrstuvwxyz"; n char c 1 = alphabet. char. At(9); n char c 2 = alphabet. char. At(15); n char c 3 = alphabet. char. At(2); n n What are the values of c 1, c 2, and c 3? Why?
Program Word. Length. java public class Word. Length { public static void main(String[] args) { Scanner stdin = new Scanner(System. in); System. out. print("Enter a word: "); String word = stdin. next. Line(); int word. Length = word. length(); System. out. println("Word " + word + " has length " + word. Length + ". "); } }
More String methods n Consider String wedding. Date = "August 21, 1976"; String month = wedding. Date. substring(0, 6); System. out. println("Month is " + month + ". "); n What is the output? Month is August.
More String methods n Consider String int n 1 int n 2 int n 3 fruit = "banana"; search. String = "an"; = fruit. index. Of(search. String, 0); n 1 + 1); n 2 + 1); System. out. println("First search: " + n 1); System. out. println("Second search: " + n 2); System. out. println("Third search: " + n 3); n What is the output? First search: 1 Second search: 3 Third search: -1
More String methods n Consider int v 1 = -12; double v 2 = 3. 14; char v 3 = 'a'; String s 1 = String. value. Of(v 1); String s 2 = String. value. Of(v 2); String s 3 = String. value. Of(v 3);
Final variables n Consider final String POEM_TITLE = “Appearance of Brown"; final String WARNING = “Weather ball is black"; n What is the representation? POEM_TITLE "Appearance of Brown" WARNING "Weather ball is black" The locks indicate the memory location holds constants
Final variables The reference cannot be modified once it is established object type constant In general, these attributes can be modified through member methods Value
Rectangle int x = 3; int y = 4; int width = 5; int height = 2; Rectangle r = new The first two parameters of the Rectangle constructor specify the position of the upper-left-hand corner of the new Rectangle(x, y, width, height); The third and fourth parameters of the Rectangle constructor specify the dimensions of the new Rectangle
Rectangle n Consider final Rectangle BLOCK = new Rectangle(6, 9, 4, 2); BLOCK. set. Location(1, 4); BLOCK. resize(8, 3);
Final variables n Consider final String LANGUAGE = "Java"; The reference cannot be modified once it is established LANGUAGE The contents are immutable because there are no String methods that allow the contents to be changed "Java"
A bit of humor…
- Slides: 43