Quick Java Reference

  1. Getting Started
  2. Local Information
  3. Java Resources
  4. AWT
  5. Java Compiler
  6. Packages
  7. Command-Line Arguments
  8. Applet Arguments
  9. Timing
  10. Java 2D API
  11. Object References in Java
  12. Inner Classes
  13. Images in Java 2D
  14. Random Numbers
  15. Generic Programming
  16. Bottom

Gettting Started

Local Java Information

If you are on the CS machines, then the latest java compiler is found in /opt/java1.2/bin. An older version is found in /opt/jdk1.1.5/bin.

Java Books and Resources


About the java compilers

% man javac


Command-Line Arguments

Applet Arguments


Java 2D API

Object References in Java

When we pass an object (e.g., Integer) as a parameter to a method, the conventional description is that it is "passed by reference". In contrast, when we pass a primitive (e.g., int), we say it is "passed by value". Despite this conventional terminology, it is often hotly debated (do websearch) whether Java objects are really passed by reference or by value. This is a subtle programming concept that we will not enter in much detail.

If you know about pointers, then the answer is this: whenever we pass an object to a Java method, we are passing the value of the pointer (not the pointer itself). In this sense, we are passing the object parameter "by value" (but it is not "value" in the naive sense).

To illustrate these concepts, suppose we write a simple wrapper class called MyInt, that is similar to Integer:

	public class MyInt{
	   int val=0;

	   public MyInt(int k) { val = k; }

	   //access methods:
	   public void setVal(int n){ val=n; }
	   public int getVal(){ return val; }

	   //string method
	   public String toString(){ return Integer.toString(val);}
Consider the following attempt to assign to an object parameter inside a method. we present two methods -- one method fails, the other succeeds.
	public class RefDemo {

	   static public void setIntFail(int n, MyInt N){
	      System.out.println("n = " + n + ", N = " + N);
	      N = new MyInt(100);

	   static public void setIntSucc(int n, MyInt N){
	      System.out.println("n = " + n + ", N = " + N);
	   public static void main(String[] args){
	      MyInt NN = new MyInt(0);
	      System.out.println("NN = " + NN + " (Failure: NN has not changed to 100)");
	      System.out.println("NN = " + NN + " (Success: NN has changed to 100)");
Here is the output:
	> javac MyInt.java RefDemo.java
	> java RefDemo
	n = 1, N = 0
	NN = 0 (Failure: NN has not changed to 100)
	n = 2, N = 0
	NN = 100 (Success: NN has changed to 100)
NOTE: we couldn't use Integer for our demo becasue Integer class is "final".

Inner Classes

Inner classes are those classes that are defined within the scope of another (outer) class. There are two varieties: static and non-static. In the following example, the class Outer has two inner classes, one static and one non-static:
        public class Outer {
           static int s;
           int n;

           public static class InnerStatic {
              int m;
              void f(){
                  System.out.println("s = " + s); // This is OK
                  m = n;         // ERROR!  Cannot access
                                 // non-static member of outer class.

           public class InnerNonStatic {
              int m;
              void f(){
                  System.out.println("s = " + s); // This is OK
                  m = n;         // This is OK too
From InnerNonStatic, you can refer to your own instance by the usual \verb+this+, but you can also refer to the outer instance using \verb+Outer.this+. Both of these references do not make sense for InnerStatic. Why Inner Classes? This is one way to organize your programs so that you can conveniently refer to certain methods/fields without much fuss (and without making these methods/fields public). This is used frequently in Java2D, e.g., in the the Point2D class.

Images in Java 2D

We treat images separately than the rest of the Java 2D. For more information, see tutorial from Sun.

Random Numbers

To access random numbers, you need to include a package:
	import java.util.Random;    // or: import java.util.*;
Next, you need to set up the random number generator:
	Random generator = new Random(1000);   // 1000 is the seed.
You may omit the seed, which is an optional argument. Now, we can use the random number generator to generate random int values, using one of the following calls:
	int randInt = generator.nextInt();
	int randInt = generator.nextInt(100); // generate value between 0--99.
We can similarly generate random float values:
	double randflot = generator.nextDouble();    // generate uniform value between 0 and 1.0
	double randflot = generator.nextGaussian(); // generate Gaussian distributed value between 0 and 1.0
Note that nextDouble() and nextGaussian() do not take any arguments.

Generic Programming

Java 1.5 introduced generic programming into the language. Recall the "List" interface in java. We would like to talk about a List of Integers, a List of Strings, etc. In general, we wish to have a List of E, where E is any (non-primitive) type. We can therefore define a class called "List<E>" where E is a type variable, to be instantiated when we actually use the class. E can be instantiated by Interfaces or Classes, but not primitive types (like int). Call "List<E>" a generic class.

We will discuss the three generic programming mechanisms: (a) generic interfaces (e.g., java.util.List), (b) generic classes (e.g., java.util.AbstractList), and (c) generic methods. [NOTE: in C++, the generic programming mechanism is called "templates" and there are differences how the concept is implemented.]

An annoying quirk of Java is its treatment of generic arrays. Apparently, there is no way to initialize a generic array in a generic class! You might try:
		class GenericClass {
		   T [] myArray = new T[100];    	// illegal

		   T [] myArray = (T []) new Object [100]; // Cast from Object: compiler warning
		   T element(1);
		   myArray[0] = element;		
The compiler warning can be suppressed. In general, you cannot create instances of a generic type:
		T myObj = new T();	// illegal, as T is a type variable
There is a lot of discussion of this issue, especially of "generic array creation" in the web. The following nice solution uses reflection, and is taken from http://stackoverflow.com/questions/529085/java-how-to-generic-array-creation:
		import java.lang.reflect.Array;  
		public class GenericSet  
		   private E[ ] a;  
		   public GenericSet(Class clazz, int length)  
		      a = clazz.cast(Array.newInstance(clazz.getComponentType( ), length));  
		   public static void main(String[ ] args)  
		      GenericSet foo = new GenericSet(String[ ].class, 1);  

		      // Now, foo.a can be used freely, as expected:
		      String[ ] bar = foo.a;  
		      foo.a[0] = "xyzzy";  
		      String baz = foo.a[0];  
Note that the GenericSet constructor requires a Class object called clazz. This object is able to carry the type information (overcoming "type erasure") of generic types. To use it (when calling the GenericSet constructor) we need to follow the name of the class with a .class. Above, we use "String[ ].class". This is an example of "class literals" from Java Tutorials, which the compiler treats as instances of java.lang.Class.

Go to Top