The purpose of this document is to give a taster of what Java is
about, and what it looks like.
Java
Sun Microsystems are the originators of Java. The site www.javasoft.com
contains all the latest Java information and releases.
Java is a programming language designed for a virtual operating
system known as the Java Virtual Machine (JVM for short). Java's
attractive features are:
- Strong type checking at compile time.
- Easy multithreading
- Garbage collection: it cleans up as it goes along, hence it's
harder to have memory leaks.
- Good OO implementation
- OS independent
- Excellent networking support built in: It's the only Internet
programming language.
- Dynamically loads modules at runtime: it's possible to write
code that finds out what else is there and link to it dynamically.
- Reasonably fast.
- GUI which is OS independent.
- Extensible
Classes
Any piece of Java code must be enclosed in a class.
Example:
public class AClass {
private int aVariable;
public AClass(){
aVariable = 10;
}
public int add(int num){
return aVariable += num;
}
}
The above class must be saved in a file of the same name as the
class (AClass.java in the above example).
We have defined a member variable called aVariable and a method
to add values to the member variable.
We have defined a constructor, so that when this object is created
aVariable is set to 10.
To use the above we could write another class:
public class AnotherClass {
public static void main(String[] args){
AClass c = new AClass();
System.out.println("The result is " + c.add(30));
}
}
Notes:
- All classes have a base class Object
- Java defines primitive types, only these types are not classes,
all other types are classes. Java's primitive types are:
- boolean
- char
- byte
- short
- int
- long
- float
- double
- Java objects are not like primitive types. Variables are references
(known as pointers in C & C++) to objects. So two variables
can point to the same object, changing a value using one variable
is visible in the other variable. For example:
Vector aVector, aCopy;
aVect = new Vector();
aCopy = aVector;
aVector.add("Something");
System.out.println(aCopy.elementAt(0).toString());
The above prints "Something"
With primitives, the effect is not the same:
The above prints "int2 is 100"
- Java classes, methods and members have access controls which
can be one of:
- public: any class type can access the member/method/class
- protected: the defining class and any classes which
inherit from the class can access the protected member
- private: the member is not accessible from other class
types, even if they inherit it.
- <blank> if nothing is put the default package
access control applies. Only classes defined in the same package
can access each other's members with a package access control.
- A class can inherit from at most one other class type.
- A class can implement any number of interfaces.
Example:
public class AClass extends java.awt.Frame
implements java.awt.event.ActionListener, java.io.Serializable {
...
}
The above class inherits a Frame's functionality, and implements
the ActionListener and Serializable interfaces.
Interfaces
Interfaces are used to specify a behaviour. Classes can implement
an interface.
Example:
public interface AnInterface
{
public void doSomething();
}
public class AClass implements AnInterface {
public void doSomething(){
System.exit(1);
}
}
public class BClass {
AnInterface member;
public aMethod(AnInterface interfaceThing) {
member = interfaceThing;member.doSomething();
}
}
Applications
A Java application is a class that can be run like a program. In
order for a class to be able to run, it must define a method called
main, with the following signature:
public class AnotherClass {
public static void main(String[] args)
{
AClass c = new AClass();
System.out.println("The result is " + c.add(30));
}
}
To run the above class something like the following is typed
at the command prompt:
java AnotherClass
It is quite common to put the main function in many classes. So
that they can be individually tested.
Packages
Java introduces the idea of a package. It's a neat way of grouping
together related classes. In fact most of Java's implementation
is in packages, which any Java programmer should know about.
A package can be considered as a directory tree on a disk drive.
If your company has the internet domain emp.com, then all your Java
classes should be at least within a package called com.emp. You
could have com.emp.utilities, com.emp.applets, com.emp.applications.....
These would be represented as a directory tree, where each . becomes
a \
When a class is written, the 1st line says what package it's in.
For example:
package com.emp;
import java.util.*;
public class AnotherClass {
public static void main(String[] args) {
AClass c = new AClass();
Vector v = new Vector(10);
// or without the import
java.util.Vector v2 = new java.util.Vector(10);
System.out.println("The result is " + c.add(30));
}
}
The above class has the following features:
- It's in the com.emp package, hence the file is com\emp\AnotherClass.java
- It imports another package called java.util. This means that
we can use any classes in the java.util package without having
to fully qualify the class' package: in this example we have created
a Vector() variable, which is present in the java.util package.
Core Java
Core Java is really the set of packages that are shipped with the
Java SDK. These include java.awt, java.awt.event, java.util, java.io,
java.net.... Good java programmers don't necessarily know all the
packages and classes, but they know that it's worth checking out
what's in there before writing some code: it could already be there.
For example the java.util.Thread class is used for multithreading.
java.util.Vector is good at holding lots of objects.
Java Extensions
Java extensions are usually placed in javax packages. For example
javax.swing is Java's GUI of choice, javax.ejb are Enterprise Java
Beans, javax.servlet.....
Extensions are intended to be additional bolt on packages that
Sun ships separately. Depending on the application in question,
the Java programmer can download these extras, for things such as
XML support, Mail.....
|