Java Quick Reference Guide: String Comparisons
Java Quick Reference Guide: String Comparisons
Arithmetic Operators
+
Addition
Subtraction
/
Division (int / floating-point)
2/3 = 0, 2.0/3.0 =.666667
*
Multiplication
%
Modulus (integer remainder)
Relational/Equality Operators
<
Less than
<= Less than or equal to
>
Greater than
>= Greater than or equal to
== Equal to
!= Not equal to
Example
if (expression)
statement;
if (x < y)
x++;
String Comparisons:
if/else
Example
if (expression)
statement;
else
statement;
if (x < y)
x++;
else
x--;
Example
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
Logical Operators
!
NOT
&& AND
|| OR
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 trailing else in an if-else-if
statement).
Form:
Example:
Form:
Example:
x = 0;
while (x < 10)
{
sum += x;
x++;
}
init;
while (test)
{
statement;
update;
}
}
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);
Example:
init;
do
{
statement;
update;
} while (test);
x = 0;
do
{
sum += x;
x++;
} while (x < 10);
Escape Sequences
Operator Precedence
newline character
tab character
double quote
single quote
backslash
'\n'
'\t'
'\"'
'\''
'\\'
( )
---------*, /, %
---------+, -
Java Arrays:
[ mathematical ]
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