Create a new object instance
StringBuffer buffer = new StringBuffer(128);
Output a string to standard output
System.out.println("Some string");
Create a new object using generic types (array list that holds strings)
ArrayList<String> list = new ArrayList<String>();
For Each Loop (loop over any Iterable object, or array this way)
String[] spaghetti = { "a", "b", "c"};
for (String noodle : spaghetti) {
System.out.println(noodle);
}
JavaDocs Example
/**
* HTML Description here
* @author Bob
* @version 1.0
* @see java.lang.String
*/
public class Foo {
/**
* Method description
* @param arg1 The first arg
* @param arg2 The second arg
* @throws FooException if things are bad
* @return what it returns
*/
public String bar(int arg1, int arg2) throws FooException { }
}
Enum Example
public enum Day { SUN, MON, TUE, WED, THU, FRI, SAT }
public void checkDay(Day d) {
if (d == Day.FRI) {
System.out.println("Yippie!");
}
}
Annotations Example
/** Use to define a license for an annotated element */
public @interface License {
String value();
}
@License("GPL")
public class MyGPLLicensedClass { ... }
Main - Hello World
public class TestRun {
public static final void main(String[] args) {
System.out.println("Hello World.");
}
}
From: http://www.petefreitag.com/cheatsheets/java/