Show Menu
Cheatography

Operator an statement Cheat Sheet (DRAFT) by

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

Operators

[] ()
x++ x--
post unary
++x --x
pre unary
~ + - !
! only for logic
* / %
+ -
<<>­>>>>
shift
<> <= >= instanceof
compare
== !=
for primit­ives, obj type compare addr
&- ^ ->|
logic
&& -> ||
shortcut logic
(boole­an)?x :y
tenary (can be nested)
=
assignment
+= -=...
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;
y=x++;
assign x to y(1), then increment x(2)
y=++x;
increment x(2), then assign x to y(2)
y=++x*­5/x-- + --x;
x=2,y=7
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:
   ..
  default:
   ....
 }

enum

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
 

Assign­ments

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 {
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
  THURSDAY, FRIDAY, SATURDAY;
}

// 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{
  CLASSIC(1),JAZZ(4),ROCK(6),METAL(11);
  private int earDamageFacor;
  
  private MusicType(int earDamageFactor)
 { this earDamageFactor=earDamageFactor; }
 
  public int calcHearingLoss(int huors)
  { return huors*earDamageFactor;}

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

Java Statement

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

Switch

switch (liter­al-­value) {
Byte,b­yte­,Sh­ort­,sh­ort­,In­teg­er,int
case 1: ....;b­reak;
Charac­ter­,ch­ar,­enu­m,S­tring
case 2: ....;b­reak;
8 types, after case is literal
case 0: ....;b­reak;
default: position not matters
default:
breaks; avoid follow through, need in every case, exclude last on
}
the case value must be compiling time literal

While

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­tio­n){}
for each: iterate only, unable to access i
java.i­terable <-j­ava.la­mg.c­ol­lection
Array, ArrayList, List
for(­;;){}
infinite loop
for( int i=0;i<10;) {i=i++;}
infinite loop
for ( int value, values) {} -> for ( java.u­itl.it­era­tor­(In­teger) i=valu­e.i­ter­ator(); i.hasN­ext­()){}

Advance Flow Control

Nested loops
Optional label
OUTER_­LOO­P:.... INNER_LOOP
all flow control allowed, but bad practice
breaks option­al_­label;
not for if..th­en..else
breakout current block
breakout current block and go to optional label
Cont­inue option­al_­label;
not for if..th­en..else and switch
stop and Continue next iteration
stop and go to optional label

Scope

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

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi