JAVA: Summary / Basic Syntax 95
3
3
.
.
2
2
B
B
a
a
s
s
i
i
c
c
S
S
y
y
n
n
t
t
a
a
x
x
C
C
o
o
n
n
t
t
e
e
n
n
t
t
Comments //Single line comment /* Multi line comment */
Print System.out.println("Hello " + name);
Exceptions try { .. } catch(Exception e) { ... }
Statements
Conditional if, switch, VARIABLE = CONDITION ? TRUE : FALSE
Loop for, while, do
Jump break, continue, return
Operators
Comparison compare values of two variables
Arithmetic perform mathematical operations
Assignment assign value to variable
Bitwise combine bits within one or two int or long values
Logical combine boolean values that can be only TRUE or FALSE
C
C
o
o
m
m
m
m
e
e
n
n
t
t
s
s
Comments
//Single line comment.
/* Multi line
comment. */
P
P
r
r
i
i
n
n
t
t
Print
//DECLARE VARIABLES.
String name = "John";
int age = 20;
//DISPLAY VARIABLES.
System.out.println("Hello " + name); //Hello John (Concatenation)
E
E
x
x
c
c
e
e
p
p
t
t
i
i
o
o
n
n
s
s
MyClass.java
public class Test {
public static void main(String[] args) {
try{
Integer i = null;
i.byteValue(); //Throws Exception (calling Method on a null Object)
}
catch(Exception e){
System.out.println(e.toString());
}
}
}
JAVA: Summary / Basic Syntax 96
S
S
t
t
a
a
t
t
e
e
m
m
e
e
n
n
t
t
s
s
-
-
C
C
o
o
n
n
d
d
i
i
t
t
i
i
o
o
n
n
a
a
l
l
if (evaluates over different Variables/Conditions)
if (age == 20 ) { System.out.println("Jonh is 20" ); }
else if (name == "John" ) { System.out.println("Name is John"); }
else if (name.equals("Bill")) { System.out.println("Name is Bill"); }
else { System.out.println("No match" ); }
switch (evaluates over the same Variable/Condition)
switch (name) {
case "John":
System.out.println("Name is John");
break; //Without break all below cases are also executed.
case "Bill":
System.out.println("Name is Bill"); //There can be multiple commands inside each case
System.out.println("Found Bill");
break;
default:
System.out.println("No match");
}
VARIABLE = CONDITION ? TRUE : FALSE (simplified assignment)
//TEST VARIABLES.
int a = 10;
int b = 5;
//VARIABLE = CONDITION ? TRUE : FALSE
int max = (a > b) ? a : b;
S
S
t
t
a
a
t
t
e
e
m
m
e
e
n
n
t
t
s
s
-
-
L
L
o
o
o
o
p
p
for
//SIMPLE
for(int i=1; i<5; i++) { System.out.println(i); }
//COMPLEX
for(int i=1, j=5; i<5 && j>2; i++, j--) {
System.out.println(i);
}
while
int i = 1;
while(i<5) {
i++;
System.out.println(i);
}
do
int i = 1;
do {
i++;
System.out.println(i);
} while(i<5);
JAVA: Summary / Basic Syntax 97
S
S
t
t
a
a
t
t
e
e
m
m
e
e
n
n
t
t
s
s
-
-
J
J
u
u
m
m
p
p
break (break from loop)
//FOR (WHILE, DO)
for(int i=1; i<5; i++) {
if(i==3) { break; } //Break from Loop.
System.out.println(i); //1, 2
}
//SWITCH
int i = 1;
switch (i) {
case 1:
System.out.println("CASE 1"); //Only selected case is executed.
break; //Exit Switch. Or it will go to next case.
default:
System.out.println("CASE Default");
}
continue (continue with next iteration)
//FOR (WHILE, DO)
for(int i=1; i<5; i++) {
if(i==3) { continue; } //Continue with next loop iteration (skips subsequent lines)
System.out.println(i); //1, 2, 4 (missing 3)
}
return (return from method)
public class Test {
//DECLARE METHOD.
static String test() {
return("Hello"); //Return string.
}
//CALL METHOD.
public static void main(String[] args) {
String result = test(); //Store function's return value into variable result.
System.out.println(result);
}
}
JAVA: Summary / Basic Syntax 98
O
O
p
p
e
e
r
r
a
a
t
t
o
o
r
r
s
s
Comparison Operators (for Numerical values)
//FOR char, byte, short, int, long, float, double.-------------------------------------------------
char left = 65;
long right = 70;
if (left == right) { System.out.println("Left equals right."); } // Equal
if (left != right) { System.out.println("Left is different from right."); } // Not equal
if (left < right) { System.out.println("Left is smaller then right."); } // Less than
if (left <= right) { System.out.println("Left is smaller or equals right."); } // Less than or equal
if (left > right) { System.out.println("Left is greater then right."); } // Greater than
if (left >= right) { System.out.println("Left is greater or equals right."); } // Greater than or equal
//FOR boolean.-------------------------------------------------------------------------------------
boolean l = true;
boolean r = false;
if (l == r) { System.out.println("Left equals right."); }
if (l != r) { System.out.println("Left is different from right."); }
Comparison Operators (for Objects and Strings)
//FOR Objects.-------------------------------------------------------------------------------------
String text = "Hello";
if (text instanceof String) { System.out.println("Object is of class String."); }
//FOR Strings.-------------------------------------------------------------------------------------
String text = "Hello";
if (text.equals ("Hello")) { System.out.println("text is equal to \"Hello\"." ); }
if (text.equalsIgnoreCase("hello")) { System.out.println("text is equal to \"hello\"." ); }
if (text.contains ("llo" )) { System.out.println("text contains \"llo\"." ); }
if (text.contentEquals ("Hello")) { System.out.println("text is equal to \"Hello\"." ); }
if (text.startsWith ("He" )) { System.out.println("text starts with \"He\"." ); }
if (text.startsWith ("llo",2)) { System.out.println("text at index 2 starts with \"llo\"."); }
if (text.endsWith ("llo" )) { System.out.println("text ends with \"llo\"." ); }
if (text.matches ("Hello")) { System.out.println("text matches regex \"Hello\"." ); }
Arithmetic Operators (perform mathematical operations)
//TEST VARIABLES.
int x = 10;
int y = 20;
//ARITHMETIC OPERATORS.
int plus = +y; //+10 = +10
int negate = -y; //-10 = -10
int add = x+y; //10+20 = 30
int subtract = x-y; //10-20 = -10
int multiply = x*y; //10*20 = 200
int divide1 = x/y; //10/20 = 0.5 = 0
float divide2 = (float)x/y; //10.0/20.0 = 0.5
int reminder = x%y; //10%20 = 0*20+10 = 10
int increment1 = ++x; //Increment x by 1 and then store x into increment1.
int decrement1 = --x; //Decrement x by 1 and then store x into decrement1.
int increment2 = x++; //Store x into increment2 and then increment x by 1.
int decrement2 = x--; //Store x into decrement2 and then decrement x by 1.
JAVA: Summary / Basic Syntax 99
Assignment Operators (assign value to variable)
int x = 10; // Assignment
x += 10; // Addition-Assignment x = x + 10
x -= 10; // Subtraction-Assignment x = x - 10
x *= 10; // Multiplication-Assignment x = x * 10
x /= 10; // Division-Assignment x = x / 10
x %= 10; // Modulus-Assignment x = x % 10
Bitwise Operators (combine bits within one or two int or long values)
//TEST VARIABLES.
int left = Integer.parseInt ("11011", 2);
int right = Integer.parseInt ("10010", 2);
//BITWISE OPERATORS.
int and = left & right; //10010. 1 if both bits are 1.
int or = left | right; //11011. 1 if either bit is 1.
int xor = left ^ right; //01001. 1 if bits are different.
int invert = ~left; //00100. Invert bits.
int shiftLeft = left << 3; //11011000. Shift bits to the left by 3. Fill with 0.
int shiftRight = left >> 2; //110. Shift bits to the right by 2. Fill with 0.
int shiftRight2 = left >>> 2; //????
//DISPLAY RESULTS.
System.out.println (Integer.toBinaryString(and)); //Display in Binary Format
Logical Operators (combine boolean values)
//TEST VARIABLES.
char left = 65;
long right = 70;
//LOGICAL OPERATORS.
if ( left> 50 && right==70 ) { System.out.println("Both are true" ); } // AND
if ( left> 50 AND right==70 ) { System.out.println("Both are true " ); } // AND
if ( left!=4 || right> 90 ) { System.out.println("At least one is true" ); } // OR
if ( left!=4 OR right> 90 ) { System.out.println("At least one is true " ); } // OR
if ( left!=4 XOR right> 90 ) { System.out.println("Only one is true ); } // XOR
if ( ! (left==80)) { System.out.println("left is NOT equal to 80" ); } // NOT
if ( NOT (left==80)) { System.out.println("left is NOT equal to 80" ); } // NOT