-
When you compile a Java program, the compiler creates a .class
file containing byte codes. To execute the .class file,
you run in a Java Virtual Machine (JVM) that converts the
Java byte codes to native instructions.
-
Java programs allocate objects using the new operator,
which allocates storage for the object from the heap.
Unlike other languages such as C++, the Java Virtual Machine
provides a service known as garbage collection which
automatically removes unreferenced objects from the heap.
-
Java supports two basic categories of types: primitives
and reference types. The eight primitives are simple,
built-in types
that comprise byte, short, int, long,
float, double, char and boolean.
-
Java supports two basic types of methods: instance methods,
and static methods. Before you can call an instance
method, you must first create an instance using new and
then use the returned reference as the receiver. For static
methods, you don't need to create an instance, and the class
itself acts as the receiver. The general form of calling static
methods is:
Classname.methodName()
For example, to call the sqrt()
static method in the Math class:
Math.sqrt(); // the Math class is the receiver
-
Any Java class can define zero or more constructor methods.
Constructors participate in how the new operator creates
objects of that class. Here's the flow of the new operator:
- Allocate space for the object on the heap
- Initialize all fields to logical zero (e.g. 0.0 for double)
- Run field initializers (e.g. private double f = 12.33)
- Call a constructor
Note that the constructor runs AFTER the other steps. Constructor
methods have a special syntax that's different from other methods:
- Constructors must have the same name as the class they are in
- Constructors must NOT specify any return value
Here's an example:
public class MyClass {
public MyClass() { // define a zero-argument constructor
}
}
Other than the above, there are no restrictions on what a constructor
can do. They can read/write any fields in the class (including static
fields) and call any methods in the class.
-
The rules for Java identifiers (names for classes, variables, etc.)
include:
- Names can be any length
- Names can include alphabet (A-Z) and numeric (0-9) and
special characters: $ and _
-
Names must begin with an alpha or special character and
cannot begin with a digit
- Names must not be Java reserved words such as class,
double or true
-
Java includes operators that work on data, including the ++ operator
that increments (adds one to) a variable.
-
The basic syntax of a traditional for loop lets you define
zero or more initialization statements, a boolean expression and
zero or more executeable statements. Here's an example that counts
from 0 to 2 (three times through the loop):
for(int i = 0; i < 3; i++ {
System.out.printlin(i);
}
-
Java arrays are indexed data structures that provide fast access
to individual elements, but cannot be resized after creation. Arrays
provide the length property that evaluates to the number of
elements specified when the array was created. For example:
int[] myArray = new int[8];
System.out.println(myArray.length); // prints 8
-
Java provides several types of collection classes that
store data in different ways. Lists, for example, ArrayList,
maintain the ordering of elements. Sets, for example HashSet,
reject duplicate elements and Maps, such as HashMap consist
of key/value pairs.
-
Java provides the try/catch/finally syntax so that programs can
handle exceptional conditions such as FileNotFoundException.
Each exception is a class and the exception class's superclass
determines the exception category, of which there are three:
- Classes that subclass Error are in the Error category
- Classes that subclass RuntimeException are Unchecked
exceptions
- All other exceptions are in the Checked exception
category