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++ :
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:
// 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,
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.)
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."
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.
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:
A field may also be final - effectively it is a constant.
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