Much of Java Looks Like C
Objects and Classes ■ Arithmetic & relational
operators are the same:
■ Goal was to make a +, -, *, / and <, >, <=, >=
programming language that
people would pick up easily ■ Assignment is the same:
a = b;
CS211
Fall 2000 ■ There are lots of C and ■ Conditional & looping
C++ programmers, so statements are the same:
make it much like C if/else, while, for, do, break,
continue, switch
■ Arrays are the same:
a[i] and b[i][j]
2
What’s Different? Object Oriented Programming
■ Java allows method ■ In Java, there is a separate ■ This is a style of ■ An object is a software
overloading String class programming based on the bundle of data and related
● C++ does this, but C ● A String is not the same ideas of operations (the operations
does not as an array of ● Objects are called methods in Java)
● C++ also allows characters and it is not ● Classes
operator overloading; terminated by the NUL ● Inheritance ■ A class is a template that
Java does not character defines objects of a certain
■ The Java numeric types all ■ Java does automatic kind
Garbage Collection ■ Java is based on these
conform to IEEE standards ideas
● C numeric types can ■ Many other differences… ■ Using one class, I can
vary depending on create several objects,
platform ■ Currently, this is the best of where each is an instance
■ Java is claimed to be safer, known programming styles
■ Java does not have explicit more portable, and easier of this class
pointers to use than C++
3 4
Simple Inheritance Java Programs
■ Classes can be defined in ■ Some classes in Java ■ A Java program consists of a number of interacting
terms of other classes ● String classes
● If a new class B is ● Vector
● All methods and all variables reside within some
based on a previous ● Stack
class A then class
● Hashtable
▲ B is a subclass of A
▲ A is a superclass of B ■ When an application runs
■ Stack is a subclass of
Vector which is a subclass ● You specify a class
■ In general, the variables of Object
and operations of a class ● The “system” looks for and runs the method that
are available to its looks like
subclasses ■ All Java classes are
▲ public static void main(String[ ] args)
subclasses of Object
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
A Simple Example Class Some Terminology
When
Thing t = new Thing( ); ■ private?
public class Thing { public class Thing {
private int value;
is executed, an object is private int value;
public static int count; created that looks like this public static int count; ■ static?
public void setValue (int v) { value = v; } public void setValue (int v) { value = v; }
Thing class name
public int getValue ( ) { return value; } public int getValue ( ) { return value; }
// Plus other methods value // Plus other methods ■ static members vs.
} count attributes } instance members?
getValue( )
setValue( ) methods
■ function vs. procedure?
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
Objects vs. References Object vs. Reference Example
When ■ Pictorially, we represent What happens?
Thing t = new Thing ( ); this as
public class Thing { Thing t1;
is executed, the variable s private int value; Thing t2;
does not contain the object public static int count;
t The object t1 = new Thing ( );
public void setValue (int v) { value = v; } t2 = t1;
public int getValue ( ) { return value; }
■ Instead, it contains a t2.setValue(4);
reference to the object System.out.println(t2.getValue( ));
■ In fact, it’s more like this // A constructor t2 = new Thing ( );
public Thing ( ) { count++; } System.out.println(t1.getValue( ));
■ In other words, t contains t address 22543 System.out.println(Thing.count);
// Plus other methods
the address of the place in
}
memory where the object is
22543
stored
The object
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
An Improved Thing class Assignment vs. Copying (Cloning)
■ Every class automatically ■ What happens if we really public class Thing {
public class Thing { private int value;
has an equals( ) method want to make a copy of an
private int value; public static int count;
public static int count; ● The default equals( ) object?
public void setValue (int v) { value = v;
public void setValue (int v) { value = v; } method is inherited from ■ Can’t do it this way: }
public int getValue ( ) { return value; } Object public int getValue ( ) { return value; }
Thing t1 = new Thing( );
// A constructor public Thing ( ) { count++; }
▲ This is usually not what // Do stuff with t1; now make a copy
public Thing ( ) { count++; } public boolean equals (Thing other) {
you actually want Thing t2 = new Thing( );
return value == other.value;
▲ You often need to write t2 = t1;
// Equality test }
public boolean equals (Thing other) {
your own equals( ) ■ Instead we use the public Thing clone ( ) {
return value == other.value; “clone( )” method: Thing thing = new Thing( );
} Thing t2 = t1.clone( ); thing.value = getValue( );
// Plus other methods return thing;
}
■ Can use inherited (from }
Object) clone( ) if class }
Thing implements Cloneable
15 16
Another “must-have” Method Parameter Passing
■ Methods that appear in ■ An example “change”
many classes ■ In Java, all parameters are method
● equals( ) ■ A toString( ) method for passed by copying their public void change (int j, Thing t) {
values j = 4; t.setValue(5);
● clone( ) Thing:
}
● toString( ) ● For primitive types, this
controls what an public String toString ( ) {
creates a new copy
instance of your class return “[ Thing ” + value + “]”; ● For objects, this makes ■ What does the following
looks like when printed } of copy of the object’s code do?
reference Thing t1 = new Thing( );
■ All these methods have t1.setValue(1);
int i = 10;
default versions that are
change(i,t1);
defined in the class Object
■ What happens if change(
17 18 ) sets t to null?