Department of Engineering

IT Services

Java - A Rough Guide

For people who know a bit about languages like C++ and would like to know a little more about Java's features without having to concern themselves with looking at code.

The Product

The Java 2 product comes in 3 main forms.

  • Standard Edition (what we have on the Teaching System)
  • Enterprise
  • Micro (for VCRs, etc)

The Package Tour

There's a lot to explore - over 2300 classes! All classes are in packages (usually easy enough to guess which package a class is in).

  • Basic (java.lang, java.util, java.math, etc)
  • Graphics (java.awt - Abstract Windowing Toolkit, javax.swing, etc)
  • I/O (java.io, etc)
  • Network (java.net, java.applet, etc)
  • Components (java.beans, etc)
  • DB (java.sql, etc)
  • Text (java.text, etc)

import java.awt.Graphics means that the Graphics class in the java.awt package is made known to the current class. import java.awt.* imports the whole package but not sub-packages. java.lang is automatically imported.

It's not always obvious where things are

  • BigInteger is in java.lang rather than java.math
  • HTML handling is in javax.swing.text.html

There's online documentation - see the API Specification

The Language

It's very Object-Orientated. However

  • though there's inheritance there's no operator overloading.
  • though nearly everything's an object (java.lang.Object tops the class hierarchy), int, byte, boolean, char, float etc aren't - no new is needed to create them, and they're 'passed by value'. If you need to 'pass by reference' there are alternatives - Integer, Byte, etc.

    string is a special case (no new needed and + concatenates, but == doesn't work as C++ people might expect).

Some Interesting sights

  • Memory management (garbage collection)
  • Native Methods -(write once, use anywhere or write once, debug anywhere?)
  • Serialisation (saving/loading objects)
  • Javadoc (integrating source code and documentation)
  • javax.servlet.jsp (serverside scripts)
  • Java3D (flashier graphics)
  • Jini (network technology for devices)

Applets

Applets are runnable via a WWW browser. Thanks to inheritance, applet code can be impressively short. They come with several methods (a selection's below) but often you need only override the paint() method which does the drawing.

  • load() (when page first loaded).
  • init() (when applet first run), destroy()
  • start(), stop() (when applet goes in and out of view)
  • paint() (override!)

Security

  • "sandbox" (applets) - If you're running someone's applet over the WWW you don't want it to start printing or (worse still) writing/removing files so by default applets are restricted in what they can do
  • signed applets - More recently however applets can be written to overcome these restrictions as long as the user trusts them (e.g. the user may trusts applets from certain sites)
  • More recently still, access rights can be defined using policies. A policy is a number of permission objects. Any code (not just applets) can be treated this way. Fine-grain. E.g. you can let a java program read the root directory using
    java.io.filePermission,"/", "read"

How to learn