欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java 1.5 小手册 Cheat Sheet 博客分类: Java JavaSUNCC++C# 

程序员文章站 2024-03-19 16:07:34
...

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/