Show Menu

Operator an statement Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.


[] ()
x++ x--
post unary
++x --x
pre unary
~ + - !
! only for logic
* / %
+ -
<> <= >= instanceof
== !=
for primit­ives, obj type compare addr
&- ^ ->|
&& -> ||
shortcut logic
(boole­an)?x :y
tenary (can be nested)
+= -=...
compound assign­ment, auto casting
casting (type)
== and != are logic; = is assignment
. operator precedent of casting, so be careful with (B a).m() vs (B)a.m()

Numberic promotion

promote the larger operand type
byte,s­hor­t,char ->i­nt-­>fl­oat­->d­ouble
assign result to promoted type
1)unary exclusive short ++ -> still short
2) by default literal is int or double
float f=1;
ok,1 is a definite number
float f=0.1;
X 0.1 is not definite number
long l=powe­r(2,32)
X Intege­r.M­AX_­VAL­UE=­pow­er(­2,32)-1
short a=2,b=­3,z­;z=a*b;
a*b -> int

x++ and ++x

int x=3,y;
assign x to y(1), then increment x(2)
increment x(2), then assign x to y(2)
y=++x*­5/x-- + --x;
No effect in stand for loop

Using enum

1. dot notation, Days.FRIDAY
2. name(), toString() ->capital label
3. ordinal()  -> index from 0
4. valueOf(String) ->parse string to enum
5. compare: == or equals()
6.switch (enum_var) {
   case enum_value:


Why enum?
more readable constant declar­ation
allow compiler time check
avoid unexpected behavior
document upfront
What's enum
start from java5
extends java.l­ang.enum
enum constant internally implem­ented as public static final variables
overriden equals(), toString, hasCode()
may define variab­le,­con­str­uctor, methods just like in a class
name()­,to­Str­ing() return string value
ordinal() return index
could has constuctor and methods, just like a class


assignment overflow ( too high to hold)
int i=1.0;byte b=128;­float f=0.1;
explicit cast required when overflow
underflow (float number too small to represent)
Compound assignment
auto cast back to left operand
assignment chaining allowed
int i,j,k; i=j=k=9;
if (a=9) {} //exce­ption
if (a=true){} //ok

enum example

public enum Days {

// syntax like class, but with enum keyword
// name=capital label;ordinal=index value from 0
// could have fields/constructor/methods inside
// extends java.lang.enum
// implemented internally as class, 
// enum value are public static final object constant

public enum MusicType{
  private int earDamageFacor;
  private MusicType(int earDamageFactor)
 {  this earDamageFactor=earDamageFactor; }
  public int calcHearingLoss(int huors)
  {  return huors*earDamageFactor;}

  public String toString ()
  {  return"-"+this.ordinal()+'_"
     + this.earDamageFactor;

Java Statement

if (boolean) {}
multiple statement need {}
else if (...) {}
one statement may miss {}
else {}
( ...) ? x :y
x,y: same data type
... ? ...?x :y:z
if (x=5) {} //won't compile


switch (liter­al-­value) {
case 1: ....;b­reak;
case 2: ....;b­reak;
8 types, after case is literal
case 0: ....;b­reak;
default: position not matters
breaks; avoid follow through, need in every case, exclude last on
the case value must be compiling time literal


while (...) { .. increate conunter ...}
do {....} wjile (....)
execute >=1 times
infinity loop
while (true) {}

for loop

for ( int i;i<ar­r.l­eng­th-­1;i++)
standard for loop: (4 steps init ->eval ->body exec ->i­ncr­eament, ++i,i++ no different)
multiple declar­ation initia­lizer allowed, separated by ',', should be same type
initia­lizer scope: inside for loop
for ( Class instan­ce:­col­lec­tion){}
for each: iterate only, unable to access i
java.i­terable <-j­­mg.c­ol­lection
Array, ArrayList, List
infinite loop
for( int i=0;i<10;) {i=i++;}
infinite loop
for ( int value, values) {} -> for ( java.u­­era­tor­(In­teger) i=valu­e.i­ter­ator(); i.hasN­ext­()){}

Advance Flow Control

Nested loops
Optional label
all flow control allowed, but bad practice
breaks option­al_­label;
not for­en..else
breakout current block
breakout current block and go to optional label
Continue option­al_­label;
not for­en..else and switch
stop and Continue next iteration
stop and go to optional label


if, while,for loop define local scope
vaiable declared inside control block could not used out of {}