Java Reference Index


  1. Introduction
  2. Data Types
  3. Arrays
  4. Operators
  5. Program Control
  6. Classes
  7. Objects
  8. Interfaces
  9. Containers
  10. Class Structure
  11. Error Handling

    Program Types

  12. Applications
  13. Applets

    How To

  14. Type Conversions

    Packages

  15. java.lang.reflect
  16. java.applet
  17. java.awt
  18. java.beans
  19. java.io
  20. java.lang
  21. java.math
  22. java.net
  23. java.rm
  24. java.security
  25. java.sa
  26. java.text
  27. java.util
  28. java.servlet

    Classes

  29. Object Class

    Appendices

  30. Terms
  31. Credits

The CTDP Java Reference Version 0.6.0, February 1, 2001

Java Reference Introduction

Java Characteristics

In many ways Java is comparable to C++. The syntax is similar but Java has these characteristics:

  • Smaller and less complex.
  • Supports garbage collection (memory management is handled by the system).
  • Runs on a virtual machine (interpreted).
  • Source code is converted to byte code.
  • Java has no pointers, structures, or unions. Classes and interfaces are used to replace the functionality of these data types.
  • Strings and arrays are objects.
  • Names are case sensitive.

Using a Class in a Package

The following code creates a class that is based on the class applet in the java.applet package.

"
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

public class Palindrome extends java.applet.Applet {
  Font f = new Font(“TimesRoman”, Font.BOLD, 36);

  public void paint(Graphics screen) {
   screen.setFont(f);
    screen.setColor(Color.red);
    screen.drawString(“Go hang a salami, I’m a lasagna hog.”, 5, 40);
  }
}
"

The keyword extends means that the class being created is a subclass of the other. Classes in the java.lang package do not need to be explicitly referenced by package name, but all others must be referenced using one of the following methods:

  • Referenced by full package name.
  • Included in the java source file using an import statement.

The method paint in the superclass Applet is a public method. It must be public in the Palindrome class to override the method in the Applet class.