[ ^ CSE 341 | section index | <-- previous | next -->]

Java basics review

Java types are strong, explicit, and checked both statically and dynamically. There are two kinds of types: primitives (bool, char, int, etc.) and classes.

Parameters are always passed by value, but all values except primitives have reference semantics (i.e., they behave like pointers).

public class Foo { // Implicitly extends (inherits) Object public int i; // Instance field member // Instance member function public String toString() { return Integer.toString(i); } // Static (classwide) member function private static void increment(int i, Foo f) { i++; f.i++; } public static void main(String[] args) { int x = 0; // Primitive integer on "stack" Foo y; // Reference to instance of class Foo y = new Foo(); // Now points to an actual instance y.i = 0; increment(x, y); // Will print "x=0, y=1" System.out.println("x=" + x + ", y=" + y); } }

Other notable points:


Last modified: Mon Jun 26 11:54:36 PDT 2000