Java provides various operators, such as arithmetic, assignment, comparison, logical, etc. Arithmetic operators perform basic arithmetic operations, assignment operators assign values to variables, comparison operators compare values, and logical operators perform logical operations like AND, OR, NOT.
In Java, operators are symbols or special characters used to perform various operations on variables and values. They allow you to manipulate data, perform arithmetic calculations, compare values, and control the flow of your program. Java operators are categorized into several types based on their functionality. Let’s explore the different types of operators in Java:
Operator | Description | Example |
---|---|---|
+ | Addition | int result = 5 + 3; |
- | Subtraction | `int result = 5 - 3;` |
* | Multiplication | ‘int result = 5 * 3;` |
/ | Division | `int result = 5 / 3;` |
% | Modulus (Remainder) | `int result = 5 % 3;` |
++ | Increment (postfix or prefix) | `int i = 5; i++;` or `int j = 5; ++j;` |
-- | Decrement (postfix or prefix) | `int i = 5; i--;` or `int j = 5; --j;` |
Example Arithmetic Operatore :-
// Addition
int a =10;
int b = 5;
int sum = a + b; // sum = 15
//Subtraction
int Sub = a – b; // difference = 5
// Multiplication
int multiplication = a * b; // product = 50
// Division
int division = a / b; // quotient = 2
// Module
int remainder = a % b; // remainder = 0
System.out.println(“Value of a = ” +a +” and b = ” +b);
System.out.println(“Calculation of A and B Is \t Sum = ” + sum + “\t Subtraction = ” + sub + “\t Multiplcaton = ” + multiplication + “\t Devision = ” + division + “\t Module = ” + remainder);
Output :- Value of a = 10 and b = 5
Calculation of A and B Is Sum = 15 Subtraction = 5 Multiplcaton = 50 Devision = 2 Module = 0
2. Assignment Operators :- Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Simple assignment | int x = 10; |
+= | Addition assignment | x += 5; |
-= | Subtraction assignment | x -= 3; |
*= | Multiplication assignment | x *= 2; |
/= | Division assignment | x /= 4; |
%= | Modulus assignment | x %= 3; |
Example :-
public class Demo
{
public static void main(String []args)
{
int x = 4;
// If x is greater than 5, increment x by 1
if (x > 5)
{
x++;
System.out.println(x);
}
// If x is even, multiply x by 2
if (x % 2 == 0)
{
x *= 2; // Hare i Use Muliplication assignment Operatore
System.out.println(x);
}
}
}
Output :- 8
Operator | Description | Example |
---|---|---|
== | Equal to | if (x == y) |
!= | Not Equal to | if (x != y) |
> | Greater than | if (x > y) |
< | Less than | if (x < y) |
>= | Greater than Equal to | if (x >= y) |
<= | Less than equal to | if (x <= y) |
Example :-
public class ComparisonExample { public static void main(String[] args) { int a = 5; int b = 3; // Equal to boolean isEqual = (a == b); System.out.println("a == b: " + isEqual); // Output: false // Not equal to boolean isNotEqual = (a != b); System.out.println("a != b: " + isNotEqual); // Output: true // Greater than boolean isGreaterThan = (a > b); System.out.println("a > b: " + isGreaterThan); // Output: true // Less than boolean isLessThan = (a < b); System.out.println("a < b: " + isLessThan); // Output: false } }
Output :- false
ture
ture
false
Operator | Description | Example |
---|---|---|
&& | Logical AND | if (x > 0 && x < 10) |
|| | Logical OR | if (x == 0 || x == 10) |
! | Logical ! (Negation) | if (!flag) |
Example:-
boolean a = true;
boolean b = false;
// Logical AND
boolean OP_And = a && b; //variable OP_And result is false
System.out.println(OP_And);
// Logical OR
boolean OP_Or = a || b; // variable OP_Or result is ture
System.out.println(OP_Or);
// Logical NOT
boolean OP_Not = !a; // variable OP_Not result is false
System.out.println(OP_Not);
Output :- false
ture
false
Operator | Description | Example |
---|---|---|
& | Bitwise AND | int result = a & b; |
\ | Bitwise OR | int result = a \| b; |
^ | Bitwise XOR (exclusive OR) | int result = a ^ b; |
~ | Bitwise NOT (complement) | int result = ~a; |
<< | Left Shift | int result = a << 2; |
>> | Right Shift (With Sign ) | int result = a >> 2; |
>>> | Right Shift (No Sign Extension) | int result = a >>> 2; |
Example:-
int num1 = 5; // binary: 0101
int num2 = 3; // binary: 0011
int resultAnd = a & b; // 0101 & 0011 = 0001
System.out.println("a & b = " + resultAnd); // Output: 1
// Bitwise OR
int resultOr = a | b; // 0101 | 0011 = 0111
System.out.println("a | b = " + resultOr); // Output: 7
// Bitwise XOR
int resultXor = a ^ b; // 0101 ^ 0011 = 0110
System.out.println("a ^ b = " + resultXor); // Output: 6
Output :- 1
6
7
Syntax :- variable = (condition) ? expression1 : expression2
Example :-
int x = 5;
String result = (x > 0) ? “Positive” : “Non-positive”;
Output :- Positive
Syntax :-
Example :-
public class Test { public static void main(String args[]) { String Msg = "This is string."; // following will return true when Msg Variable is type of String boolean result = name instanceof String; System.out.println( result ); } }
Output :- true
These are the main categories of operators in Java. Understanding how to use operators effectively is essential for writing efficient and error-free Java code.