Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Using Objects
CS 101-E
Aaron Bloomfield
Announcements
Midterm 1 is a week from this Wednesday
TESTS ARE IN CHM 402!!!!!!!!
Schedule:
This week: chapter 3
Next Monday: review for midterm
Homework done via CodeLab
Bring in questions!!!!
Use code VIRGIN-7592-1043
There is a lab due this week
Warn your grandparents!
Historically, this class has been lethal to
grandparents of students in the class
More often grandmothers
This happens most around test time
Although occasionally around the times a big
assignment is due
What is a reference
References are like pointers in C/C++
But they are not the exact same thing!
C++ has references also (in addition to pointers)
A reference is a memory address
References 1
Consider:
int j = 5;
String s = “Hello world”;
Note that there is
no “new” here
Java translates that last line into:
String s = new String (“Hello world”);
References 2
What’s happening in memory
Takes up 32 bits
(4 bytes) of memory
String s
int j
Takes up 32 bits
(4 bytes) of memory
5
Takes up 12
bytes of memory
Hello world
Primitive types are never references; only objects
References 3
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
0x0d4fe1a8
At memory location
0x0d4fe1a8
radius = 1.0
Pi = 3.1415926536
References 4
Consider:
Circle c1 = new Circle();
Circle c2 = new Circle();
c1.radius = 5;
c2 = c1;
c2.radius = 7;
System.out.println (c1.radius);
What happens
to this?
Circle c1
radius = 5.0
7.0
1.0
radius = 1.0
Circle c2
Pi = 3.1415926536
Pi = 3.1415926536
Java’s garbage collection
If an object in memory does not have a
reference pointing to it, Java will automagically
delete the object
This is really cool!
In C/C++, you had to do this by yourself
References and memory
Most modern computers are 32-bit computers
This means that a 32-bit machine cannot access more
than 4 Gb of memory!
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
Giga, Tera, Peta, Exa
That’s 16 billion Gb!
The null reference
Sometimes you want a reference to point to
nothing
Use the null reference:
Circle c = null;
The null reference is equivalent to a memory
address of zero (0x00000000)
No user program can exist there
The null reference
Consider:
Circle c = null;
c.radius = 0.0;
What happens?
What happens in Windows…
The null reference
Consider:
Circle c = null;
c.radius = 0.0;
This is called accessing (or following) a null
pointer/reference
What happens?
Java: java.lang.NullPointerException
C/C++: Segmentation fault (core dumped)
So what is a null reference good
for?
Let’s say you had a method that returned a Circle
when passed some parameters
Normally it returns a valid Circle
But what if it can’t? How to deal with that?
Return a null reference
A bit of humor…
Variable initialization
Recall that Java will NOT initialize a variable in a
method
But it does initialize a field of a class
Consider:
String s;
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
Your current job
Gain experience creating and manipulating objects
from the standard Java types
Why
Prepares you for defining your own classes and
creating and manipulating the objects of those
classes
Values versus objects
Numbers
Objects
Have values but they do not have behaviors
Have attributes (fields) and behaviors (methods)
System.in
References an InputStream
Attribute: keyboard
Behaviors: reading
System.out
References an OutputStream
Attribute: monitor
Behaviors: printing
Other Java object types
String
Rectangle
Color
JFrame
Consider
Statements
int peasPerPod = 8;
String message = "Don't
the door!“
look
How do we represent these
according to the notions of Java?
behind
definitions
A bit of humor…
Representation
peasPerPod
The value of primit ive int
variable peasPerPod is 8
8
message
String
The value of St ring
variable message
is a reference t o a
St ring object
represent ing t he
charact er st ring
"Don't look behind
t he door!"
- text = "Don't look behind the door!"
- length = 27
- ...
+
+
+
+
+
length() : int
charAt(int i) : char
subString(int m, int n) String
indexOf(String s, int m) : int
...
Shorthand representation
peasPerPod
message
8
"Don't look behind the door!"
Examples
Consider
String a = "excellence“;
String b = a;
What is the representation?
a
b
"excellence"
Uninitialized versus null
Consider (in a method):
String dayOfWeek;
Scanner inStream;
What is the representation?
dayOfWeek
-
inStream
-
Uninitialized versus null
Consider (in a class):
String dayOfWeek;
Scanner inStream;
What is the representation?
dayOfWeek
null
inStream
null
Uninitialized versus null
Consider
String fontName = null;
Scanner fileStream = null;
What is the representation?
fontName
null
fileStream
null
Assignment
Consider
String word1 = "luminous";
String word2 = "graceful";
word1 = word2;
Initial representation
word1
"luminous"
word2
"graceful"
Using objects
Consider
Scanner stdin = new Scanner(System.in);
System.out.print("Enter your account name: ");
String response = stdin.nextLine();
Suppose the user interaction is
Enter your account name: artiste
stdin
reponse
Scanner
"artiste"
A bit of humor…
String representation
Consider
String alphabet = "abcdefghijklmnopqrstuvwxyz";
Standard shorthand representation
alphabet
"abcdefghijklmnopqrstuvwxyz"
Truer representation
alphabet
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
y
z
String representation
Consider
String alphabet = "abcdefghijklmnopqrstuvwxyz";
char c1 = alphabet.charAt(9);
char c2 = alphabet.charAt(15);
char c3 = alphabet.charAt(2);
What are the values of c1, c2, and c3? Why?
c1
'j'
c2
'p'
c3
'c'
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 + ".");
}
}
More String methods
Consider
String weddingDate = "August 21, 1976";
String month = weddingDate.substring(0, 6);
System.out.println("Month is " + month + ".");
What is the output?
Month is August.
More String methods
Consider
String
String
int n1
int n2
int n3
fruit = "banana";
searchString = "an";
= fruit.indexOf(searchString, 0);
= fruit.indexOf(searchString, n1 + 1);
= 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?
First search: 1
Second search: 3
Third search: -1
More String methods
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);
s1
"-12"
s2
"3.14"
s3
"a"
Final variables
Consider
final String POEM_TITLE = “Appearance of Brown";
final String WARNING = “Weather ball is black";
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
x
3
y
4
width
5
height
2
The first two parameters of the
Rectangle constructor specify the
position of the upper-left-hand
corner of the new Rectangle
Rectangle(x, y, width, height);
The third and fourth
parameters of the
Rectangle constructor
specify the dimensions of
the new Rectangle
(3, 4)
r
2
Rectangle:
5
Rectangle
Consider
final Rectangle BLOCK = new Rectangle(6, 9, 4, 2);
BLOCK.setLocation(1, 4);
BLOCK.resize(8, 3);
(6, 4)
(1,
9)
BLOCK
2
3
Rectangle:
4
8
Final variables
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…