Java Cheat Sheet
found at http://mindprod.com/jgloss/jcheat.html
Control Structures
// I F / E L S E
if
( a > b )
{ System.out.println ( a ); }
else
{ System.out.println ( b ); }
// I F / E L S E : is x in range low .. high?
if
( low <= x && x <= high )
{ System.out.println ( }
"in range"
);
else
{ System.out.println ( }
"out of range"
);
// S W I T C H / C A S E
switch (
{
n )
case 1:
System.out.println (
"one"
);
break; case 2:
System.out.println (
"two"
);
break; default:
System.out.println ( }// end switch (n) Loops
"something else"
);
// F O R // Note lack of ; after i++
for ( int i=0
; i
=0 ; i-- )
{ System.out.println( i ); }
// D U A L F O R // Note lack of ; after j++
for ( int i = 0
,j = 0; i>>.
4
<< >> >>>
Left (infix)
5 6 7
< > <= >= instanceof == != &
Left (infix) Left (infix) Left (infix) Pascal's <> not equal will not work. == and != work on booleans too, often saving a forest of if/elses. Bitwise AND mostly for for ints. XOR for ints. It is the difference operator. It is true if the boolean operands are different. e.g. false ^ false == false
false ^ true == true true ^ false == true true ^ true == false
8 ^ Left (infix) It is useful in cryptography because of this magic property of encryption and decryption with a random scrambler number. long encrypted = message ^ scrambler ;
long decryped
= encrypted ^ scrambler ; If you XOR twice with the scrambler, you get right back where you started. For booleans it is clearer to use a != b instead of a ^ b and a == b instead of !( a ^ b) 9 10 11 12 | && || ?: = *= /= += -= <<= >>= >>>= &= ^= |= Left (infix) Left (infix) Left (infix) Right ( ternary ) bitwise OR mostly for ints. short circuit logical AND for booleans. short circuit logical OR for booleans. a = b ? c : d; is shorthand for if ( b ) a = c; else a = d;
13
Right (infix)
These make proofreading easier by eliminating typing a variable name twice.
Keywords Java Keywords abstract do boolean break byte case double else extends final import public throws transient try void volatile Page 7 Course Syllabus
instanceof return int interface long short static strictfp
POS 407 -- Computer Programming II
catch char class
finally float for
native new package private
super switch synchronized this
while
continue if default
implements protected throw Reserved keywords (not currently in use)
const
goto Reserved Literals
null
true
false
JavaDoc /** FormattedTextField.java * @author Roedy Green * @version 1.34 1998 January 18 * @deprecated Noreplacement * @deprecated Replaced by otherMethod(int) * @see otherMethod * @see #otherMethod * @see java.awt.Component#repaint * @see Java & Internet Glossary * @see "design patterns by Gamma et. al" * @param x pixels right of the origin. * @return number of oranges. * @exception java.beans.PropertyVetoException when mask is invalid * @since JDK1.1 */
POS 407 -- Computer Programming II
Page 8
Course Syllabus