Download presentation
Presentation is loading. Please wait.
1
Using Objects CS 101-E Aaron Bloomfield
2
Announcements Midterm 1 is a week from this Wednesday Midterm 1 is a week from this Wednesday TESTS ARE IN CHM 402!!!!!!!! TESTS ARE IN CHM 402!!!!!!!! Schedule: Schedule: This week: chapter 3 This week: chapter 3 Next Monday: review for midterm Next Monday: review for midterm Bring in questions!!!! Bring in questions!!!! Homework done via CodeLab Homework done via CodeLab Use code VIRGIN-7592-1043 Use code VIRGIN-7592-1043 There is a lab due this week There is a lab due this week
3
Warn your grandparents! Historically, this class has been lethal to grandparents of students in the class Historically, this class has been lethal to grandparents of students in the class More often grandmothers More often grandmothers This happens most around test time This happens most around test time Although occasionally around the times a big assignment is due Although occasionally around the times a big assignment is due
4
What is a reference References are like pointers in C/C++ References are like pointers in C/C++ But they are not the exact same thing! But they are not the exact same thing! C++ has references also (in addition to pointers) C++ has references also (in addition to pointers) A reference is a memory address A reference is a memory address
5
References 1 Consider: Consider: int j = 5; String s = “Hello world”; Java translates that last line into: Java translates that last line into: String s = new String (“Hello world”); Note that there is no “new” here
6
References 2 What’s happening in memory What’s happening in memory Primitive types are never references; only objects Primitive types are never references; only objects 5 int j Hello world String s Takes up 32 bits (4 bytes) of memory Takes up 32 bits (4 bytes) of memory Takes up 12 bytes of memory
7
References 3 Consider our Circle class Consider our Circle class Circle c = new Circle(); radius = 1.0 Pi = 3.1415926536 Circle c 0x0d4fe1a8 At memory location 0x0d4fe1a8 Declares a reference to a Circle object Creates a new Circle object in memory; Returns a reference to it
8
Circle c1 = new Circle(); Circle c2 = new Circle(); c1.radius = 5; c2 = c1; c2.radius = 7; System.out.println (c1.radius); Consider: Consider: Circle c1 = new Circle(); Circle c2 = new Circle(); c1.radius = 5; c2 = c1; c2.radius = 7; System.out.println (c1.radius); Pi = 3.1415926536 radius = 7.0radius = 1.0radius = 5.0 References 4 Circle c1 Circle c2 radius = 1.0 Pi = 3.1415926536 What happens to this?
9
Java’s garbage collection If an object in memory does not have a reference pointing to it, Java will automagically delete the object If an object in memory does not have a reference pointing to it, Java will automagically delete the object This is really cool! This is really cool! In C/C++, you had to do this by yourself In C/C++, you had to do this by yourself
10
References and memory Most modern computers are 32-bit computers Most modern computers are 32-bit computers This means that a reference takes up 32 bits This means that a reference takes up 32 bits 2 32 = 4 Gb 2 32 = 4 Gb This means that a 32-bit machine cannot access more than 4 Gb of memory! This means that a 32-bit machine cannot access more than 4 Gb of memory! Well, without doing some “tricks”, at least Well, without doing some “tricks”, at least Most machines come with 1 Gb memory these days Most machines come with 1 Gb memory these days Will come with 4 Gb in a year or so Will come with 4 Gb in a year or so 64-bit machines will have 16 exabytes of memory 64-bit machines will have 16 exabytes of memory Giga, Tera, Peta, Exa Giga, Tera, Peta, Exa That’s 16 billion Gb! That’s 16 billion Gb!
11
The null reference Sometimes you want a reference to point to nothing Sometimes you want a reference to point to nothing Use the null reference: Use the null reference: Circle c = null; The null reference is equivalent to a memory address of zero (0x00000000) The null reference is equivalent to a memory address of zero (0x00000000) No user program can exist there No user program can exist there
12
The null reference Consider: Consider: Circle c = null; c.radius = 0.0; What happens? What happens?
13
What happens in Windows…
14
The null reference Consider: Consider: Circle c = null; c.radius = 0.0; This is called accessing (or following) a null pointer/reference This is called accessing (or following) a null pointer/reference What happens? What happens? Java: java.lang.NullPointerException Java: java.lang.NullPointerException C/C++: Segmentation fault (core dumped) C/C++: Segmentation fault (core dumped)
15
So what is a null reference good for? Let’s say you had a method that returned a Circle when passed some parameters Let’s say you had a method that returned a Circle when passed some parameters Normally it returns a valid Circle Normally it returns a valid Circle But what if it can’t? How to deal with that? But what if it can’t? How to deal with that? Return a null reference Return a null reference
16
A bit of humor…
17
Variable initialization Recall that Java will NOT initialize a variable in a method Recall that Java will NOT initialize a variable in a method But it does initialize a field of a class But it does initialize a field of a class Consider: Consider: String s; If s is a variable in a method, it is not initialized 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 If s is a field in a class, it is initialized to null Save yourself the hassle, and always initialize your variables and fields Save yourself the hassle, and always initialize your variables and fields
18
Getting classy Your current job Your current job Gain experience creating and manipulating objects from the standard Java types Gain experience creating and manipulating objects from the standard Java types Why Why Prepares you for defining your own classes and creating and manipulating the objects of those classes Prepares you for defining your own classes and creating and manipulating the objects of those classes
19
Values versus objects Numbers Numbers Have values but they do not have behaviors Have values but they do not have behaviors Objects Objects Have attributes (fields) and behaviors (methods) Have attributes (fields) and behaviors (methods) System.in System.in References an InputStream References an InputStream Attribute: keyboard Attribute: keyboard Behaviors: reading Behaviors: reading System.out System.out References an OutputStream References an OutputStream Attribute: monitor Attribute: monitor Behaviors: printing Behaviors: printing
20
Other Java object types String String Rectangle Rectangle Color Color JFrame JFrame
21
Consider Statements Statements int peasPerPod = 8; String message = "Don't look behind the door!“ How do we represent these definitions according to the notions of Java? How do we represent these definitions according to the notions of Java?
22
A bit of humor…
23
Representation
24
Shorthand representation 8 message peasPerPod "Don't look behind the door!"
25
Examples Consider Consider String a = "excellence“; String b = a; What is the representation? What is the representation?
26
Uninitialized versus null Consider (in a method): Consider (in a method): String dayOfWeek; Scanner inStream; What is the representation? What is the representation?
27
Uninitialized versus null Consider (in a class): Consider (in a class): String dayOfWeek; Scanner inStream; What is the representation? What is the representation? null dayOfWeek null inStream
28
Uninitialized versus null Consider Consider String fontName = null; Scanner fileStream = null; What is the representation? What is the representation?
29
Assignment Consider Consider String word1 = "luminous"; String word2 = "graceful"; word1 = word2; Initial representation Initial representation
30
Using objects Consider Consider Scanner stdin = new Scanner(System.in); System.out.print("Enter your account name: "); String response = stdin.nextLine(); Suppose the user interaction is Suppose the user interaction is Enter your account name: artiste Scannerstdin
31
A bit of humor…
32
String representation Consider Consider String alphabet = "abcdefghijklmnopqrstuvwxyz"; String alphabet = "abcdefghijklmnopqrstuvwxyz"; Standard shorthand representation Standard shorthand representation Truer representation Truer representation
33
String representation Consider Consider String alphabet = "abcdefghijklmnopqrstuvwxyz"; String alphabet = "abcdefghijklmnopqrstuvwxyz"; char c1 = alphabet.charAt(9); char c1 = alphabet.charAt(9); char c2 = alphabet.charAt(15); char c2 = alphabet.charAt(15); char c3 = alphabet.charAt(2); char c3 = alphabet.charAt(2); What are the values of c1, c2, and c3? Why? What are the values of c1, c2, and c3? Why?
34
Program WordLength.java public class WordLength { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); System.out.print("Enter a word: "); String word = stdin.nextLine(); int wordLength = word.length(); System.out.println("Word " + word + " has length " + wordLength + "."); }}
35
More String methods Consider Consider String weddingDate = "August 21, 1976"; String month = weddingDate.substring(0, 6); System.out.println("Month is " + month + "."); What is the output? What is the output? Month is August.
36
More String methods Consider Consider String fruit = "banana"; String searchString = "an"; int n1 = fruit.indexOf(searchString, 0); int n2 = fruit.indexOf(searchString, n1 + 1); int n3 = fruit.indexOf(searchString, n2 + 1); System.out.println("First search: " + n1); System.out.println("Second search: " + n2); System.out.println("Third search: " + n3); What is the output? What is the output? First search: 1 Second search: 3 Third search: -1
37
More String methods Consider Consider int v1 = -12; double v2 = 3.14; char v3 = 'a'; String s1 = String.valueOf(v1); String s2 = String.valueOf(v2); String s3 = String.valueOf(v3);
38
Final variables Consider Consider final String POEM_TITLE = “Appearance of Brown"; final String WARNING = “Weather ball is black"; What is the representation? What is the representation? "Appearance of Brown"POEM_TITLE "Weather ball is black"WARNING The locks indicate the memory location holds constants
39
Final variables object type constant Value In general, these attributes can be modified through member methods The reference cannot be modified once it is established
40
Rectangle The third and fourth parameters of the Rectangle constructor specify the dimensions of the new Rectangle The first two parameters of the Rectangle constructor specify the position of the upper-left-hand corner of the new Rectangle int x = 3; int y = 4; int width = 5; int height = 2; Rectangle r = new Rectangle(x, y, width, height);
41
Consider Consider final Rectangle BLOCK = new Rectangle(6, 9, 4, 2); BLOCK.setLocation(1, 4); BLOCK.resize(8, 3); Consider Consider final Rectangle BLOCK = new Rectangle(6, 9, 4, 2); BLOCK.setLocation(1, 4); BLOCK.resize(8, 3); Rectangle
42
Final variables Consider Consider final String LANGUAGE = "Java"; "Java" The contents are immutable because there are no String methods that allow the contents to be changed The reference cannot be modified once it is established LANGUAGE
43
A bit of humor…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.