Sie sind auf Seite 1von 66

Java Language and OOP

Part II
By Hari Christian
Agenda
01 Operator in Java
02 Operator - Assignment
03 Operator - Relational
04 Operator - Arithmetic
05 Operator - Conditional
06 Operator - Bitwise
07 Operator - Logical
08 Operator - Precedence
09 Operator - Associativity
Agenda
10 Selection Statement - If
11 Selection Statement - Switch
12 Selection Statement - Conditional
13 Looping Statement - For
14 Looping Statement - While
15 Looping Statement - Do While
16 Continue Statement
17 Break Statement
18 Basic of Arrays
19 Arrays of Arrays
Operator
An operator is a punctuation mark that says to
do something to two (or three) operands

An example is the expression "a * b". The "*" is
the multiplication operator, and "a" and "b" are
the operands
Operator
Example:
int a = 3 * 2;

int b = 3 + t.calculate();

arr[2] = 5;

arr[2+5] = 2 + 5;


Operator - Assignment
Using notation equal (=)

This Operator using two operand, left operand
and right operand

Expression on the right operand is evaluated
and the result stored in left operand
Operator - Assignment
int num1 = 5; // Assign value 5 to num1
int num2 = num2 + 5; // Add value 5 to num2
int num3 += 5; // Add value 5 to num3
int num4 = num4 - 5; // Substract 5 to num4
int num5 -= 5; // Substract 5 to num5
int num6 = num6 * 5; // Multiply 5 to num6
int num7 *= 5; // Multiply 5 to num7
int num8 = num8 / 5; // Divide 5 to num8
int num9 /= 5; // Divide 5 to num9



Operator - Relational
Relational Operator always give a boolean result
(true or false)

6 Relational Operator:
Operator Description
< Less than
> More than
<= Less than or Equal
>= More than or Equal
== Comparison
!= Not equal
Operator - Relational
int num1 = 5;

if (num1 < 5) {
System.out.println(Less than 5);
}

if (num1 > 5) {
System.out.println(More than 5);
}


Operator - Relational
int num1 = 5;

if (num1 <= 5) {
System.out.println(Less than or equal 5);
}

if (num1 >= 5) {
System.out.println(More than or equal 5);
}


Operator - Relational
int num1 = 5;

if (num1 == 5) {
System.out.println(Equal 5);
}

if (num1 != 5) {
System.out.println(Not equal 5);
}


Operator Instance Of
class Vehicle {}
class Car extends Vehicle {}
class Mercedes extends Car {}

