In today’s topic, we will discuss various operators in Java. These are very important concepts that are necessary to remember before writing a Java program. Before these, we also discussed Data Type in Java, if you don’t know then Hurry up Now! Previously we also describe briefly various topics of Java such as Features of Java, History of Java, Difference between C++ & Java, and many more which is really helpful for your better understanding. So without wasting time let’s dive into our topic:
Operators in Java
The symbol that is used to perform some logical and mathematical operations are called operators. These are very important to perform an operation on variables and values.
There are different types of operators which are available in Java based on their functionalities these are as given below:
- Arithmetic Operator
- Assignment Operator
- Relational Operator (Comparison Operator)
- Logical Operator
- Bitwise Operator
Arithmetic Operator
Arithmetic operators in Java are generally used to perform addition, subtraction, multiplication, and division. These are perform simple arithmetic operations like : addition (+), subtraction (-), multiplication (*), division (/), modulo(%), increment(++), decrement(–).
Operator | Name |
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo |
++ | Increment |
— | Decrement |
Example:-
public class GeektoCode { public static void main(String args[]){ int num1=100; int num2=10; int addnum = num1+num2; int subnum = num1-num2; int mulnum = num1*num2; int divnum = num1/num2; int modnum = num1%num2; System.out.println(addnum); System.out.println(subnum); System.out.println(mulnum); System.out.println(divnum); System.out.println(modnum); } }
OUTPUT:-
110
90
1000
10
0
Assignment Operator
Assignment operators in Java are used to assign values to variables. Here values are on the right-hand side of the operator and the variable is on the left side, so that the right side value is stored in the variable. It is basically used for assigning values to the variable.
Operator | Functions |
+= | Adding left operand with right operand and then assigning done. |
-= | Subtracting right operand from left operand and then assigning done. |
*= | Multiplying left operand with right operand and then assigning done. |
/= | Dividing left operand by right operand and then assigning done. |
%= | Modulo of left operand by the right operand and then assigning the value. |
Example:-
public class GeektoCode { public static void main(String args[]){ int num=50; num+=1; System.out.println(num); num-=2; System.out.println(num); num*=2; System.out.println(num); num/=2; System.out.println(num); num%=2; System.out.println(num); } }
OUTPUT:-
51
49
98
49
1
Relational Operator
Relational operators in Java are used to compare relations i.e. equality, greater than, and less than. Here these operators return a boolean values i.e. true or false. These are:-
Operator | Name |
== | Equal to |
!= | Not equal |
< | Less than |
<= | Less than equal to |
> | Greater than |
>= | Greater than equal to |
Example:-
public class GeektoCode { public static void main(String args[]){ int num1 = 50; int num2 = 10; System.out.println(num1==num2); System.out.println(num1!=num2); System.out.println(num1<num2); System.out.println(num1<=num2); System.out.println(num1>num2); System.out.println(num1>=num2); } }
OUTPUT:-
false
true
false
false
true
true
Logical Operator
Logical operators in Java are used to perform the logical operation between the given variables or values. Logical operations like “logical AND”, “logical OR” and “logical NOT”. It is similar to the logic gate in digital electronics.
Operator | Name | Functions |
&& | Logical AND | Return true when both conditions are true otherwise false. |
|| | Logical OR | Return false when both conditions are false otherwise true. |
! | Logical NOT | Return true when the condition is false and vice-versa |
Example:-
public class GeektoCode { public static void main(String args[]){ int num = 50; System.out.println(num<60 && num>40); //for logical AND System.out.println(num<30 && num>50); //for logical OR System.out.println(!(num<60 && num>40)); //for logical NOT } }
OUTPUT:-
true
false
false
Bitwise Operator
Bitwise operators are used for the manipulation of bits of a number.
Operator | Name | Functions |
& | Bitwise AND | Return bit by bit AND of input value |
| | Bitwise OR | Return bit by bit OR of input value |
^ | Bitwise XOR | Return bit by bit XOR of input value |
~ | Bitwise Complement | Return one’s complement of the input value |
>> | Right Shift | Shifts the bits of the number to the right |
<< | Left Shift | Shifts the bits of the number to the left |
So this is all about the Operators in Java. I hope this is helpful to you. In the next upcoming post we will discuss another new topic of java, so wait for this will meet in the next post. Thank you.