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
Introduction to ObjectOriented Programming
with Java
Spring Semester 2003
Paul Krause
Object-Orientation & Java
Contents
Getting Started
A Little Bit of Syntax
Differences between C and Java
Object-Oriented Programming in Java
Getting Started
Goto:
http://java.sun.com
Download
Its free!
Download
the Java Development Kit (JDK)
the Documentation
Also free!
Buy
JAVA In a Nutshell, by David
Flanagan, Publ. O’Rielly
It’ll cost you £19.95, sorry!
File extensions in Java
.java
Source
javac (compiler)
.class
JVM
Any Hardware (that supports the JVM)
Byte code
Java Virtual Machine
What you get in the JDK
appletviewer
javac
java
classes.zip
src.zip
javadoc
javap
…
For running Applets
Compiles .java .class
Interprets a Java Class
The system provided classes
Complete source for standard
classes
Generates Java HTML documents
Disassembler
Object-Orientation & Java
Contents
Getting Started
A Little Bit of Syntax
Differences between C and Java
Object-Oriented Programming in Java
Defining a Class
public class Account {
Account
members
{
number
balance
credit_account
debit_account
fields
methods
public int number;
public double balance;
public void credit(double x) {
// do some sums
}
public void debit(double y) {
// do checking then sums
}
}
“Circle” Example
public class Circle {
public static final double PI = 3.14159;
Circle
radius
public double radius;
circumference
area
public double circumference() {
return 2 * PI * radius;
}
public double area() {
return PI * radius * radius;
}
}
The “Circle” class
public class Circle {
// A class field
public static final double PI= 3.14159;
// A useful constant
// A class method: just compute a value based on the arguments
public static double radiansToDegrees(double rads) {
return rads * 180 / PI;
}
// An instance field
public double r;
// The radius of the circle
// Two instance methods: they operate on the instance fields
// of an object
public double area() {
// Compute the area of the circle
return PI * r * r;
}
public double circumference() {
// Compute the circumference
return 2 * PI * r;
}
}
The “main” method
public class Circle {
public double r;
// The radius of the circle
public double area() {
// Compute the area of the
circle
return PI * r * r;
}
public double circumference() {
// Compute the circumference
return 2 * PI * r;
}
public static void main(String[] args) {
int input = Integer.parseInt(args[0]);
Circle c = new Circle();
c.r = input;
double result = c.circumference();
System.out.println(result);
}
}
Put “main” in a new class?
public class MakeCircle {
public static void main(String[] args)
{
int input = Integer.parseInt(args[0]);
Circle c = new Circle();
c.r = input;
double circum = c.circumference();
System.out.println(circum);
double a = c.area();
System.out.println(a);
}
}
An Association
Circle
MakeCircle
creates instances of
main
radius
circumference
area
File extensions in Java
.java
Source
javac (compiler)
.class
JVM
Any Hardware (that supports the JVM)
Byte code
Java Virtual Machine
The Circle example
C:\>cd Java
C:\Java contains
Circle.java and
MakeCircle.java
C:\Java>javac Circle.java
C:\Java>javac MakeCircle.java
C:\Java>java MakeCircle 4
25.13272
50.26544
C:\Java>java MakeCircle 5
31.4159
78.53975
C:\Java now also contains
Circle.class and
MakeCircle.class
Object-Orientation & Java
Contents
Getting Started
A Little Bit of Syntax
Differences between C and Java
Object-Oriented Programming in Java
Differences
No
No analogues of #define, #include,
#ifdef
Constants are replaced by static final
fields
No
Preprocessor
Global Variables
Avoids possibility of namespace collisions
We will see later how you can make a
constant or variable globally accessible
Java vs. C
Well-defined
Removes this as a platform dependency
No
primitive type sizes
pointers
Although Java Classes and Arrays are
reference types, these references are
“opaque”. No “address of” or “dereference”
operators
This is not a handicap and eliminates and
important source of bugs
Java vs. C
Garbage
Objects are “tidied away” as soon as there are
no further references to them
So, no need to explicitly manage memory
Eliminates memory leaks
No
Collection
goto statement
Adds exception handling and labelled break
and continue statements
Java vs. C
Variable
Java allows local variable definitions to be
made anywhere in a method or block
Good practice to group them, though
Forward
declarations anywhere
references
Methods can be invoked before they are
defined (we’ll see why it is important to be
able to do this)
Java vs. C
Method overloading
Multiple methods can be defined with the same name,
so long as they have different parameter lists
No struct and union types
No enumerated types
No bitfields
No typedef
No method pointers
No variable-length argument lists
Object-Orientation & Java
Contents
Getting Started
A Little Bit of Syntax
Differences between C and Java
Object-Oriented Programming in Java
The Members of a Class
Class fields
public static final double PI = 3.1416;
Class methods
public static double
radiansToDegrees(double rads)
… }
Instance fields
{
public double radius;
Instance methods
public double circumference() {…}
Class Fields
public static final double PI = 3.14159
of type double
Named PI (capitalise constants)
Assigned a value of 3.14159
The static modifier tags this as a Class
Field
A field
Associated with the class in which it is defined
final modifier means it cannot be
changed
The
Class Fields…
is only one copy of PI
Any instance of Class can refer to this
field as PI
PI is essentially a Global Variable
There
BUT
Methods that are not part of Circle
access this as Circle.PI
No name collisions
Class Methods
public static double radiansToDegrees(double
rads) {
return rads * 180 / PI;
}
parameter of type double and
returns a value of type double
Is essentially a “global method”
Single
// how many degrees is 2.0 radians?
double d =
Circle.radiansToDegrees(2.0);
Instance Fields
public double radius;
Each Circle object can have a have a radius
independent of other Circle objects
Outside a class, a reference to an instance field
must be prepended by a reference to the object
that contains it
Circle c
c.radius
Circle d
d.radius
=
=
=
=
new Circle();
2.0;
new Circle();
c.radius; Are they the same object?
Instance Methods
Instance methods operate on instances of a
Class, and not on the Class itself
E.g.
area()
circumference()
If an instance method is used from outside the
Class itself, it must be prepended by a reference
to the instance to be operated on:
Circle c = new Circle();
c.radius = 2.0;
double a = c.area();
Creating an Instance
Every
Class has at least one constructor
This is used as a default constructor - a
method with the same name as the Class
Circle c = new Circle();
The new operator creates a new
uninitialised instance of the Class
The constructor method is then called,
with the new object passed implicitly
Initialising an Instance
A Constructor can use arguments placed
between the parentheses to perform initialisation
Define a new Constructor for Circle
public Circle(double r) {this.r = r;}
Now two ways:
Circle c = new Circle();
c.r = 0.25;
Or
Circle c = new Circle(0.25);
Multiple Constructors
Public Circle() { r = 1.0; }
Public Circle(double r) {this.r = r;}
This
is perfectly legal
Each constructor must have a different
parameter list
This is a simple example of method
overloading
Destroying Objects
Java
automatically reclaims the memory
occupied by an object when it is no longer
needed
Garbage Collection
The
Java interpreter can determine when
an object is no longer referred to by any
other object or variable
Also works for cycles