Java Cheat Sheet

Last updated 1998 June 18 by Roedy Green © 1998 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);
    }

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.
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)
'\n' newline,
'\b' backspace,
'\f' formfeed,
'\r' carriage return,
'\t' tab,
'\\' backslash,
'\'' single quote,
'\"' double quote,
'\377' octal (must be exactly 3 digits)
There is no '\' style way of specifying constants in decimal. 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
  */


HTML Checked!
Canadian Mind Products You can get an updated copy of this page from http://mindprod.com/jcheat.html