Operators in Java
Any programming language which we use to process data and to get help with manipulating it according to the user’s needs, performs some kind of operations on the data. These operations are carried out with the help of operators. Learning about how an operator works will help you to manage the data properly and perform the required tasks easily. In this blog, you will learn what kind of operators are supported by Java, how to use them, the correct syntax to help with the practical application of it when you’re coding and the precedence of operators in Java.
What are operators in Java?
Operators are basically symbols that we use to perform certain operations on the given operands. In other words, operators perform certain operations on the given data which helps us to derive our desired result from it.
- Operators in Java can perform different kinds of operations such as arithmetic, logical or bit level. They can even be used to define relationships among different operands.
- On the basis of the kinds of operations they can perform, operators are classified into 8 different types.
- All the operators in Java work according to their precedence and associativity. Based on these two parameters different expressions are evaluated to give different results.
- The entities on which the operations are performed are known as operands.
Types of Operators in Java
The different types of operators in Java are:-
a. Arithmetic Operators in Java
These are the operators which are used to perform mathematical calculations in a Java program. Arithmetic Operators are;-
i. Addition(+):- Used to perform Addition.
ii. Subtraction(-):- Used to perform Subtraction.
iii. Multiplication(*):- Used to perform Multiplication.
iv. Division(/):-Used to perform Division.
v. Modulus(%):- Used to calculate the remainder after a division operation.
Program to illustrate java arithmetic operators
//FirstCode Program to illustrate arithmetic operators class FirstCode { public static void main(String[] args) { int val1, val2,resultadd,resultsub,resultmult,resultdiv,resultmod; val1=5; val2=10; resultadd=val1+val2; resultsub= val2-val1; resultmult=val1*val2; resultdiv= val2/val1; resultmod=val2%val1; System.out.println(“The result of addition is”+ resultadd); System.out.println(“The result of subtraction is”+ resultsub); System.out.println(“The result of multiplication is”+ resultmult); System.out.println(“The result of division is”+ resultdiv); System.out.println(“The result of mod is”+ resultmod); } }
Output:
The result of subtraction is 5
The result of multiplication is 50
The result of division is 2
The result of mod is 0
b. Relational Operators in Java
These are the operators which are used to define relationships among operands. The operations performed by these operands result in true or false values.
i. Equal to(==): Compare two values are equal or not and find if they are equal or not. Returns true if the values are equal, otherwise false.
ii. Not Equal to(!=): Compares two values and returns true if they aren’t equal to each other, otherwise returns false.
iii. Greater than(>): Compares two values and returns true if the left hand side value is greater than the right hand side value, otherwise false.
iv. Lesser than(<):Compares two values and returns true if the left hand side value is lesser than the right hand side value, otherwise false.
v. Greater the equal to (>=):Compares two values and returns true if the left hand side value is greater than or equal to the right hand side value, otherwise false.
vi. Lesser than equal to(<=):Compares two values and returns true if the left hand side value is lesser than or equal to the right hand side value, otherwise false.
Program to illustrate java relational operators
//FirstCode Program to illustrate relational operators class FirstCode { public static void main(String[] args) { int val1, val2; boolean resultgreat,resultless,resultgreateq,resultlesseq,resulteq,resultnoteq; val1=5; val2=10; resultgreat=(val1>val2); resultless= (val2<val1); resultgreateq=(val1>=val2); resultlesseq= (val2<=val1); resulteq=(val2==val1); resultnoteq= (val1!=val2); System.out.println(“The result of greater than is ”+ resultgreat); System.out.println(“The result of smaller than is ”+ resultless); System.out.println(“The result of greater than equal to is ”+ resultgreateq); System.out.println(“The result of smaller than equal to is ”+ resultsmalleq); System.out.println(“The result of equal to is ”+ resulteq); System.out.println(“The result of not equal to is ”+ resultnoteq); } }
Output:
The result of smaller than is false
The result of greater than equal to is false
The result of smaller than equal to is false
The result of equal to is false
The result of not equal to is true
c. Unary Operator
Unary Operators are the ones that perform operations only on a single operator. They can increment or decrement a value by 1 or give the negation of a value.
i. Unary minus(-): This negates a value.
ii. Unary plus(+): This is used to show that the given value is a positive one. However, it can also convert a byte, short or char value to int value.
iii. Increment Operator(++): This is used to increase a value by 1. It’s of two types:-
- Pre increment operator: This first increments the value then uses it in the expression that it is included.
- Post increment operator: This first uses the original value in the expression and then increments it.
iv. Decrement Operator: This is used to decrease a value by 1. It’s of two types:-
- Pre decrement operator: This first decrements the value then uses it in the expression that it is included.
- Post decrement operator: This first uses the original value in the expression and then decrements it.
Program to illustrate Java Unary operators
//FirstCode Program to illustrate Unary operators class FirstCode { public static void main(String[] args) { int val1, val2, unmin,preinc,postinc,predec,postdec; boolean resultgreat,resultless,resultgreateq,resultlesseq,resulteq,resultnoteq; val1=5; val2=10 unmin=-(val1); preinc=++val1+val2; postinc=(val1++)+val2; predec=--val1+val2; postdec=(val1- -)+val2; System.out.println(“The result of Unary minus is ”+Unmin); System.out.println(“The result of pre increment ”+ preinc); System.out.println(“The result of post increment is ”+ postinc); System.out.println(“The result pre decrement is ”+ predec); System.out.println(“The result of post decrement is ”+ postdec); } }
Output:
The result of pre increment 16
The result of post increment is 15
The result pre decrement is 14
The result of post decrement is 15
d. Logical Operators in java
These operators are used to perform operations which imitate logic gates AND, OR and NOT. They return true or false value. If the first value, that is, the value on the left hand side is false in case of an AND operation then it returns the result as false without checking the second value.
i. Logical AND(&&):– returns true if both values are true, otherwise false.
ii. Logical OR(||):-returns true even if one value is true.
iii. Logical not(!):- Turns true value to false and vice-versa.
Program to illustrate java logical operators
//FirstCode Program to illustrate logical operators class FirstCode { public static void main(String[] args) { Boolean val1, val2; val1=true; val2=false; andres=(val1&&val2); orres=(val1||val2); notres=(!val1); System.out.println(“result of AND”+ andres); System.out.println(“result of OR”+ orres); System.out.println(“result of NOT”+ notres); } }
Output:
result of OR true
result of NOT false
e. Bitwise Operators in java
These operators are used to perform operations at the bit level.
i. Bitwise AND(&): used to perform logical AND operation on the bit level.
ii. Bitwise XOR(^):used to perform logical XOR operation on the bit level.
iii. Bitwise OR(|):used to perform logical OR operation on the bit level.
iv. Bitwise Complement (~): Complements the value by inverting the bits.
Program to illustrate Java Bitwise Operators
//FirstCode Program to illustrate Bitwise operators class FirstCode { public static void main(String[] args) { int num1 = 2, num2 =4, resor, resand, rescomp, resxor; resor= num1 | num2; resand=num1&num2; rescomp=~num1 resxor=num1^num2; System.out.println(resor); System.out.println(rescomp); System.out.println(resand); System.out.println(resxor); } }
Output
13
0
6
f. Shift Operators in Java
Shift Operators are used to shift the bits in a number. It’s results are the same as multiplying a number with two or dividing a number with two.
i. Left shift (<<):– This operator shifts all the bits to their left. The empty space on the left side is filled with the 0s. It’s results are the same as multiplying something with 2.
ii. Unsigned Right Shift(>>):– This operator shifts all the bits to their right. The empty space on the right side is filled with 0s. The last bit on the left remains a 0.
iii. Signed Right Shift(>>>):– This operator shifts all the bits to their right. The empty space on the right side is filled with 0s. The last bit on the left is decided by the existing number.
The syntax to use this is <number><shift operator><number of places to shift>
Program to illustrate Java Shift Operators
//FirstCode Program to illustrate shift operators class FirstCode { public static void main(String[] args) { int num1 = 4; Int num2=-8; int resls = num1 << 2; Intr resurs=num1>>>2 Int ressrs=num2>>2 System.out.println(resls); System.out.println(resurs); System.out.println(ressrs); } }
Output:
-2
1
g. Ternary Operators in Java
We substitute ternary operators for if-else statements. These work on the basis of a condition.
<condition>?<statement to be executed if the condition is true>:<statement to be executed if
the condition is false>
Program to illustrate Java Ternary Operators
//FirstCode program to illustrate ternary operator Class FirstCode { Public static void main(String [] args) { int val1,val2,max; val1=10; val2=20; max=val1>val2?val1:val2; System.out.println(“Maximum value is” + max); } }
Output:
h. Assignment Operators in Java
The “equal to” is the assignment operator and it assigns a value to a variable. The value on the right side of the = symbol is assigned to the variable on the left side. We must ensure that the value which we are assigning to a variable is either declared or it’s a constant value. This will avoid getting undesired output.
This can be used with arithmetic operators to make compound statements which make the program shorter.
For Example:-
+=
/=
*=
This means that the variable on the left-hand side will be included in the expression on the right-hand side along with the operator which is there with the = sign.
Program to illustrate java assignment operators
//FirstCode program to illustrate assignment operators Class FirstCode { Public static void main(String [] args) { int val1,val2,res,res1=1,res2=2; val1=10; val2=20; res=val1+val2; res1+=val1+val2; res2*=val1*val2; System.out.println(“The results of different operations are” +res+ res1+res2); } }
Output:
Precedence and Associativity of Operators in Java
When we come across complex expressions which contain a lot of operators we tend to get confused. It gets hard to decide which operation we should perform first. In cases like these we need to work according to the precedence and associativity of the operators. When there are multiple operators, the operator with the highest precedence should be applied first. However, when the operators with the same precedence are there we should work according to the associativity that is left to right or right to left.
Given below is a table to show the precedence of operators.
The table below shows the associativity of each operator.
instanceof in Java
This is used to check if the given object is an instance of a specific class or not. It returns either true or false value. It does the job of comparing the instance with type.
Program to illustrate java Instanceof
Class FirstCode { public static void main(String[] args) { FirstCode Obj= new FirstCode() boolean res; res=(Obj instanceof FirstCode); System.out.println(res); } }
Output:
Conclusion
We use operators in Java to perform different kinds of operations. These operations can be considered as a way to manipulate data in order to get the desired results. Each operator has a precedence order and associativity and works according to it. This blog thoroughly explains every important concept related to operators.