Top Banner
Java Language and OOP Part II By Hari Christian
66

02 Java Language And OOP PART II

May 25, 2015

Download

Software

Hari Christian

02 Java Language And OOP PART II
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 02 Java Language And OOP PART II

Java Language and OOP Part II

By Hari Christian

Page 2: 02 Java Language And OOP PART II

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

Page 3: 02 Java Language And OOP PART II

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

Page 4: 02 Java Language And OOP PART II

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

Page 5: 02 Java Language And OOP PART II

Operator

• Example:

int a = 3 * 2;

int b = 3 + t.calculate();

arr[2] = 5;

arr[2+5] = 2 + 5;

Page 6: 02 Java Language And OOP PART II

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

Page 7: 02 Java Language And OOP PART II

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

Page 8: 02 Java Language And OOP PART II

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

Page 9: 02 Java Language And OOP PART II

Operator - Relational

int num1 = 5;

if (num1 < 5) {

System.out.println(“Less than 5”);

}

if (num1 > 5) {

System.out.println(“More than 5”);

}

Page 10: 02 Java Language And OOP PART II

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”);

}

Page 11: 02 Java Language And OOP PART II

Operator - Relational

int num1 = 5;

if (num1 == 5) {

System.out.println(“Equal 5”);

}

if (num1 != 5) {

System.out.println(“Not equal 5”);

}

Page 12: 02 Java Language And OOP PART II

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");

}

Page 13: 02 Java Language And OOP PART II

Operator - Arithmetic

• Arithmetic Operator just like a Math!!!

• 7 Arithmetic Operator:Operator Description

+ Addition

- Substraction

* Multiplication

/ Divide

% Modulus

++ Increment by 1

-- Decrement by 1

Page 14: 02 Java Language And OOP PART II

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++;

Page 15: 02 Java Language And OOP PART II

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;

Page 16: 02 Java Language And OOP PART II

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 */

Page 17: 02 Java Language And OOP PART II

Operator - Logical

• To combine other operator

• 2 Relational Operator:

Operator Description

&& And

|| Or

Page 18: 02 Java Language And OOP PART II

Operator – Logical And

• AND Combination table

Operator 1 Operator 2 Result

False False False

False True False

True False False

True True True

Page 19: 02 Java Language And OOP PART II

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”);

}

Page 20: 02 Java Language And OOP PART II

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”);

}

Page 21: 02 Java Language And OOP PART II

Operator – Logical Or

• OR Combination table

Operator 1 Operator 2 Result

False False False

False True True

True False True

True True True

Page 22: 02 Java Language And OOP PART II

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”);

}

Page 23: 02 Java Language And OOP PART II

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”);

}

Page 24: 02 Java Language And OOP PART II

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

Page 25: 02 Java Language And OOP PART II

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

Page 26: 02 Java Language And OOP PART II

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

Page 27: 02 Java Language And OOP PART II

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

Page 28: 02 Java Language And OOP PART II

Selection Statement - IF

• Format:

if ( Expression ) Statement [ else Statement ]

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

Page 29: 02 Java Language And OOP PART II

Selection Statement - IF

• Example:

boolean valid = false;

if (valid = true) {

System.out.println(“VALID”);

} else {

System.out.println(“TIDAK VALID”);

}

Page 30: 02 Java Language And OOP PART II

Selection Statement - IF

• Example:

boolean valid = false;

if (valid) {

System.out.println(“VALID”);

} else {

System.out.println(“TIDAK VALID”);

}

Page 31: 02 Java Language And OOP PART II

Selection Statement - IF

• Example:

boolean valid = false;

if (valid == true) {

System.out.println(“VALID”);

} else {

System.out.println(“TIDAK VALID”);

}

Page 32: 02 Java Language And OOP PART II

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”);

}

Page 33: 02 Java Language And OOP PART II

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”);

}

Page 34: 02 Java Language And OOP PART II

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);

Page 35: 02 Java Language And OOP PART II

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;

}

Page 36: 02 Java Language And OOP PART II

Selection Statement - Switch

• Explanation: Cannot have same expression Default is optional, but bear in mind there can be only

one default Default doesn’t have to be last

Page 37: 02 Java Language And OOP PART II

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”;

}

Page 38: 02 Java Language And OOP PART II

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;

}

Page 39: 02 Java Language And OOP PART II

Looping Statement - For

• Format:

for ( Initial; Test; Increment ) Statement

• New Format (since Java 5):

for ( Object object : Array) Statement

Page 40: 02 Java Language And OOP PART II

Looping Statement - For

• Example:

int i;

for (i = 0; i < 10; i++) {

// loop body

}

OR

for (int j = 0; j < 10; j++) {

// loop body

}

Page 41: 02 Java Language And OOP PART II

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();

}

Page 42: 02 Java Language And OOP PART II

Looping Statement - For

• Infinite loop

for ( ;; ) {

}

Page 43: 02 Java Language And OOP PART II

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

Page 44: 02 Java Language And OOP PART II

Looping Statement - While

• Example:

int i = 0;

while (i < 10) {

// loop body

i++;

}

Page 45: 02 Java Language And OOP PART II

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

Page 46: 02 Java Language And OOP PART II

Looping Statement – Do While

• Example:

int i = 0;

do {

// loop body

i++;

} while (i < 10) ;

Page 47: 02 Java Language And OOP PART II

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

Page 48: 02 Java Language And OOP PART II

Looping Statement - Continue

• Example:

for (int i = 0; i < 10; i++) {

if (i == 5) {

continue;

}

}

Page 49: 02 Java Language And OOP PART II

Looping Statement - Continue

• Example:

gasi: for (int i = 0; i < 10; i++) {

for (int j = 0; j < 10; j++) {

if (j == 5) continue gasi;}

}

Page 50: 02 Java Language And OOP PART II

Looping Statement – Break

• Format:

break;

break Identifier;

• Explanation: When a break statement executed, it will break or

exit the loop

Page 51: 02 Java Language And OOP PART II

Looping Statement - Break

• Example:

for (int i = 0; i < 10; i++) {

if (i == 5) {

break;

}

}

Page 52: 02 Java Language And OOP PART II

Looping Statement - Break

• Example:

gasi: for (int i = 0; i < 10; i++) {

for (int j = 0; j < 10; j++) {

if (j == 5) break gasi;}

}

Page 53: 02 Java Language And OOP PART II

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;

}

Page 54: 02 Java Language And OOP PART II

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

Page 55: 02 Java Language And OOP PART II

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

Page 56: 02 Java Language And OOP PART II

Basic of Arrays

• Declare an Arrays:

// Recommended

int[] data;

int[] data1, data2, data3;

// Not recommended

int data[];

int data1[], data2[], data3[];

Page 57: 02 Java Language And OOP PART II

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

Page 58: 02 Java Language And OOP PART II

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

Page 59: 02 Java Language And OOP PART II

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

Page 60: 02 Java Language And OOP PART II

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

Page 61: 02 Java Language And OOP PART II

Arrays of Arrays

• Declare an Arrays:

// Recommended

int[][] data;

int[][] data1, data2, data3;

// Not recommended

int data[][];

int data1[][], data2[][], data3[][];

Page 62: 02 Java Language And OOP PART II

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

Page 63: 02 Java Language And OOP PART II

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;

Page 64: 02 Java Language And OOP PART II

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;

Page 65: 02 Java Language And OOP PART II

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

Page 66: 02 Java Language And OOP PART II

Thank You