Java Quick Reference

by Dave Feinberg

Contents

Library Classes (their constructors and methods)

class java.lang.Character implements java.lang.Comparable<T>
interface java.lang.Comparable<T>
class java.lang.Double implements java.lang.Comparable<T>
class java.lang.Integer implements java.lang.Comparable<T>
class java.lang.Math
class java.lang.Object
class java.lang.String implements java.lang.Comparable<T>
class java.lang.StringBuilder
class java.lang.System
class java.io.File implements java.lang.Comparable<T>
class java.io.FileWriter
class java.io.PrintWriter
class java.util.ArrayList<E> implements Iterable<E>
class java.util.Arrays
class java.util.Collections
interface java.util.Comparator
class java.util.HashMap<K, V> implements java.util.Map<K, V>

class java.util.HashSet<E> implements java.util.Set<E>

interface java.util.Iterable<E>
interface java.util.Iterator<E>
interface java.util.Map<K, V>
interface java.util.Queue<E>
implemented by LinkedList<E>
class java.util.Scanner
interface java.util.Set<E> extends Collection<E>
class java.util.Stack<E>
class java.util.TreeMap<K, V> implements java.util.Map<K, V>

class java.util.TreeSet<E> implements java.util.Set<E>

class javax.swing.JOptionPane

Using A Main Method

public class SomeClass
{
	public static void main(String[] args)
	{
		System.out.println("I run!");
	}
}

Printing To System.out

System.out.print("I print out!  ");
System.out.println("I'm on the same line!");
System.out.println("I'm on the next line!");

Reading From System.in

Scanner scanner = new Scanner(System.in);
...
String text = scanner.nextLine();

Reading From Files

Scanner fileIn = new Scanner(new File("somefile.txt"));
while(fileIn.hasNextLine())
{
	String line = fileIn.nextLine();
	...
}

Writing To Files

PrintWriter fileOut = new PrintWriter(new FileWriter("somefile.txt"));
...
fileOut.println("some text");
...
fileOut.close();

Using Strings


Using Arrays

CodeBehavior
String[] names; Declares names to be of type String[].
names is currently null,
but is capable of referring to an array of Strings.
names = new String[3]; names refers to a new array of size 3.
The array contains 3 nulls,
but it is capable of referring to 3 Strings.
names.length Evaluates to 3--the length of the names array
names[0] = "Dave"; The first position in the array names now refers to the String "Dave".
copy = names; Does not create a new array.
copy refers to the same array as names
names[1] = "fberg"; The second position in the array names, and therefore copy, now refers to the String "fberg".
copy[1] Evaluates to "fberg"
double[] roots = {1.0, 1.4142, 1.7321, 2.0}; Declares and initializes roots to refer to an array of the given 4 doubles.

Using Exceptions

There are two kinds of exceptions in Java. Run-time exceptions are the easy kind. When you write your code, you can forget all about runtime exceptions. They simply crash your program when it runs. These include NullPointerException, ClassCastException, StringIndexOutOfBoundsException, ArrayIndexOutOfBoundsException, etc. On the other hand, consider the following code.
public void giveSnack(Food snack)
{
	friend.eat(snack);
}
Suppose that friend's eat method may throw the compile-time exception AllergicToPeanutsException. Java won't let you compile your giveSnack method until you explicitly handle the AllergicToPeanutsException. There are two ways to handle a compile-time exception. Incidentally, it is sometimes useful to use a try/catch block to catch certain run-time exceptions.

Using Interfaces

public interface Player
{
	int removeFrom(int total);
}

public class SimplePlayer implements Player
{
	public int removeFrom(int total)
	{
		return 1;
	}
}

Using Abstract Classes

public abstract class Player
{
	private String name;

	public Player(String n)
	{
		name = n;
	}

	public String getName()
	{
		return name;
	}

	public abstract int removeFrom(int total);
}

public class SimplePlayer extends Player
{
	public SimplePlayer(String name)
	{
		super(name);
	}

	public int removeFrom(int total)
	{
		return 1;
	}
}

Compile-Time Errors

Error MessageWhat It Usually Means
something expected The parser was surprised by a symbol you wrote at or just before this point.
cannot find symbol -- class You need to import that class.
cannot find symbol -- method You got the method name wrong, or you called it on the wrong class.
class is public, should be declared in a file named Your class name and file name must match exactly.
incompatible types -- expected type Make sure you understand why it found what it did, and why it expected what it did.
incompatible types -- expected type Make sure you understand why it found what it did, and why it expected what it did.
incompatible types -- expected variable You used = instead of ==.
missing method body Your method declaration line has a semicolon.
missing return statement The compiler found a pathway through your non-void method that does not reach a return statement.
non-static method cannot be referenced from a static context You called a method on a class, instead of on a specific instance.
possible loss of precision You assigned a double value to an int variable.
variable might not have been initialized The compiler found a pathway through your method where you access the value of a variable before you've assigned anything to it.

Run-Time Errors

Error MessageWhat It Usually Means
My program freezes. You have an infinite for/while loop.
ArrayIndexOutOfBoundsException You tried to access an array element with an index that was too high or too low.
ClassCastException You lied when you promised that some object was an instance of a specific class.
NoSuchMethodError: main You tried to run a class without a main method.
NullPointerException You tried to call a method on null (or access an array element of null).
OutOfMemoryError You are constructing new objects in an infinite for/while loop.
StackOverflowError You have an infinite recursion.
StringIndexOutOfBoundsException You tried to access a character in a String with an index that was too high or too low.