2017-04-05

operator

math

+, -, *, /
%(mod), ++(+1), –

(++a) will do the operation first then expression
(a++) will do the expression first then operation

int a = 3;
int b = ++a;
// b = 4
int c = 3;
int d = --c;
// d = 2


==, !=, >, >=

digit operator

apply on every digit of binary
A & B, A | B, A ^ B, ~A
A« 2: move digit to the left by argument

A = 0011 1100;
B = 0000 1101;
A&B = 0000 1100;


logical operator

A&&B, A||B, !A

assignment operator

A += B: A = A + B

condition operator

variable x = (expression) ? true-value: false-value;


instanceof

String name = 'James';
boolean result = name instanceof String;
// if name is String, true


loop structure

while

int x = 10;
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}


do while

do{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );


for

for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}


break keyword

for(int x : numbers ) {
if( x == 30 ) {
break;
}


continue keyword

for(int x : numbers ) {
if( x == 30 ) {
continue;
}


enhanced for

int [] numbers = {10, 20, 30, 40, 50};

for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
//
String [] names ={"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}


branch statement

if

int x = 30;

if( x == 10 ){
System.out.print("Value of X is 10");
}else if( x == 20 ){
System.out.print("Value of X is 20");
}else if( x == 30 ){
System.out.print("Value of X is 30");
}else{
System.out.print("This is else ");
}


switch

the variable in switch can be byte, short, int or char

char grade = 'C';

{
case 'A' :
System.out.println("excellent");
break;
case 'B' :
case 'C' :
System.out.println("good");
break;
case 'D' :
System.out.println("fair");
case 'F' :
System.out.println("keep working");
break;
default :
System.out.println("unknown");
}