3 Basic I_O, Operators 16-07-2024
3 Basic I_O, Operators 16-07-2024
Topic/Course
Sub-Topic (Example: name of college)
Operators are used to perform operations on variables and
values.
Topic/Course operators
Sub-Topic (Example: name of college)
• Unary operators
X=Y–1
• Binary operators
• Ternary operators
operands
Operators in Java
• Unary operators • Logical operators
• Arithmetic operators • Ternary operators
• Relational operators • Assignment
• Bitwise operators operators
• instanceof operator
Unary operators
• Unary operator performs operation on only one operand.
• They are used to increment, decrement or negate a
value.
Unary operators
Operator Description Example
++x(prefix)
Increases the value of a variable
++ by 1
x++(postfix)
--x(prefix)
Decreases the value of a variable
-- by 1
x--(postfix)
+ Used for giving positive values +x
~ Negating an expression ~x
output
15
5
50
2
0
Relational operators
• Relational/Comparison operators are used to compare two
values.
• Can be applied to the integer types, long, int, short, char, and
byte.
Bitwise operators
Operator Description Example
& Returns bit by bit AND of input values x&y
<< shifts the bits of the number to the left and fills 0 on voids left as a result x << 2
>> shifts the bits of the number to the right and fills 0 on voids left as a result x >> 2
>>> shifts the bits of the number to the right and fills 0 on voids left as a result x >>>2
1 public class Main { output
2 public static void main(String args[]){
3 int a = 10;
4 int b = 20; 0
5 System.out.println(a&b); 30
6 System.out.println(a|b); -11
7 System.out.println(~a); 40
10 System.out.println(a<<2); 2
11 System.out.println(a>>2); 2
12 System.out.println(a>>>2);
13 }
14 }
Logical operators
• The logical operators || (conditional-OR) and && (conditional-
AND) operates on boolean expressions.
• The second condition is not evaluated if the first one is false, i.e.
it has a short-circuiting effect.
Logical operators
Operator Description Example
&& Returns true if both statements are true x < 5 && x < 10
! Reverse the result, returns false if the result is true !(x < 5 && x < 10)
1 public class Test { output
2 public static void main(String args[]){
3 boolean a = true;
4 boolean b = false; false
5 System.out.println(a&&b); true
6 System.out.println(a||b); true
7 System.out.println(!(a && b));
10 }
11 }
Ternary operator
• Ternary/Conditional operator consists of three operands and is
used to evaluate Boolean expressions.
• Ternary operator is a shorthand version of if-else
statement.
• It has three operands and hence the name ternary.
1 class OperatorExample{ output
2 public static void main(String args[]){
3 int a=2;
4 int b=5; 2
5 int min=(a<b)?a:b;
6 System.out.println(min);
7 }
10 }
Assignment operators
• Assignment operator is used to assign a value to any variable.
+= Adds right operand to the left operand and assign the result to left operand. C += A
Subtracts right operand from the left operand and assign the result to left
-= C -= A
operand.
Multiplies right operand with the left operand and assign the result to left C *= A
*=
operand.
Divides left operand with the right operand and assign the result to left C /= A
/=
operand.
%= Takes modulus using two operands and assign the result to left operand. C %= A
Assignment operators
Operator Description Example
<<= Left shift AND assignment operator C <<= 2
1. Nothing
2. Error
1 // Predict the output
2 class Test {
3 public static void main(String args[]) {
4 System.out.println(10 + 20 + “Face");
5 System.out.println(“Face" + 10 + 20);
6 }
7 }
OUTPUT
1. 30Face Face30
2. 1020Face Face1020
3. 30Face Face1020
4. 1020Face Face30
1 // Predict the output
2 class Test
3 {
4 public static void main(String args[])
5 {
6 String s1 = “FACE";
7 String s2 = “FACE";
8 System.out.println("s1 == s2 is:" + s1 == s2);
9 }
10 }
OUTPUT
1. true
2. false
3. compiler error
4. throws an exception
THANK YOU