Java Lecture 1
[Lecture Index]
[Next Lecture]
Main features of Java Language
- Object-Oriented
- C/C++ like syntax (but cleaner)
- compiled to byte code, which is then interpreted
- `safe' design: disallows common C/C++ errors
- run time checking
- no pointer manipulation
- strongly typed
- garbage collection
- `portable' design: same results on all machines
- same `binary' works everywhere
- data types have defined ranges & behaviour
- machine dependent aspects hidden or not available
(goal not fully achieved: java.util.HashTable, threads)
- secure design - protection from malicious programs
(in theory)
- `advanced' features:
- garbage collection (no need to free memory)
- exceptions: another way to handle errors
- threads: can do several things at once
- widely adopted on the net
(i.e., included in 2 most popular browsers)
HelloWorld.java
class HelloWorld {
static public void main (String args[]) {
System.out.println("Hello, World!");
}
}
How to Compile Java Programs
- Java installed on linux boxes and freebsd boxes
(don't use garfield)
- Add /local/java/bin to your PATH
- To compile a java program:
dove $ javac foo.java
Creates a file ClassName.class for each class
defined in foo.java
- To run a java program:
dove $ java ClassName
Looks for a file called ClassName.class, and
calls the method main() within that class.
(applets are run using appletviewer)
Echo.java
class Echo {
public static void main(String av[]) {
for (int i = 0; i < av.length; i++)
System.out.print(av[i] + " ");
System.out.println();
}
}
Some Basic Language Details
-
Java source files can contain:
- class definitions
- interface definitions (fully abstract classes)
- name space controls (package, import)
Note: All code is contained within classes
-
Classes can have:
- methods (including constructors)
- variables
- types (i.e., nested classes)
- initializers
Each can be tied to an instance (non-static) or to the class
(static)
- two kinds of data types:
- built-in scalar types (e.g., int, float)
- objects (i.e., class and array instances)
- class libraries can be imported:
import java.io.*;
(brings the symbols defined in java.io package
into the current scope).
MyPoint.java
class MyPoint {
public MyPoint() {
}
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() { return x; }
public int getY() { return y; }
public void moveBy(int dx, int dy) {
x += dx;
y += dy;
}
public double distanceTo(MyPoint p) {
double dx = x - p.x;
double dy = y - p.y;
return Math.sqrt(dx * dx + dy * dy);
}
public String toString() {
return "(" + x + "," + y + ")";
}
private int x, y;
// For testing purposes
static public void main(String x[]) {
MyPoint p1, p2;
p1 = new MyPoint();
p2 = new MyPoint(4, -8);
System.out.println("Point 1 is: " + p1);
System.out.println("Point 2 is: " + p2);
System.out.println("Distance is: "
+ p1.distanceTo(p2));
}
}
Scalar Data Types
Integer types:
Type | byte | short | int | long | char
|
# bits | 8 | 16 | 32 | 64 | 16
|
signed? | yes | yes | yes | yes | no
|
- integer types can not be assigned real types
(use casts: int i = (int) 1.6;)
- char stores unicode characters
Boolean type:
- type name is boolean
- literal values are true and false
- int, double, objects, etc
are not booleans
Real types:
Type | float | double
|
# bits | 32 | 64
|
- real types follow IEEE 754 standard
- can assign float from int
- can not assign float from double
(without a cast)
- 1.6 is a double, so
float f = 1.6;
is not legal; use 1.6f
Notes on Instance Data Types
-
all instances are dynamically allocated
-
arrays are a special kind of `instance'
-
class and array instances are created with new:
MyPoint c = new MyPoint(0, 0);
int x[] = new int[10];
-
the keyword null is used for a `null pointer':
MyPoint a = null;
MyPoint c = maybeCreatePoint();
if (c == null)
...
-
all classes are derived from the pre-defined Object class
(arrays are also derived from this)
-
the Object class provides some methods,
the most commonly used of these is
public String toString();
Class Syntax
[public] [abstract]
class ABC
[extends XYZ]
[implements PRQ[, STU]]
{
// constructors
public ABC() {
super();
a = 8;
count++;
}
public ABC(int x) {
this();
a += x;
}
// instance methods
public void aMethod() {
a++;
this.b++;
}
// instance variables
int a;
int b = 10;
// class initialization
static {
stuff = 1;
}
// class methods
public static void main(String args[]) {
// ...
}
// class variables
static final int MAXGIZMOS = 10;
static int stuff;
}
[Lecture Index]
[Next Lecture]