• Unary operator
• Arithmetic operator
• Shift operator: <<, >>, dan >>>
• Comparison operator
• Bitwise operator: &, ^, dan |.
• Short – Circuit operator
• Conditional operator : ?
• Assignment operator
• Operator lain : new, instanceof
• Urutan pemrosesan
Macam operator
• Terdapat 3 macam operator:
– Operator unary
hanya memerlukan satu operator
contoh :(menambah nilai pada operand dengan 1)
y = 3; ++y ; (y=4)
– Operator binary
Memerlukan dua operand.
• Operator aritmatika
• Operator relas
• Bitwise Logical Operators
• Operator instanceof
• Operator shift
• Operator Short-Circuit Logical
• Shortcut Assignment Operators
Contoh : (menambah nilai dari dua operand)
a=2;b=3; a = a+b; (a=5)
– Operator ternary
memerlukan tiga operand. Hanya mempunyai satu operator yaitu ?:
Operator Unary
1. X = -3;
2. Y = +3;
3. Z = -(Y+6);
Operator Unary
• The Bitwise Inversion Operator: ~
– converting all the 1 bits in a binary value to 0s and all
the 0 bits to 1s.
Example:
00001111 11110000
• Cara:
1. Tulis biner bilangan positifnya
2. Dikurangi dengan 1
2. ~(hasil)
• Misal: Bagaimana representasi biner untuk bilangan –5 ?
• Cara:
1. Lakukan negasi terhadap bilangan biner
tersebut
2. Ditambah dengan 1
• Misal : 1111 …. 1111 1011
• Tipe data (char, byte, short, int, and long) dibagi dengan
0 akan menyebabkan ArithmeticException.
– 1. int x = 2;
– 2. int y =0;
– 3. int z = x/y;
• Untuk tipe data float dan double pembagian dengan nol
tidak membangkitkan error tapi menghasilkan infinity
(IPOSITIVE_INFINITY or NEGATIVE_INFINITY).
• Mencari akar dari bilangan negatif (float atau double)
akan membangkitkan nilai NaN (Not a Number), tapi
bukan exception.
Nan
• In general, an NaN value indicates that the calculation
has no meaningful result in ordinary arithmetic.
• Two NaN values are defined in the java.lang package:
Float.NaN, and Double.NaN.
• Because NaN means not a sensible value, all the
following value of the double variable x, including NaN:
– x < Double.NaN
– x <= Double.NaN
– x > Double.NaN
– x >= Double.NaN
– x == Double.NaN
• As a corollary to the preceding example, consider
– double x = 7.0/0.0;
– x != Double.NaN (return true)
Operator Binary (Operator Modulo : %)
Example:
x = 7 % 4; //so x = 3
y = 7.6 % 2.9; //so y = 1.8
Contoh:
Operator Binary (Operator relasi)
• Output
Operator Binary (Equality operators)
• Operator equality
== equals
!= not equals
• Terdapat 4 tipe yang dapat digunakan :
– Angka (desimal atau pecahan)
– Karakter
– Boolean
– Object
Equality untuk primitif
class 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);
}
}
Operator Binary (Operator relasi)
int p = 9;
int q = 65;
int r = 12;
float f = 9.0f;
char c = ‘A’;
import 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);
}
}
Operator instanceof
• Hasil:
Is b a Button? true
Is b a Component? true
• Shift operator:
– << : left shift
– >> : sign right shift
– >>> : unsigned right shift
• Fundamentals of Shifting
– moving the bit pattern left or right.
– 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 pada
posisi terkiri yang akan digeser, jika nilainya :
– 1 negatif, maka nilai baru adalah 1
– 0 positif, maka nilai baru adalah 0
Operator Binary
The Shift Operators
Operator >>
Operator >>>
Operator Binary
(Bitwise Logical Operators)
|
^
^
The Short-Circuit Logical Operators
• Output
• x=3y=4
Shortcut Assignment Operators
x=x+y;
• Dapat disingkat x += y;
Shortcut Assignment Operators
• x = x + 2 * 5;
• Disingkat x+=2*5
• x = (x * 2) + 5; disingkat x*=2+5
• Ekspresi pada bagian kanan = selalu
dievaluasi terlebih dahulu
The Conditional Operator: ?:
• Example:
a = x ? b : c;
• Aturan:
– Tipe data b, c dan a sebaiknya sama. Jika tidak sama? Terjadi
promosi
– Tipe ekspresi x harus boolean
– Contoh nilai x (6>7)
– Jika ekspresi x benar maka akan menghasilkan b
– Jika ekspresi x salah maka akan menghasilkan c
Operators Precedence
Operators Precedence (cont.)