Java Cheatsheet/Overview

Actually, this isn't so much a Java programming language cheatsheet as it is a series of running notes. I'm currently (December 2008) learning Java, so this page might not be that useful to anyone but me for a few weeks yet.


Code goes into a text file. That source file is compiled like: javac, which produces the bytecode file MyClass.class. That bytecode (if it contains a runnable main class) can be executed in the Java virtual machine: java MyClass.


Java has end-of-line and block comments:

/* This is
a block
int foo = 3    // Another comment

There are also javadoc comments, which describle classes, constructors, methods, and field, and appear immediately before the declaration. Note that these start with two splats, not just one.

* The class myClass provides . . .
public class myClass { . . .


Each Java program has one or (usually) many classes, but only one Main method:

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


int myInt = 2;
String[] myArray = {"foo", "bar", "bat"};
String myString = "The magic word is: " + myArray[myInt];
int[][] twoDimensional = {
	{ 2, 7, -19},
	{ 23, 9, 44},
	{ 18, -1, 3}

Built-in data types

Loops and Conditionals

Java has the usual loops, including while loops:

int beers = 99
while (beers >= 0) {
	System.out.println(beers + " bottles of beer on the wall!");
	System.out.println(beers + " bottles of beer!");
	beers --;
	System.out.println("Take one down, pass it around, " + beers 
        + " bottles of beer on the wall!");

And do-while loops:

do {
} while (i < 100);

for loops:

for (int i = 0; i <= n; i++) {

To get for-each functionality, use this for loop syntax (note lack of curly braces):

String[] birds = {"owl", "jay", "sparrow", "parrot"};
for (String b: birds)
	System.our.println("The " + b + " is a bird.");

Java has switch statements:

switch (dayOfWeek) {
	case 0: String dayName = "Sunday"; break;
	case 1: String dayName = "Monday"; break;
	case 2: String dayName = "Tuesday"; break;
	case 3: String dayName = "Wednesday"; break;
	case 4: String dayName = "Thursday"; break;
	case 5: String dayName = "Friday"; break;
	case 6: String dayName = "Saturday"; break;

And, or course, if-else:

if (x == y) {
} else if ( x > y) {
} else {

Classes and objects

Classes are the blueprints for objects.

[access modifier] [return data type] [class name] [extends superclass] [implements interface] {


public class Bicycle {
	public int speed;
	public int gear;
	public Bicycle(int startSpeed, int startGear) { 
        // The constructor has the same name as the class.
		speed = startSpeed;
		gear = startGear;
	public void setGear(int newGear) {
		gear = newGear;
	public void faster(int i) {
		speed += i;
	public void slower(int i) {
		speed -= i;

Access modifiers

Access modifiers enforce scope and enable encapsulation. They can be applied to variables or methods.

Class inheritance

A class can have children which inherit thier parent class' methods and members.

public class BikeBuiltForTwo extends Bicycle {
	public boolean secondRider;
	public void addRider() {
		secondRider = true;


Object are instantiated by calling the constructor of the class:

Widget myWidget = new Widget("argument");

Exception handling

try {
	// Do stuff.
} catch (Exception e) {
	// Do this on error.
} finally {
	// Do this, whether there's an error or not.

File IO

Read a file:

String line = null;
try {
	BufferedReader input = new BufferedReader(new FileReader(inputFile));
	while ((line = input.readLine()) != null) {
} catch (Exception e) {
	System.err.println("Can't read input file: " + e.getMessage());

Write a file:

DataOutputStream output = new DataOutputStream(new FileOutputStream(outputFile.txt));

JAR files for archiving and distribution

Java programs can be packaged as runnable, compressed JAR files for distribution.

Note that for a JAR file to be runnable, it have a manifest file to indicate where the main() method is located. A work-around to creating a manifest file is to specify the entry point with the -e flag when creating the JAR file: jar cfe jarfile.jar MyApp MyApp.class