0% found this document useful (0 votes)
98 views

Java Cheat Sheet

This document provides a cheat sheet on common Java concepts and examples, including how to create objects and output strings, use generics and for-each loops, write JavaDoc comments, define enums and annotations, and write a basic "Hello World" main method.

Uploaded by

Giova Rossi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Java Cheat Sheet

This document provides a cheat sheet on common Java concepts and examples, including how to create objects and output strings, use generics and for-each loops, write JavaDoc comments, define enums and annotations, and write a basic "Hello World" main method.

Uploaded by

Giova Rossi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Cheat Sheet

Java 1.5 Cheat Sheet

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

1/2
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.");

2/2

You might also like