CS536 Spring 1998
Class Notes for Wednesday, January 21, 1998
Discussion Session on Java
by Charles Fisher
Notes prepared by Paul Baranowski

Java Overview
Java is a new programming language, designed to support secure, platform independent programming.  It's goal is a good alternative to C++.  In many ways Java is similar to C and C++, but it is cleaner and simpler.


Basic Notation
Data is either primitive or an object representing an instance of a class.  All code is written inside classes, so programming consists of writing classes.

Primitive data types are very similar to those of C and C++ :

You must explicitly cast types.

Objects are instances of classes.  All objects are heap-allocated with automatic garbage collection. (Garbage collection runs in the background and reclaims any object you stop referring to, freeing up the memory once used by the object.) A reference to an object, as a variable or parameter, is actually a pointer; although there are no explicit pointer operations.  The "*" and "->" in C++ do not exist in Java.  The following is a example of a class:



// Begin code - the basics of creating a class and accessing data members.
class Point { int x, y; }

// And later in the program you can create an instance with:
Point data = new Point();
// The left hand side is a declaration of a variable of type Point (a reference to it actually).
// The right hand side allocates a new Point object.

// You can assign a value to one of the data members just like in C++ :
data.x = 0;

Point data2;  // Default is a null reference
// (In fact, everything in Java initially defaults to something when it is declared,
// if you forget to initialize it somehow.)

data2.x = 0; //This will produce a run-time error
// because no object has been assigned to data2.

// End Code


Classes contain members.  Members are either fields(data) or methods(functions).
For example,



// Begin code - an example of using methods in a class
class Point {
    int x, y;
    void clear() { x = 0; y = 0; }
}

Point d = new Point();
d.clear();
// End code


A special method is a constructor.  It has no result(return) type and is used only to initialize an object after it is created.  Constructors may be overloaded.  (Overloading : Creating a method with the same name as another method, but which differs in its number and type of arguments.  The compiler figures out which one you want by the arguments.)



// Begin code - an example of a constructor
class Point {
    int x, y;
    Point() { x = 0; y = 0; } // constructor
    Point( int x_in, int y_in) { x = x_in; y = y_in;}  // constructor (overloaded)
}

Point d = new Point();
Point e = new Point(1, 2);
// End code


Members of a class may be declared static.  A static member is allocated only once - for all instances of a class.  Usually you use a static object when you need something to be "global."



// Begin code - an example of using static objects and methods
class Point {
    int x, y;
    static int howMany = 0;
    Point() { x = 0; y = 0; howMany++; }
    static void reset() { howMany = 0 };
}

Point d = new Point();

// Static member functions (methods) may not access non-static data.
// Static members are accessed using a class name rather than an instance name:
Point.reset();

// End code


Class members are public or private.  Public members may be accessed from outside a class; private members are accessible only inside a class.



// Begin code - an example of a private object
class Customer {
    int id;
    private int pinCode;
    Customer() { id = 0; pinCode = 0; }
}

Customer me = new Customer();
me.id = 1234; // This is OK
me.pinCode = 7777; // Compile-time error
// End code


In a class, a static public void method, with a single String array argument, that is named "main" is special.  It is automatically invoked when a class is run:



// Begin code - the canonical "Hello world" program
class Test {
    static public void main( String args[] ) {
        System.out.println("Hello world!");
    }
}
// End code

A field may also be final - effectively it is a constant.



// Begin code - an example of final objects
class Point {
    int x, y;
    static final Point origin = new Point(0,0);
    Point( int x_in, int y_in) { x = x_in; y = y_in;}
}

Final fields may be used to create constants within a class.

class Card {
    final static int CLUBS = 1;
    final static int DIAMONDS = 2;
    final static int HEARTS = 3;
    final static int SPADES = 4;
}

//To access one of these constants:
int suite=Card.SPADES;
// End code



 

Running Java programs (in JDK):
To compile:
    javac myfile.java
(javac stands for Java compiler)
This produces a file called:  myfile.class

To execute a class (using the JVM) :
    java myfile

* Note there is no ".class" suffix!!!


Terms

API - Application Programing Interface


Links