Operators
1. Assignment
int i = 5;
i = i + 3;
i += 3;
2. Promotion
int-den daha kicik tiplerde edilen arifmetik emeliyyatlar daima int qaytarir.
int i = 5;
double d = 2.3D;
i = i + d; // Compile time error
i = (int) (i + d);
byte b1 = 8;
byte b2 = 5;
byte b3 = b1 + b2; // Because sum is int
byte b3 = (byte) (b1 + b2);
Ashagidaki numunelerde cast-a ehtiyac yoxdur, cunki cast avtomatik bash verir
int i = 5;
double d = 2.3D;
i += d;
3. Pre increment, post increment, pre decrement, post decrement
int i = 10;
i++;
System.out.println(i);
i--;
System.out.println(i);
++i;
System.out.println(i);
--i;
System.out.println(i);
int i = 10;
System.out.println(i);
System.out.println(i++);
System.out.println(++i);
int i = 10;
int j = i++;
System.out.println(i);
System.out.println(j);
int i = 10;
int j = i++;
System.out.println(i);
System.out.println(j);
j = --i;
System.out.println(i);
System.out.println(j);
int i = 10;
int j = ++i;
System.out.println(i);
System.out.println(j);
weird behaviour
int i = 0;
System.out.println(i);
i = ++i;
System.out.println(i);
i = ++i + 2;
System.out.println(i);
int i = 0;
System.out.println(i);
i = i++;
System.out.println(i);
i = i++ + 2;
System.out.println(i);
4. Unary Logical operator
boolean b1 = true;
b1 = !b1;
System.out.println(b1);
boolean a = true;
boolean b = false;
boolean c = a == b;
System.out.println(c);
5. Conditional operators
https://press.rebus.community/programmingfundamentals/chapter/logical-operators/
if((getOne() == 2) & (getTwo() == 2)) {
System.out.println("value1 is 1 and value2 is 2");
}
if((getOne() == 2) | (getTwo() == 2)) {
System.out.println("value1 is 1 and value2 is 2");
}
}
public static int getOne() {
System.out.println("in getOne method");
return 1;
}
public static int getTwo() {
System.out.println("in getTwo method");
return 2;
}
6. Ternary operator
int a = 1;
int b = 2;
int min = (a < b) ? a : b;
System.out.println(min);
int a = 1;
int b = 2;
System.out.println((a < b) ? "kicikdir" : "boyukdur");
7. Bitwise operators
bitwise and
int a = 4; // 0100
int b = 8; // 1000
int c = 4 & 8;
System.out.println(c);
bitwise or
int a = 4; // 0100
int b = 8; // 1000
int c = a | b;
System.out.println(c);
bitwise xor
int a = 4; // 0100
int b = 8; // 1000
int c = a ^ b;
System.out.println(c);
bitwise complementary
int a = 4; // 0100
int b = ~a;
System.out.println(b);
left shift and right shift
-----------------------------------------------------------------------------------------------------------------------------
Evaluation Order
int a = 6;
int b = 5;
int c = 10;
float result = a + ++b * c / a * b;
System.out.println(result);
Комментарии
Отправить комментарий