Java Quick Reference Guide
Java Quick Reference Guide
Simple if
Example
if (expression)
statement;
if (x < y)
x++;
if/else
Example
if (expression)
statement;
else
statement;
if (x < y)
x++;
else
x--;
Example
Logical Operators
!
NOT
&& AND
|| OR
Assignment Operators
=
simple assignment
+= addition/assignment
-= subtraction/assignment
*= multiplication/assignment
/= division/assignment
%= modulus/assignment
integer:
floating-point:
2, 3, -5, 0, 8
2.0, 0.5, -3., 4.653
int x = 3;
double y = 2.5;
Most commonly used reference type in Java is String. String name = "Jack";
if (expression)
statement;
else
if (expression)
statement;
else
statement;
if (x < y)
x++;
else
if (x < z)
x--;
else
y++;
The
"expression" in
the parentheses
for an
if statement
or
loop
is often also
referred to as a
"condition"
Example
if (expression)
{
statement;
statement;
}
if (x < y)
{
x++;
System.out.println( x );
}
Form:
switch (expression)
{
case int-constant :
statement(s);
[ break; ]
Example:
switch (choice)
{
case 0 :
System.out.println( You selected 0. );
break;
case int-constant :
statement(s);
[ break; ]
case 1:
System.out.println( You selected 1. );
break;
[ default :
statement; ]
default :
System.out.println(
You did not select 0 or 1. );
The "expression" and int-constant are usually type int or char. Java 7
adds the ability to use a string. switch(behavior) { case good: }
Use the break keyword to exit the structure (avoid falling through other
cases). Use the default keyword to provide a default case if none of the
case expressions match (similar to a trailing else in an if-else-if
statement).
Form:
Example:
Form:
Example:
init;
while (test)
{
statement;
update;
}
x = 0;
while (x < 10)
{
sum += x;
x++;
}
}
statement;
int scores[ ] = {85, 92, 76, 66, 94}; //collection is the array scores
for ( int number : scores )
//parameter is the variable number
System.out.println(number);
Form:
Example:
Escape Sequences
init;
do
{
statement;
update;
} while (test);
x = 0;
do
{
sum += x;
x++;
} while (x < 10);
newline character
tab character
double quote
single quote
backslash
Operator Precedence
(1) mathematical (2) relational (3) logical
'\n'
'\t'
'\"'
'\''
'\\'
( )
---------*, /, %
---------+, -
[ mathematical ]
Java Arrays:
Selection:
Unary or single selection
Binary or dual selection
Case structure possible when
branching on a variable
Simple selection
One condition
Compound selection
Multiple conditions joined
with AND / OR operators
Looping:
Java Pre-test loops
Test precedes loop body
while
for
Java Post-test loop
Test follows loop body
do-while
Loop Control:
3 types of expressions that
are used to control loops:
initialization ( init )
test
update
Counter-controlled loops,
aka definite loops, work with
a loop control variable (lcv)
Sentinel-controlled loops,
aka indefinite loops, work
with a sentinel value
Java Loop Early Exit:
break statement
Note: The break statement can
be used with a switch
statement or a loop in
Java. Loops may also use
a continue statement.
All arrays have a public field named length which holds the number of elements in the array.
Given this declaration: int x[][][];
x.length
x[m].length
x[m][n].length