Basics of the Java Language

General Information

Lexical Elements of Java

Java programs are build out of these seven basic lexical elements:
  1. white space
  3. identifier
  4. reserved words
  5. literals
  6. delimiters, and
  7. operators


Java is an object-oriented language. It is impossible to give a simple explanation for what that means. For now, suffice it to say: Java is organized around a construct known as a class. The class in Java is fundamental unit of program construction. And it has syntax:
class ClassName {
   // Contents of class
The class is the major part of a compilation unit (the construct given to the compiler) All code is grouped into methods (or static initializers). Every method (and static initializers) must belong to some class. Generally a Java class is defined in a file by itself (with the same name) and this is the unit of compilation (or translation).
   attributes type name (parameters) {
      // code
For the some time, we focus specifically on static methods, those defined using the static attribute.
   attributes static type name (parameters) {
      // code
Generally speaking, objects are the entities manipulated by the program as in the objects of computation. In Java classes have a second very different role than the one mentioned above. Classes are used as templates for new objects. So there are two kinds of objects in Java: primitive values (like numbers) and instances of classes. In object-oriented languages like Java, we usually refer to instances of classes when we say objects. (This leaves no good word for all objects of computation.)

Structure of a Main Program

class HelloWorld  {
   public static void main (String[] args)  {
      System.out.println ("Hello World!");

The class, HelloWorld in this case, must be in a file with a matching name

Tip: Every class may have a main program. This is useful for unit testing.

Executing a Java Program

Compile producing the byte code in a .class file. Execute with the "launcher" java. Can compile on platforms with different architectures---the byte code is machine independent.
      javac  ;; compile source to byte code
      java HelloWorld        ;; execute "main" of this class
For bigger programs Java normally finds all the other classes and libraries it needs. (No make file is needed.)

compile/run compile/move/run

Java is a big improvement (over C/C++ compiling) as there is no preprocessor and no problem in locating the program pieces. Not only that, but the resulting class files can be run on any platform (with a Java interpreter) without need for recomplication. This make is possible to deploy and distribute application much easier in a heterogeneous environment.

Programs With Multiple Files

Consider two files and
class Main {
   public static void main (String args[]) {
      Point p = new Point ();
class Point {
   int x=0, y=0;
javac          ;; will cause to be compiled
java -verbose:class Main ;; will cause Point.class to be loaded dynamically

[Opened /software/java/jdk1.2.2/jre/lib/rt.jar in 32 ms]
[Opened /software/java/jdk1.2.2/jre/lib/i18n.jar in 4 ms]
[Loaded java.lang.NoClassDefFoundError from /software/java/jdk1.2.2/jre/lib/rt.jar]
[Loaded java.lang.Class from /software/java/jdk1.2.2/jre/lib/rt.jar]
[Loaded java.lang.Object from /software/java/jdk1.2.2/jre/lib/rt.jar]
etc., etc., etc., etc., etc., etc., ...
[Loaded Main]
[Loaded Point]

The execution of the program depends crucially on the fact that both class reside in the same directory (the current working directory). How does Java "know"? The main program needed the class Point, the class Point must be found in the file Point.class, Java looks in the current directory for a file Point.class, if it finds it loads it (dynamically). If the file were missing
java Main
Exception in thread "main" java.lang.NoClassDefFoundError: Point
        at Main.main(, Compiled Code)

Tip: -verbose:class is occasionally useful in complex programs to determine exactly which class is loaded.

java -verbose:class BigProject | grep MyClass
[Loaded project.package.MyClass from not_the_expected_file.jar]

Each Java class (ignoring inner classes) should be in a file by itself. The file name should be the same as the class. Doing anything else is just asking for trouble.

A compilation may, in fact, have multiple class definitions. The Java compiler produces a class file for each class defined. A compilation unit may have at most one public class (a public class is one marked by the keyword public); it must have the same name as the file. If your operating system, ie., DOS/Windows, is lax about the names of files, then you must be vigilant about the file names. Otherwise your files might not compile when transferd elsewhere.


Everything in Java is in some class. Members of classes can be procedures (called methods) and variables (called fields). A name is used to refer to an entity declared in a program. Entities in Java are a package, class type, interface type, member (class, interface, field, or method) of a reference type, parameter (to a method, constructor, or exception handler), or a local variable. There are two forms of names: simple names and qualified names. A simple name is a single identifier. A qualified name consists of a name, a "." token, and an identifier. Names in programs are either simple, consisting of a single identifier, or qualified, consisting of a sequence of identifiers separated by "." tokens (6.2). Not all identifiers are names. (Statement labels, for instance, are not.)


Inside the class the prefix can be omitted, leaving just


A member may refer to an "instance of a class" (introduced later). It's members are referred to using another "dot"


Packages (introduced much later) can add names to the beginning.

Ulitmately there are six kinds of names (six namespaces).

(In this example we purposely violate the style guideline which rightly suggests that class names be capitalized to avoid confusing them with the other five kinds of names.)

Command Line

Local variables, like args, are accessed by their name.

Larger Programs

Java programs can make use of classes and methods found in other files. Java itself comes with an extensive collection of classes. The import statement permits access from a Java package to names without the "dotted" notation. Time for another example

Primitive Data Types and Wrapper Classes

The primitive data types in Java are: boolean, byte, short, char, int, long, float, and double. (Strings aren't really primitive, but they are fundamental and have special syntax (cf Strings at Expressions); enums are new in Java 5.0 (cf Enumeratio Types at Classes).

See more about arbitrary-precision math in the second section on Java classes.
Ryan Stansifer <>
Last modified: Mon Feb 7 13:01:47 EST 2011