SlideShare uma empresa Scribd logo
1 de 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 doesn’t 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

Mais conteúdo relacionado

Mais procurados

Clone Refactoring with Lambda Expressions
Clone Refactoring with Lambda ExpressionsClone Refactoring with Lambda Expressions
Clone Refactoring with Lambda ExpressionsNikolaos Tsantalis
 
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions Ganesh Samarthyam
 
Functional Operations - Susan Potter
Functional Operations - Susan PotterFunctional Operations - Susan Potter
Functional Operations - Susan Potterdistributed matters
 
Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)Trisha Gee
 
Writing beautiful code with Java 8
Writing beautiful code with Java 8Writing beautiful code with Java 8
Writing beautiful code with Java 8Sergiu Mircea Indrie
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Nishan Barot
 
The Ring programming language version 1.3 book - Part 23 of 88
The Ring programming language version 1.3 book - Part 23 of 88The Ring programming language version 1.3 book - Part 23 of 88
The Ring programming language version 1.3 book - Part 23 of 88Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 32 of 185
The Ring programming language version 1.5.4 book - Part 32 of 185The Ring programming language version 1.5.4 book - Part 32 of 185
The Ring programming language version 1.5.4 book - Part 32 of 185Mahmoud Samir Fayed
 
OCP Java SE 8 Exam - Sample Questions - Generics and Collections
OCP Java SE 8 Exam - Sample Questions - Generics and CollectionsOCP Java SE 8 Exam - Sample Questions - Generics and Collections
OCP Java SE 8 Exam - Sample Questions - Generics and CollectionsGanesh Samarthyam
 
Is java8 a true functional programming language
Is java8 a true functional programming languageIs java8 a true functional programming language
Is java8 a true functional programming languageSQLI
 

Mais procurados (18)

Clone Refactoring with Lambda Expressions
Clone Refactoring with Lambda ExpressionsClone Refactoring with Lambda Expressions
Clone Refactoring with Lambda Expressions
 
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
 
Functional Operations - Susan Potter
Functional Operations - Susan PotterFunctional Operations - Susan Potter
Functional Operations - Susan Potter
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Writing beautiful code with Java 8
Writing beautiful code with Java 8Writing beautiful code with Java 8
Writing beautiful code with Java 8
 
Java programming-examples
Java programming-examplesJava programming-examples
Java programming-examples
 
Java 8: the good parts!
Java 8: the good parts!Java 8: the good parts!
Java 8: the good parts!
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.
 
The Ring programming language version 1.3 book - Part 23 of 88
The Ring programming language version 1.3 book - Part 23 of 88The Ring programming language version 1.3 book - Part 23 of 88
The Ring programming language version 1.3 book - Part 23 of 88
 
TDD Training
TDD TrainingTDD Training
TDD Training
 
The Ring programming language version 1.5.4 book - Part 32 of 185
The Ring programming language version 1.5.4 book - Part 32 of 185The Ring programming language version 1.5.4 book - Part 32 of 185
The Ring programming language version 1.5.4 book - Part 32 of 185
 
Pattern Matching in Java 14
Pattern Matching in Java 14Pattern Matching in Java 14
Pattern Matching in Java 14
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
Lazy java
Lazy javaLazy java
Lazy java
 
OCP Java SE 8 Exam - Sample Questions - Generics and Collections
OCP Java SE 8 Exam - Sample Questions - Generics and CollectionsOCP Java SE 8 Exam - Sample Questions - Generics and Collections
OCP Java SE 8 Exam - Sample Questions - Generics and Collections
 
Is java8 a true functional programming language
Is java8 a true functional programming languageIs java8 a true functional programming language
Is java8 a true functional programming language
 

Semelhante a 02 Java Language And OOP PART II (20)

Introduction to c part -1
Introduction to c   part -1Introduction to c   part -1
Introduction to c part -1
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
 
Operators
OperatorsOperators
Operators
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Operators and Expressions in C++
Operators and Expressions in C++Operators and Expressions in C++
Operators and Expressions in C++
 
C++
C++ C++
C++
 
Oop using JAVA
Oop using JAVAOop using JAVA
Oop using JAVA
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
Looping
LoopingLooping
Looping
 
C++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPEC++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPE
 
05 operators
05   operators05   operators
05 operators
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Java unit 3
Java unit 3Java unit 3
Java unit 3
 
Fekra c++ Course #2
Fekra c++ Course #2Fekra c++ Course #2
Fekra c++ Course #2
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
java switch
java switchjava switch
java switch
 

Mais de Hari Christian

06 Java Language And OOP Part VI
06 Java Language And OOP Part VI06 Java Language And OOP Part VI
06 Java Language And OOP Part VIHari Christian
 
01 Java Language And OOP Part I LAB
01 Java Language And OOP Part I LAB01 Java Language And OOP Part I LAB
01 Java Language And OOP Part I LABHari Christian
 
05 Java Language And OOP Part V
05 Java Language And OOP Part V05 Java Language And OOP Part V
05 Java Language And OOP Part VHari Christian
 
03 Java Language And OOP Part III
03 Java Language And OOP Part III03 Java Language And OOP Part III
03 Java Language And OOP Part IIIHari Christian
 
04 Java Language And OOP Part IV
04 Java Language And OOP Part IV04 Java Language And OOP Part IV
04 Java Language And OOP Part IVHari Christian
 
01 Java Language And OOP PART I
01 Java Language And OOP PART I01 Java Language And OOP PART I
01 Java Language And OOP PART IHari Christian
 
MM38 kelas B Six Sigma
MM38 kelas B Six SigmaMM38 kelas B Six Sigma
MM38 kelas B Six SigmaHari Christian
 