Vehicle v = new Vehicle();
Mercedes m = new Mercedes();
if (m instanceof Vehicle) {
System.out.println(m is Vehicle");
}
if (v instanceof Mercedes) {
System.out.println(v is Mercedes");
}

Operator - Arithmetic
Arithmetic Operator just like a Math!!!

7 Arithmetic Operator:
Operator Description
+ Addition
- Substraction
* Multiplication
/ Divide
% Modulus
++ Increment by 1
-- Decrement by 1
Operator - Arithmetic
int addition = 5 + 5;
int substraction = 5 - 5;
int multiplication = 5 * 5;
int divide = 5 / 5;
int modulus = 5 % 5; // 100 % 17 = ?
int num = 5;
int preIncrement = ++num;
int postIncrement = num++;
int preDecrement = --num;
int postDecrement = num++;

Operator - Conditional
Also called Ternary Operator

This operator using notation ? and :

Example:
int num1 = (5 == 5) ? 0 : 1;
int num2 = (5 != 5) ? 0 : 1;
int num3 = (5 < 5) ? 0 : 1;
int num4 = (5 > 5) ? 0 : 1;
Operator - Bitwise
int a = 60; /* 60 = 0011 1100 */
int b = 13; /* 13 = 0000 1101 */

c = a & b; /* 12 = 0000 1100 */
c = a | b; /* 61 = 0011 1101 */
c = a ^ b; /* 49 = 0011 0001 */
c = ~a; /*-61 = 1100 0011 */
c = a << 2; /* 240 = 1111 0000 */
c = a >> 2; /* 215 = 1111 */
c = a >>> 2; /* 215 = 0000 1111 */

Operator - Logical
To combine other operator

2 Relational Operator:
Operator Description
&& And
|| Or
Operator Logical And
AND Combination table




Operator 1 Operator 2 Result
False False False
False True False
True False False
True True True
Operator Logical And
Example:
int num = 5;

if (num == 6 && num > 10) { // false and false
System.out.println(Hari Christian);
}

if (num == 6 && num > 2) { // false and true
System.out.println(Hari Christian);
}

Operator Logical And
Example:
int num = 5;

if (num == 5 && num > 10) { // true and false
System.out.println(Hari Christian);
}

if (num == 5 && num > 2) { // true and true
System.out.println(Hari Christian);
}

Operator Logical Or
OR Combination table




Operator 1 Operator 2 Result
False False False
False True True
True False True
True True True
Operator Logical Or
Example:
int num = 5;

if (num == 6 || num > 10) { // false and false
System.out.println(Hari Christian);
}

if (num == 6 || num > 2) { // false and true
System.out.println(Hari Christian);
}

Operator Logical Or
Example:
int num = 5;

if (num == 5 || num > 10) { // true and false
System.out.println(Hari Christian);
}

if (num == 5 || num > 2) { // true and true
System.out.println(Hari Christian);
}

Operator - Precedence

calories
Symbol Note Precedence
++ -- pre-increment or decrement

16
++ --

post-increment or decrement 15
~ flip the bits of an integer 15
! logical not 14
- + arithmetic negation or plus 14
(typename) type conversion or cast 13
* / % multiplicative operators 12
- + additive operators 11
<< >> >>> left and right bitwise shift 10
Operator - Precedence

calories
Symbol Note Precedence
instanceof
< <= > >=
relational operators 9
== != equality operators 8
& bitwise and 7
^ bitwise exclusive or 6
| bitwise inclusive or 5
&& conditional and 4
|| conditional or 3
? : conditional operator 2
= *= /= %= += -=
<<= >>= >>>= &=
^= |=
assignment operators 1
Operator - Associativity

calories
Symbol Note Precedence Associativity
++ -- pre-increment or decrement 16 right
++ -- post-increment or decrement 15 left
~ flip the bits of an integer 15 right
! logical not 14 right
- + arithmetic negation or plus 14 right
(typename) type conversion or cast 13 right
* / % multiplicative operators 12 left
- + additive operators 11 left
<< >> >>> left and right bitwise shift 10 left
Operator - Associativity

calories
Symbol Note Precedence Associativity
instanceof
< <= > >=
relational operators 9 left
== != equality operators 8 left
& bitwise and 7 left
^ bitwise exclusive or 6 left
| bitwise inclusive or 5 left
&& conditional and 4 left
|| conditional or 3 left
? : conditional operator 2 right
= *= /= %= += -
= <<= >>=
>>>= &= ^= |=
assignment operators 1 right
Selection Statement - IF
Format:
if ( Expression ) Statement [ else Statement ]

Explanation:
Expression must have boolean type
Using { } to make more than 1 statement


Selection Statement - IF
Example:
boolean valid = false;
if (valid = true) {
System.out.println(VALID);
} else {
System.out.println(TIDAK VALID);
}
Selection Statement - IF
Example:
boolean valid = false;
if (valid) {
System.out.println(VALID);
} else {
System.out.println(TIDAK VALID);
}
Selection Statement - IF
Example:
boolean valid = false;
if (valid == true) {
System.out.println(VALID);
} else {
System.out.println(TIDAK VALID);
}
Selection Statement - IF
Example:
int number = 5;
if (number == 5) {
System.out.println(PT);
}
if (number > 2) {
System.out.println(GUNATRONIKATAMA);
}
if (number > 5) {
System.out.println(CIPTA);
} else {
System.out.println(LESTARI);
}

Selection Statement - IF
Example:
int number = 5;
if (number == 5) {
System.out.println(PT);
} else if (number > 2) {
System.out.println(GUNATRONIKATAMA);
} else if (number > 4) {
System.out.println(CIPTA);
} else {
System.out.println(LESTARI);
}

Selection Statement - IF
Example:
int number = 5;
String result = number == 1 ? a : b;
OR
String result = number == 1 ? satu : number == 2? dua :
bukan dua;

System.out.println(number = + number);

Selection Statement - Switch
Format:
switch ( Expression ) {
case constant_1 : Statement; break;
case constant_2 : Statement; break;
case constant_3 : Statement; break;
case constant_n : Statement; break;
default : Statement; break;
}
Selection Statement - Switch
Explanation:
Cannot have same expression
Default is optional, but bear in mind there can be only
one default
Default doesnt have to be last


Selection Statement - Switch
Example:
int number = 2;
String a = ;
switch (x) {
case 1: a = PT;
case 2: a = GUNATRONIKATAMA;
case 3: a = CIPTA;
case 4: a = JAYA;
case 5: a = LESTARI;
default: a = MAKMUR;
}
Selection Statement - Switch
Example:
int number = 2;
String a = ;
switch (x) {
case 1: a = PT; break;
case 2: a = GUNATRONIKATAMA; break;
case 3: a = CIPTA; break;
case 4: a = JAYA; break;
case 5: a = LESTARI; break;
default: a = MAKMUR; break;
}
Looping Statement - For
Format:
for ( Initial; Test; Increment ) Statement

New Format (since Java 5):
for ( Object object : Array) Statement
Looping Statement - For
Example:
int i;
for (i = 0; i < 10; i++) {
// loop body
}
OR
for (int j = 0; j < 10; j++) {
// loop body
}

Looping Statement - For
Example:
for (int i = 0; i < employees.size(); i++) {
Employee e = employees.get(i);
e.getName();
}
OR
for (Employee e : employees) { // new
e.getName();
}

Looping Statement - For
Infinite loop
for ( ;; ) {

}

Looping Statement - While
Format:
while ( Expression ) Statement

Explanation:
While boolean Expression remain true, the
Statement is executed
If Expression is false on first evaluation, the
Statement will not execute

Looping Statement - While
Example:
int i = 0;
while (i < 10) {
// loop body
i++;
}
Looping Statement Do While
Format:
do Statement while ( Expression )

Explanation:
While boolean Expression remain true, the
Statement is executed
If Expression is false on first evaluation, the
Statement will execute once
Looping Statement Do While
Example:
int i = 0;
do {
// loop body
i++;
} while (i < 10) ;

Looping Statement Continue
Format:
continue;
continue Identifier;

Explanation:
Continue occur only in loops
When a continue statement executed, it will pass to
the next iteration of the loop
Looping Statement - Continue
Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
}
Looping Statement - Continue
Example:
gasi: for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j == 5) continue gasi;
}
}
Looping Statement Break
Format:
break;
break Identifier;

