O
BJECT
O
RIENTED
O
BJECT
O
RIENTED
P
ROGRAMMING
Day 3 :
TOPIK
M b d k t i t d l l i b l
| Membedakan antara instance dan local variabel.
| Urutan pemrosesan
| Unary operator
| Unary operator
| Arithmetic operator
| Shift operator: <<, >>, dan >>>
| Comparison operator
| Bitwise operator: &, ^, dan |.
Sh t Ci it t
| Short β Circuit operator
| Conditional operator : ?
| Assignment operator
| Assignment operator
E
VALUATIONO
RDER1 i t [] { 4 4 }
1. int [] a = { 4, 4 }; 2. int b = 1;
3. a[b] = b = 0; 3. a[b] b 0;
Note: untuk assignment berlaku
aturan asosiatif Γ dari kanan ke
aturan asosiatif Γ dari kanan ke
kiri.
1. a[b] Γ a[1] 2. b = 0
T
HEU
NARYO
PERATORS| Dibutuhkan hanya satu operan.
1. Operator increment dan decrement : ++ dan
-2. Operator unary plus dan minus : + dan β
3. Operator bitwise inversion : ~
4. Operator boolean complement : !
-T
HEU
NARYO
PERATORS: +
DAN-1. X = -3;
2 Y + 3; 2. Y = + 3;
T
HEU
NARYO
PERATORSTh Bit i I i O t
| The Bitwise Inversion Operator: ~
y converting all the 1 bits in a binary value to 0s and all the 0 bits to 1s.
E l
Example:
00001111 Γ 11110000
| The Boolean Complement Operator: !
| The Boolean Complement Operator: !
y inverts the value of a boolean expression.
Example:
Γ
M
ENCARI NILAI BINER SUATU BILANGAN NEGATIF| Tulis biner positifnya
| Kurangi dengan 1
| Kurangi dengan 1
M
ENCARI NILAI DESIMAL SUATU BILANGAN BINER NEGATIF| Negasikan bilangan biner tersebut
T
HEU
NARYO
PERATORS:
CASTΓ
(
TYPE)
| Casting digunakan untuk melakukan konversi tipe secara eksplisit ke dalam type baru yang ada dalam tanda ()
dalam tanda ().
| Akan dilakukan pengecekan tipe terlebih dahulu.
| Contoh:
| Contoh:
T
HEU
NARYO
PERATORS:
CASTΓ
(
TYPE)
| Bisa diaplikasikan pada tipe obyek.
1. Vector v = new Vector();
2. v.add(βHelloβ);
T
HEA
RITHMETICO
PERATORS| The Multiplication and Division Operators: * and /p p
y multiply or divide two integers, the result will be
calculated using integer arithmetic in either int or long representation.
y Issues:
| Loses precision. int x = 7; int y = 4;
int result = x/ y;
| The result will be bigger than the maximum number
(overflow) (overflow)
byte x = 64; byte y = 4;
T
HEM
ODULOO
PERATOR: %
| Adalah sisa pembagian
| Bisa diaplikasikan pada:
y Bilangan integer
y Bilangan floating - point
Example:
7 % 4 // 3
x = 7 % 4; //so x = 3
T
HE
A
DDITION AND
S
UBTRACTION
O
PERATORS
: +
AND
-| Digunakan untuk melakukan operasi penambahan dan pengurangan.
C t ti Γ + Γ bi j di k t k
A
RITHMETIC
E
RROR
C
ONDITIONS
| Integer division by zero ( ArithmeticException)
| Floating-point calculations represent l i th IEEE 754 i fi it i
range values using the IEEE 754 infinity, minus infinity, and Not a Number (NaN) values.
| Overflow
T
HES
HIFTO
PERATORS:
| Shift operator:p
y << : left shift
y >> : sign right shift
y >>> : unsigned right shift
y >>> : unsigned right shift
| Fundamentals of Shifting
y moving the bit pattern left or right.
li d f i l l
y applied to arguments of integral types only.
| Pada operator << dan >>>: Nilai bit yang baru adalah 0
| Pada operator >> : Nilai bit yang baru
tergantung pada bit yang akan digeser, jika nilainya :
nilainya :
y 1 Γ negatif, maka nilai baru adalah 1
T
HEC
OMPARISONO
PERATORS| Menghasilakn boolean result
| Menghasilakn boolean result.
| Yang termasuk comparison operator:
y Ordinal comparison: < <= > >=
y Ordinal comparison: <, <=, >, >=
y The instanceof Operator
Tests the class of an object at runtime. Tests the class of an object at runtime.
y The Equality Comparison Operators: == and !=
Test for equality and inequality, respectively, returning a b l l
O
RDINAL COMPARISONi t 9
int p = 9; int q = 65; int r = 12; int r 12;
float f = 9.0f; char c = βAβ;
Berikut ini akan menghasilkan true: <
O
PERATOR INSTANCEOFβ’
Operator instance of digunakan untuk mengecek
β’Operator instance of digunakan untuk mengecek
class suatu obyek.
O
PERATOR INSTANCEOFβ’ Hasil:
Is b a Button? true
Is b a Component? true
β’ Argumen sebelah kiri adalah object reference expression.
E
QUALITY OPERATORSy Equality can be tested with the operators equals and not equals:
= = Γ l
β¦ = = Γ equals
β¦ != Γ not equals
y There are four different types of entities that can
y There are four different types of entities that can be tested:
β¦ Numbers
β¦ Characters
β¦ Boolean primitives
E
QUALITY FORP
RIMITIVESclass ComparePrimitives{
public static void main(String [] args) { System.out.println(βaβ ==βaβ);
System.out.println(βaβ ==βbβ); System.out.println(5 != 6);
System.out.println(5.0 == 5L); System.out.println(true==false); }
E
QUALITY FORR
EFERENCEV
ARIABLESimport java.awt.Button; class CompareReference {
public static void main(String [] args) { Button a = new Button(βExitβ);
Button b = new Button(βExitβ); Button c = a;
System.out.println(a==b); System.out.println(a==c); }
T
HEB
ITWISEO
PERATORS:
&
, ^,
AND|
P id l i l AND OR d XOR ti
T
HE
S
HORT
-C
IRCUIT
L
OGICAL
O
PERATORS
O t && d ||
y Operators && and ||
y Applicable only to boolean values and not integral types.
y For an AND operation, if one operand is false, the result p , p , is false, without regard to the other operand.
y For an OR operation, if one operand is true, the result is true, without regard to the other operand., g p
|Jadi, untuk nilai boolean x:
y false && X = false
T
HEC
ONDITIONALO
PERATOR:
?:
| known as the ternary operator
| known as the ternary operator
| takes three operands
| code simple conditions (if/else) into a single
| code simple conditions (if/else) into a single expression.
| Example:
a = x ? b : c;
| Aturan:
y Tipe data b dan c harus sama dengan tipe data a
y Tipe data b dan c harus sama dengan tipe data a
y Tipe ekspresi x harus boolean
y Jika ekspresi x benar maka akan menghasilkan b
T
HEA
SSIGNMENTO
PERATORS| set the value of a variable or expression to a new value.
E l
| Example:
1. byte x = 2;
2 3
2. x += 3;