Mais de Hari Christian (9)

HARI CV AND RESUME
HARI CV AND RESUMEHARI CV AND RESUME
HARI CV AND RESUME
 
06 Java Language And OOP Part VI
06 Java Language And OOP Part VI06 Java Language And OOP Part VI
06 Java Language And OOP Part VI
 
01 Java Language And OOP Part I LAB
01 Java Language And OOP Part I LAB01 Java Language And OOP Part I LAB
01 Java Language And OOP Part I LAB
 
05 Java Language And OOP Part V
05 Java Language And OOP Part V05 Java Language And OOP Part V
05 Java Language And OOP Part V
 
03 Java Language And OOP Part III
03 Java Language And OOP Part III03 Java Language And OOP Part III
03 Java Language And OOP Part III
 
04 Java Language And OOP Part IV
04 Java Language And OOP Part IV04 Java Language And OOP Part IV
04 Java Language And OOP Part IV
 
DB2 Sql Query
DB2 Sql QueryDB2 Sql Query
DB2 Sql Query
 
01 Java Language And OOP PART I
01 Java Language And OOP PART I01 Java Language And OOP PART I
01 Java Language And OOP PART I
 
MM38 kelas B Six Sigma
MM38 kelas B Six SigmaMM38 kelas B Six Sigma
MM38 kelas B Six Sigma
 

Último

OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationShrmpro
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 

Último (20)

OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 

02 Java Language And OOP PART II

  • 1. Java Language and OOP Part II By Hari Christian
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. Operator • Example: int a = 3 * 2; int b = 3 + t.calculate(); arr[2] = 5; arr[2+5] = 2 + 5;
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. Operator - Relational int num1 = 5; if (num1 < 5) { System.out.println(“Less than 5”); } if (num1 > 5) { System.out.println(“More than 5”); }
  • 10. 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”); }
  • 11. Operator - Relational int num1 = 5; if (num1 == 5) { System.out.println(“Equal 5”); } if (num1 != 5) { System.out.println(“Not equal 5”); }
  • 12. 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"); }
  • 13. Operator - Arithmetic • Arithmetic Operator just like a Math!!! • 7 Arithmetic Operator: Operator Description + Addition - Substraction * Multiplication / Divide % Modulus ++ Increment by 1 -- Decrement by 1
  • 14. 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++;
  • 15. 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;
  • 16. 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 */
  • 17. Operator - Logical • To combine other operator • 2 Relational Operator: Operator Description && And || Or
  • 18. Operator – Logical And • AND Combination table Operator 1 Operator 2 Result False False False False True False True False False True True True
  • 19. 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”); }
  • 20. 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”); }
  • 21. Operator – Logical Or • OR Combination table Operator 1 Operator 2 Result False False False False True True True False True True True True
  • 22. 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”); }
  • 23. 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”); }
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. Selection Statement - IF • Format: if ( Expression ) Statement [ else Statement ] • Explanation:  Expression must have boolean type  Using { } to make more than 1 statement
  • 29. Selection Statement - IF • Example: boolean valid = false; if (valid = true) { System.out.println(“VALID”); } else { System.out.println(“TIDAK VALID”); }
  • 30. Selection Statement - IF • Example: boolean valid = false; if (valid) { System.out.println(“VALID”); } else { System.out.println(“TIDAK VALID”); }
  • 31. Selection Statement - IF • Example: boolean valid = false; if (valid == true) { System.out.println(“VALID”); } else { System.out.println(“TIDAK VALID”); }
  • 32. 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”); }
  • 33. 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”); }
  • 34. 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);
  • 35. 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; }
  • 36. 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
  • 37. 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”; }
  • 38. 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; }
  • 39. Looping Statement - For • Format: for ( Initial; Test; Increment ) Statement • New Format (since Java 5): for ( Object object : Array) Statement
  • 40. Looping Statement - For • Example: int i; for (i = 0; i < 10; i++) { // loop body } OR for (int j = 0; j < 10; j++) { // loop body }
  • 41. 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(); }
  • 42. Looping Statement - For • Infinite loop for ( ;; ) { }
  • 43. 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
  • 44. Looping Statement - While • Example: int i = 0; while (i < 10) { // loop body i++; }
  • 45. 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
  • 46. Looping Statement – Do While • Example: int i = 0; do { // loop body i++; } while (i < 10) ;
  • 47. 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
  • 48. Looping Statement - Continue • Example: for (int i = 0; i < 10; i++) { if (i == 5) { continue; } }
  • 49. Looping Statement - Continue • Example: gasi: for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (j == 5) continue gasi; } }
  • 50. Looping Statement – Break • Format: break; break Identifier; • Explanation:  When a break statement executed, it will break or exit the loop
  • 51. Looping Statement - Break • Example: for (int i = 0; i < 10; i++) { if (i == 5) { break; } }
  • 52. Looping Statement - Break • Example: gasi: for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (j == 5) break gasi; } }
  • 53. 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; }
  • 54. 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
  • 55. 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
  • 56. Basic of Arrays • Declare an Arrays: // Recommended int[] data; int[] data1, data2, data3; // Not recommended int data[]; int data1[], data2[], data3[];
  • 57. 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
  • 58. 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
  • 59. 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
  • 60. 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
  • 61. Arrays of Arrays • Declare an Arrays: // Recommended int[][] data; int[][] data1, data2, data3; // Not recommended int data[][]; int data1[][], data2[][], data3[][];
  • 62. 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
  • 63. 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;
  • 64. 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;
  • 65. 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