object program 147
object program 147
5 6
1
Java Programs: Applets Object Basics
■ When a Java Applet runs ■ Primitive types in Java:
● byte, short, int, long ■ Operators (with one
● The web page specifies a class
● float, double exception) work only on
● The “system” looks for these methods primitive types
● char
▲ public void init() ● What’s the exception?
● boolean
▼ Runs
when Applet is first loaded
▲ public void start() ■ Everything else is an ■ Each Java variable holds
one of two things:
▼ Runs when Applet appears on screen Object
● a primitive type or
▲ public void stop() ● Each object is an
instance of a Java class ● a reference to an object
▼ Runs when Applet is off screen
● There are many
▲ public void destroy()
predefined Java classes
▼ Runs when Applet is terminating
7 8
Warning: The picture suggests that each object gets its own ■ accessor methods vs.
copy of each method. This provides some good intuition, but modifier methods?
is not really true…
9 10
11 12
2
Null Equality
■ What happens after the declaration, but before the ■ The “==” operator in Java
assignment? tests whether two variables What happens?
contain the same value
Thing t1;
● For primitive types, this Thing t1 = new Thing ( );
// What has happened here?
is what we want Thing t2 = new Thing ( );
t1 = new Thing( ); t1.setValue(44);
● For objects, this
t2.setValue(44);
compares “addresses”
System.out.println( t1 == t2 );
■ The variable t1 exists, but it contains no reference
● It holds the special value null ■ Need an “equals( )” method
that compares the contents
● null can be assigned to any object variable
of the object
● null can be used in “==” tests
13 14