Operators:In Java, operators are special symbols used to perform operations on variables and values. Java provides a rich set of operators categorized into several types:
🡪Arithmetic operator[+,-,*,/,%]
🡪Assignment operator
Relational operator
Logical operator
Increment/Decrement
Bitwise operator
Conditional operator/ternary operator
Assignment operator
=, +=, -=, *=, /=, %=
✅ Arithmetic Operators
Used for basic mathematical operations.
Operator | Description | Example (a=10 , b=5 ) |
Result |
---|---|---|---|
+ |
Addition | a + b |
15 |
- |
Subtraction | a - b |
5 |
* |
Multiplication | a * b |
50 |
/ |
Division | a / b |
2 |
% |
Modulus (Remainder) | a % b |
0 |
class Assignment
{
public static void main(String args[])
{
int a=10;
a+=10; //a=a+10
System.out.println(“value of a=”+a);
a-=10; //a=a-10
System.out.println(“value of a=”+a);
}
}
Relational operator
==, >, <, >=, <=, !=
✅ Relational (Comparison) Operators
Used to compare two values.
Operator | Description | Example | Result |
---|---|---|---|
== |
Equal to | a == b |
false |
!= |
Not equal to | a != b |
true |
> |
Greater than | a > b |
true |
< |
Less than | a < b |
false |
>= |
Greater or equal | a >= b |
true |
<= |
Less or equal | a <= b |
false |
import java.io.*;
class Relational
{
public static void main(String args[])
{
int a=10,b=5;
System.out.println(“\nGreater than =”+a>b);//true
System.out.println(“\nLesser than =”+a<b);//false
}
Logical operator
🡪Logical AND(&&)🡪both the condition true means output true
🡪Logical OR(||)
🡪Logical NOT(!)
class Logical{
public static void main(String args[]){
int a=85;
System.out.println(“\nLogical AND=” +(a>35 && a<=100));//true
a=32;
System.out.println(“\nLogical AND=”+(a>35 && a<=100));//false
}}
Logical OR(||)
🡪Anyone one of the condition true means output true
class Logical{
public static void main(String args[])
{
int a=32;
System.out.println(“\nLogical OR=” +(a>35 || a<=100));//true
}
}
Logical NOT(!)
True means false
False means true
class Logical{
public static void main(String args[])
{
int a=32;
System.out.println(“\nLogical not=” +!(a>=35));// false🡪true
}
}
Increment/Decrement operator
++
–
class Logical{
public static void main(String args[])
{
int a=1;
System.out.println(“\nPreIncrement=”+(++a));//first increment then print
System.out.println(“\nPostIncrement=”+(a++));//first print the value in variable
after increment occur
System.out.println(“\nvalue inside a=”+a);
}
}
Bitwise operator
&🡪Bitwise AND
|🡪Bitwise OR
^🡪Bitwise XOR
~🡪Biwise NOT
<<🡪Bitwise Left shift
>>🡪Bitwise Right shift
Bitwise AND🡪
class Logical{
public static void main(String args[]){
int a=10,b=11;// 10🡪1010, 11🡪1011 1010
System.out.println(“\nBitwise AND=”+(a&b)); 1011
}
1010🡪10
}
10🡪1010 2 10
2 5 🡪0
2 2 🡪1
1 🡪0
Bitwise OR🡪 one condition true means output true
class Logical{
public static void main(String args[]){
int a=25,b=45;
System.out.println(“\nBitwise OR=”+(a|b));
}
}
//output:61
Bitwise XOR🡪
class Logical{
public static void main(String args[]){
int a=25,b=45;
System.out.println(“\nBitwise XOR=”+(a^b));
} //output=52
25🡪0 1 1 0 0 1
45🡪1 0 1 1 0 1
1 1 0 1 0 0
Bitwise Not🡪
class Logical{
public static void main(String args[]){
int a=25;
System.out.println(“bitwise not=”+~a);
}
o/p
Bitwise not=-26
Left shift🡪(multiply by 2)
class Logical{
public static void main(String args[]){
int a=16;
int b=a<<2;//16*2–>32*2–>64
System.out.println(“bitwise left shift=”+b);
}
Right shift🡪(divide by 2)
class Logical{
public static void main(String args[]){
int a=16;
int b=a>>2;//16/2–>8/2–>4
System.out.println(“bitwise Right shift=”+b);