Explanation:
When a break statement executed, it will break or
exit the loop
Looping Statement - Break
Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
}
Looping Statement - Break
Example:
gasi: for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j == 5) break gasi;
}
}
Looping Statement - Break
Example:
for (int i = 0; i < 10; i++) {
System.out.println(i = + i);
for (int j = 0; j < 5; j++) {
System.out.println(j = + j);
if (j == 2) break;
}
if (i == 3) break;
}
Basic of Arrays
Arrays are objects it means array types are reference
types, and your array variable is really a reference to an
array

Here are some ways in which arrays are like objects:
They are objects because the language specification says so
("An object is a class instance or an array", section 4.3.1)
Array types are reference types, just like object types
Arrays are allocated with the "new" operator, similar to
constructors
Arrays are always allocated in the heap part of memory, never in
the stack part of memory. Objects follow the same rule


Basic of Arrays
We can define an Arrays of:
Primitive Data Type
Object

Index of Array ALWAYS start with zero

To get size of Arrays we use array.length
Basic of Arrays
Declare an Arrays:
// Recommended
int[] data;
int[] data1, data2, data3;

// Not recommended
int data[];
int data1[], data2[], data3[];

Basic of Arrays
Initialize an Arrays:
int[] data;
data = new int[10]; // Valid
data = new int[]; // Invalid

int[] data = new int[10]; // Valid
int[10] data = new int[10]; // Invalid
int data[10] = new int[10]; // Invalid
int data[10]; // Invalid





Basic of Arrays
Fill an Arrays:
int[] data = new int[5]; // Valid
data[0] = 5; // OK
data[1] = 6;
data[2] = 7;
data[3] = 8;
data[4] = 9; // OK so far
data[5] = 10; // We have a problem here









Basic of Arrays
Fill an Arrays:
int[] data = {6, 7, 8, 9, 10}; // Valid
int[] data = new int[] {6, 7, 8, 9, 10}; // Valid

int[] data = new int[5] {6, 7, 8, 9, 10}; // Invalid

int[] data = new int[5];
data = {6, 7, 8, 9, 10}; // Invalid

Arrays of Arrays
In other language called Multi Dimension Arrays
The Visual Basic language only has multidimensional
arrays, and only calls them multidimensional arrays
The ANSI C standard says C has what other
languages call arrays of arrays, but it also calls these
multidimensional
The Ada standard explicitly says arrays of arrays and
multidimensional arrays are different. The language
has both
The Pascal standard says arrays of arrays and
multidimensional arrays are the same thing

Arrays of Arrays
Declare an Arrays:
// Recommended
int[][] data;
int[][] data1, data2, data3;

// Not recommended
int data[][];
int data1[][], data2[][], data3[][];

Arrays of Arrays
Initialize an Arrays:
int[][] data;
data = new int[10][10]; // Valid
data = new int[10][]; // Invalid

int[][] data = new int[10][10]; // Valid
int[10][10] data = new int[10][10]; // Invalid
int data[10][10] = new int[10][10]; // Invalid
int data[10][10]; // Invalid





Arrays of Arrays
Fill an Arrays:
int[][] data = new int[3][3];
data[0][0] = 0;
data[0][1] = 1;
data[0][2] = 2;

data[1][0] = 0;
data[1][1] = 1;
data[1][2] = 2;

data[2][0] = 0;
data[2][1] = 1;
data[2][2] = 2;
Arrays of Arrays
Fill an Arrays:
int[][] data = new int[3][];
data[0] = new int[1];
data[0][0] = 0;

data[1] = new int[2];
data[1][0] = 0;
data[1][1] = 1;

data[2] = new int[3];
data[2][0] = 0;
data[2][1] = 1;
data[2][2] = 2;
Arrays of Arrays
Fill an Arrays:
int[][] data = {{0, 1}, {0, 1, 2}, {0, 1, 2, 3}}; // Valid
int[][] data = new int[][] {{0, 1}, {0, 1}}; // Valid

int[][] data = new int[1][2] {{0, 1}}; // Invalid

int[][] data = new int[2][2];
data = {{0, 1}, {1, 2}, {2, 3}}; // Invalid

Thank You

Das könnte Ihnen auch gefallen