Java p2
Java p2
1 Introduction
Operator in Java:-
Operators in Java are the symbols used for performing specific operations in Java.
Operators make tasks like addition, multiplication, etc which look easy although the
implementation of these tasks is quite complex.
There are multiple types of operators in Java all are mentioned below:
1. Arithmetic Operators +, - , * , /, %
2. Unary Operators ++ , --, - ,+
3. Assignment Operator = , += , -= , *= , /= , %=
4. Relational Operators < ,> ,<= ,>=, == , !=
5. Logical Operators && , || , !
6. Ternary Operator ? :
7. Bitwise Operators &,|,~,^
8. Shift Operators >> , <<
9. Instance of operator instanceof
1.1 Arithmetic operators
Class Arith {
Public static void main (String[] args) {
// Arithmetic operators
Int a = 20;
Int b = 3;
System.out.println(“a + b = “ + (a + b));
System.out.println(“a – b = “ + (a – b));
System.out.println(“a * b = “ + (a * b));
System.out.println(“a / b = “ + (a / b));
System.out.println(“a % b = “ + (a % b));
}
}
//Output
a + b = 33
a - b = 17
a * b = 60
a/b=6
a%b=2
Class Assig {
Public static void main(String[] args)
{
Int f = 7;
System.out.println(“f += 3: “ + (f += 3));
System.out.println(“f -= 2: “ + (f -= 2));
System.out.println(“f *= 4: “ + (f *= 4));
System.out.println(“f /= 3: “ + (f /= 3));
System.out.println(“f %= 2: “ + (f %= 2));
System.out.println(“f &= 0b1010: “ + (f &= 0b1010));
System.out.println(“f |= 0b1100: “ + (f |= 0b1100));
}
//Output
F += 3: 10
F -= 2: 8
F *= 4: 32
F /= 3: 10
F %= 2: 0
F &= 0b1010: 0
F |= 0b1100: 12