Java Cheat Sheet

Last updated by Roedy Green ©1996-1999 Canadian Mind Products.

This document is a quick summary of the Java language syntax. It is not complete. It just contains the things you may be likely to forget.

Control Structures

if (a > b) {
    System.out.println(a);
    }
else {
    System.out.println(b);
    }

switch (n) {
   case 1:
        System.out.println("one");
        break;

    case 2:
        System.out.println("two");
        break;

    default:
        System.out.println("something else");
    } // end switch (n)

Loops

for (int i=0; i<n; i++) {
    System.out.println(i);
    }

for (int i=0,j=0; i<n; i++,j++) {
    System.out.println(i);
    }

// However, this is illegal!
for (int i=0,float r=1.0; i<n; i++,r=r*2.0) {
    System.out.println(i);
    }

for (Enumeration e = props.propertyNames(); e.hasMoreElements(); ) {
    String key = (String) e.nextElement();
    System.out.println(key);
    }

while (moreData()) {
    readIt();
    }

do {
    readIt();
    if (done) break;
    if (bypassThisOne) continue;
    processIt();
    }
while (moreData());

Try/Catch/Throw

public class Test extends StandardTest {

public static void main (String[] args) {
    try {
       dangerMethod();
    }
    catch (StrangeException e) {
        System.out.println("oops " + e.getMessage());
    }
    } // end main

void dangerMethod() throws StrangeException {
   if (unexpected) throw new StrangeException ("oh oh");
   } // end dangeMethod

} end class Test

Literals

ints 1 -1, hex ints 0x0f28, unicode hex '\u003f', octal 027
longs 3L, -99l, 0xf011223344L (Beware! some compilers will just chop the high bits from literals without the trailing L even when assigning to a long.)
floats 1.0345F, 1.04E-12f, .0345f, 1.04e-13f, Float.NaN
doubles 5.6E-120D, 123.4d, 0.1, Double.NaN, Math.PI
Note floating point literals without the explicit trailing f, F, d or D are considered double. In theory you don't need a lead 0, e.g. 0.1d may be written .1d, though the Solaris compiler seems to require it.
Beware! A lead 0 on an integer implies OCTAL. That was a major design blunder inherited from C, guaranteed to introduce puzzling bugs. Be especially careful when specifying months or days, where you naturally tend to provide a lead 0.
boolean true and false,
strings "ABC", enclosed in double quotes,
chars 'A', enclosed in single quotes,
or integer forms e.g. 45, 0x45, '\u003f'
Escape sequences inside char and string literals include:
'\u003f' unicode hex, (must be exactly 4 digits) converted to literal character prior to compile.
'\n' newline, ctrl-J (10, x0A)
'\b' backspace, ctrl-H (8, 0x08)
'\f' formfeed, ctrl-L (12, 0x0C)
'\r' carriage return, ctrl-M (13, 0x0D)
'\t' tab, ctrl-I (9, 0x09)
'\\' backslash,
'\'' single quote (optional inside " "),
'\"' double quote (optional inside ' '),
'\377' octal (must be exactly 3 digits. You can get away with fewer, but then you create an ambiguity if the character following the literal just happens to be in the range 0..7.)
\007 bel, ctrl-G (7, 0x07)
\010 backspace, ctrl-H (8, 0x08)
\013 vt vertical tab, ctrl-K (11, 0x0B)
\032 sub, eof, ctrl-Z (26, 0x1A)
There is no '\' style way of specifying decimal constants. Just use char c = 123;
The following C forms are not supported:
'\a' alert,
'\v' vertical tab,
'\?' question mark.
'\xf2' hex.

Primitives

Type Signed? Bits Bytes Lowest Highest
boolean n/a 1 1 false true
char unsigned Unicode 16 2 '\u0000' '\uffff'
byte signed 8 1 -128 +127
short signed 16 2 -32,768 +32,767
int signed 32 4 -2,147,483,648 +2,147,483,647
long signed 64 8 -9,223,372,036,854,775,808 +9,223,372,036,854,775,807
float signed exponent and mantissa 32 4 ±1.40129846432481707e-45 ±3.40282346638528860e+38
double signed exponent and mantissa 64 8 ±4.94065645841246544e-324 ±1.79769313486231570e+308

Precedence

Precedence Operator Association
1 ++ --
(unary) + - ~ !
(cast)
Right
2 * / % Left
3 + - Left
4 << >> >>> Left
5 < > <= >= Left
6 == != Left
7 & Left
8 ^ Left
9 | Left
10 && Left
11 || Left
12 ? : Right
13 = *= /= += -=
<<= >>= >>>=
&= ^= |=
Right

JavaDoc

/**
  * @(#)FormattedTextField.java    1.34 98/01/27
  * @author Roedy Green
  * @version 1.34 1998 January 18
  * @deprecated No replacement
  * @deprecated Replaced by otherMethod(int)
  * @see otherMethod
  * @see #otherMethod
  * @see java.awt.Component#repaint
  * @see <a href="http://mindprod.com/gloss.html">Java glossary</A>
  * @param x >B<pixels>/B< right of the origin.
  * @return number of oranges.
  * @exception java.beans.PropertyVetoException when mask is invalid
  * @since JDK1.1
  */

CMP_home You are visitor number
.
HTML Checked! award
CMP_home Canadian Mind Products You can get an updated copy of this page from http://mindprod.com/jcheat.html The Mining Company's
Focus on Java
Best of the Net Award