Dp4 Java Operators
Dp4 Java Operators
O DH
I J
NST
class Logical {
public static void main (String s[]){
boolean a = true; OUTPUT
boolean b = false;
System.out.println("a && b: " + (a && b)); a && b: false
System.out.println("a || b: " + (a || b)); a || b: true
System.out.println("!a: " + (!a)); !a: false
}
}
4. Relational Operators
class Relational {
public static void main (String s[]){
int var1 = 5, var2 = 10, var3 = 5;
System.out.println("var1 == var2: " + (var1 == var2)); OUTPUT
System.out.println("var1 == var3: " + (var1 == var3));
System.out.println("var1 != var2: " + (var1 != var2)); var1 == var2: false
System.out.println("var1 != var3: " + (var1 != var3)); var1 == var3: true
System.out.println("var1 > var2: " + (var1 > var2)); var1 != var2: true
System.out.println("var2 > var3: " + (var2 > var3)); var1 != var3: false
} var1 > var2: false
} var2 > var3: true
5. Unary Operators
unary operators perform operations on a single operand.
class Logicalnotunaruy {
public static void main (String
s[]){
boolean condition = true; OUTPUT
System.out.println("condition is: condition is: true
" + condition); Now condition is: false
System.out.println("Now condition
is: " + !condition);}}
6.Bitwise
Operators
Bitwise operators are used on (binary) numbers:
Compares corresponding bits of two integers. If both bits are 1,
Bitwise AND (&) the result bit is 1; otherwise, it’s 0.
Sets each bit to 1 if both bits are 1
Compares corresponding bits and returns 1 if either operand bit
Bitwise OR (|) is 1.
Sets each bit to 1 if one of two bits is 1
Compares corresponding bits and returns 1 if exactly one
Bitwise XOR (^) operand bit is 1.
Sets each bit to 1 if only one of two bits is 1
Inverts individual bits.
Bitwise NOT (~) Inverts all the bits
Bitwise AND Bitwise XOR
(&) (^)
Decimal Binary Decimal Binary
17= 1 0 0 0 1 17= 1 0 0 0 1
3= 0 0 0 1 1 3= 0 0 0 1 1
1= 1 18= 1 0 0 1 0
Bitwise NOT
Bitwise OR (|)
(~)
Decimal Binary Decimal Binary
17= 0 0 0 0 0 0 0 0 0 0 0 1 0 0
17= 1 0 0 0 1 0 1
0 becomes 1 and 1 1 1 1 1 1 1 1 1 1 1 0 1 1
3= 0 0 0 1 1
1 becomes 0 1 0
19= 1 0 0 1 1 -18= 1 1 1 1 1 1 1 1 1 1 1 0 1 1
1 0
Example
class Bitwise {
public static void main(String[] args) {
int number1 = 12, number2 = 25;
int result = number1 | number2;
int result1 = number1 & number2; OUTPUT
int result2 = number1 ^ number2;
int result3 = ~number1; 29
System.out.println(result); 8
System.out.println(result1); 21
System.out.println(result2); -13
System.out.println(result3);
}
}
7. Ternary Operators
Ternary operators, also known as conditional operators, are used in
programming to evaluate a condition and return one of two values
depending on whether the condition is true or false. They are called
“ternary” because they take three operands: the condition, the result for
true, and the result for false.
OUTPU
T
The number 13 is
Odd.
8. Shift Operators
Ans:
c
2. Which operator is used for performing
logical AND in Java?
a) &&
b) ||
c) !
d) &
Ans: a
3 Which operator is used to increment a
variable by one in Java?
a) *=
b) --
c) +=
d) ++
Ans: d
4 Which operator is used to invert the
value of a boolean variable?
•A) ~
•B) !
•C) -
•D) ^
Ans: b