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 MyClass.java text file. That source file is compiled like: javac MyClass.java, 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 comment */ 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}
}
boolean with value "true" or "false"char like "A" or "\n" or "5"int like "3" or "-143"double like "33.2344" or "-0.753212"String like "Hello, world!" or "Foo Bar"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 {
stuff();
i++;
} while (i < 100);
for loops:
for (int i = 0; i <= n; i++) {
doStuff();
}
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) {
doStuff();
} else if ( x > y) {
otherStuff();
} else {
ohNoes();
}
Classes are the blueprints for objects.
[access modifier] [return data type] [class name] [extends superclass] [implements interface] {
[declarations]
[methods]
}
Example:
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 enforce scope and enable encapsulation. They can be applied to variables or methods.
public accessible from all classesprivate accessible within its own classA 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");
try {
// Do stuff.
} catch (Exception e) {
// Do this on error.
} finally {
// Do this, whether there's an error or not.
}
Read a file:
String line = null;
try {
BufferedReader input = new BufferedReader(new FileReader(inputFile));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
} catch (Exception e) {
System.err.println("Can't read input file: " + e.getMessage());
System.exit(1);
}
Write a file:
DataOutputStream output = new DataOutputStream(new FileOutputStream(outputFile.txt)); output.writeUTF(textOutput); output.close();
Java programs can be packaged as runnable, compressed JAR files for distribution.
jar cf jarfile inputfile1 inputfile2 inputdirjar tf jarfilejar xf jarfilejar uf jarfile1 jarfile2java -jar jarfile.jarNote 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
© Paul Gorman