Explore the realm of operators in Java with our comprehensive guide. From the fundamental Assignment Operator to Arithmetic, Relational, Logical, Bitwise, Unary, and Ternary Operators, delve into coding examples that demystify their functionality. Elevate your Java programming skills by understanding how these operators manipulate data, making your code efficient and concise.
Operator in Java is a symbol or a sign which is used for performing operations. For example: -, +, /, * etc. In Java there are many different types of operators which are given below:-
Assignment =
operators are used to assigns the value on its left variable to the right side variable or data. Assignment operators in Java are given below:
Operator | Example | EqualTo | Explanation |
---|---|---|---|
= | a = b; |
a = b; |
It takes the value of b and assigns into a. |
+= | a += b; |
a = a+b; |
The a += b operator in Java is a shortcut for adding the value of b to the current value of a, updating a in the process. |
-= | a -= b; |
a = a-b; |
The a-=b operator in Java subtracts the value of b from the current value of a, updating a in the process. |
*= | a *= b; |
a = a*b; |
The a *= b operator in Java multiplies the current value of a by the value of b, updating a accordingly. |
/= | a /= b; |
a = a/b; |
The a /= b operator in Java divides the current value of a by the value of b, and the result is assigned back to a. |
%= | a %= b; |
a = a%b; |
The a %= b operator in Java calculates the remainder when the current value of a is divided by the value of b, updating a with the result. |
import java.io.*;
Class Main{
Public static void main(String args[]){
int a = 12, b = 5;
int c = 12, d = 5;
a += b;
c %= d;
System.out.println(a);
System.out.println(c);
}
}
17
2
Arithmetic operators are used to perform arithmetic operations on two or more variables. Arithmetic operators in Java are given below:
Operator | Operation |
---|---|
+ | It adds two or more variables / values one with others. |
- | It subtracts two or more variables / values on from another |
* | It multiplies two or more variables / values one with others. |
/ | It division two or more variables / values one by anothers. [N:B If you use the division operator with two integers, then the result will also be an integer. And, if atleast one of the operands is a floating point number, you will get the result will also be in floating point. ] |
% | Modulo Operation is basically keep the remainder. |
import java.io.*;
Class Main{
Public static void main(String args[]){
int a = 12, b = 5;
float c = 12, d = 5;
int Addition = a + b; // addition operator
int Substract = a - b; // subtraction operator
int Multiplication = a * b; // multiplication operator
float Division = c / d; // division operator
int Modulo = a % b; // modulo operator
System.out.println("a + b = "+ Addition);
System.out.println("a - b = "+ Substract);
System.out.println("a * b = "+ Multiplication);
System.out.println("c / d = "+ Division);
System.out.println("a % b = "+ Modulo);
}
}
a + b = 17
a - b = 5
a * b = 60
c / d = 2.4
a + b = 2
Relational operators are used to check the relationship or comparison between two operands or variables. Relational operators in Java are given below:
Operator | Description | Example |
---|---|---|
== | Is Equal To | 5 == 5 will return true. |
!= | Is Not Equal To | 5 != 4 will return true. |
> | Greater Than | 5 > 4 will return true. |
< | Less Than | 5 < 4 will return false. |
>= | Greater Than Equal To | 5 >= 4 will return true. |
<= | Less Than Equal To | 5 <= 4 will return false. |
import java.io.*;
Class Main{
Public static void main(String args[]){
int a = 5, b = 4;
System.out.println(a == b);
System.out.println(a >= b);
}
}
false
true
Logical operators are used to check 2 or more expression is true or not. Logical operators in Java are given below:
Operator | Example | Description |
---|---|---|
&& (Logical AND) | 5>4 && 9>3 | If both side expression of && (AND) is true, then it returns true. |
|| (Logical OR) | 5>4 || 9<3 | If any one side expression of || (OR) is true, then it returns true. |
! (Logical NOT) | !expression | If expression is true, then it returns false and if expression is false then it returns true and vice versa. |
import java.io.*;
Class Main{
Public static void main(String args[]){
int a = 5, b = 4, c = 1;
System.out.println(a > b && b > c);
System.out.println(a > b || b < c);
System.out.println( !(a > b) );
}
}
true
true
false
Bitwise operators in Java are used to perform operations on every bits. Bitwise operators in java are:
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise Complement |
<< | Bitwise Left Shift |
>> | Bitwise Right Shift |
>>> | Unsigned Right Shift |
In Java, unary operators perform operations on a single operand. Examples include ++, -- for increment and decrement, and - for negation. These operators allow concise manipulation of variables in various expressions. Unary operators in Java are given below:
Operator | Description | Example |
---|---|---|
++ | Increment Operator | If a = 4 then a++ will be 5.
|
-- | Decrement Operator | If a = 4 then a-- will be 3.
|
import java.io.*;
Class Main{
Public static void main(String args[]){
int a = 5, b = 5, c = 5, d = 5;
System.out.println("Pre-Increment: "+ (++a));
System.out.println("Post-Increment: "+ (a++));
System.out.println("Pre-Decrement: "+ (--c));
System.out.println("Post-Decrement: "+ (d--));
}
}
Pre-Increment: 6
Post-Increment: 5
Pre-Decrement: 4
Post-Decrement: 5
In Java, the ternary operator (conditional operator) is a concise way to write a simple conditional expression. It has the form condition ? expression1 : expression2, and it evaluates to expression1 if the condition is true, or expression2 otherwise. For an example:
variable = expression ? value1 : value2;
the expression variable = expression ? value1 : value2; uses the ternary operator. It evaluates the condition represented by expression. If the condition is true, it assigns value1 to variable; otherwise, it assigns value2. This concise form is a shorthand way to express a simple conditional assignment in Java.
import java.io.*;
Class Main{
Public static void main(String args[]){
int a = 5, b;
b = a < 3 ? 2 : 4;
System.out.println(b);
}
}
4