Operators in Java
In this tutorial we are going to learn about the operators in java and how vital they are for doing different operations. We will use examples for deep understanding.
Java has a big collection of operators that can be used to perform operations on values in variables. We classify these operators based on the functionality that we provide. Some of the operators in Java are:
- Arithmetic operators
- Assignment operators
- Logical operators
- Comparison operators
- Bitwise operator
Recommended Books
Arithmetic Operators
These type of operators are used for applying arithmetic operations on primitive data types such as:
- Multiplication
- Division
- Modulo
- Addition
- Subtraction
Let’s write a program to illustrate it further:
public class MyClass { public static void main(String args[]) { int x=10; int y=25; //Addition and Subtraction System.out.println("Sum of x+y = " + (x + y)); System.out.println("Difference of x-y = " + (x - y)); //Multiplcation and Division System.out.println("Multiplication of x*y = " + (x * y)); System.out.println("Division of x/y = " + (x / y)); //Modulo gives remainder System.out.println("Remainder of x%y = " + (x % y)); } }
Assignment operator
This type of operator is used to assign or connect a value to any variable. Sentence of declaring for assigning an operator between variable and value is:
variable = value;
However, there are few cases when we want to create a shorter version of a statement called compound statement. So instead of writing sum = sum + 10, we can write sum += 10. It is important to remember that the arithmetic operator you want to utilise should be starting from left to right ( += ). Let’s look at an example:
public class MyClass { public static void main(String args[]) { int x=10; int y=25; //Assigning Operator x = y; //Printing Values System.out.println("Value of x = y is " + x); System.out.println("Value of x += y " + (x+=y)); } }
Relational operators
Relational operators or comparison operators are used for checking comparing equality, greater than equal to signs etc. Some of the comparison operators are:
- Equal to ( == )
- Not equal ( != )
- Greater than ( > )
- Less than ( < )
- Greater than or equal to ( >= )
- Less than or equal to ( <= )
Logical operators
Logical operators are used for checking the logic between two two values, some of the logical operators are:
- Logical and ( && ) returns true both conditions are met.
- Logical or ( || ) returns true only if one of the condition is met.
- Logical not ( ! ) it shows a reverse result, means it turned out to be false when the result is true.
Let’s look at a program to see how these conditions are met:
public class MyClass { public static void main(String args[]) { int x=10; int y=25; //Logical operators System.out.println(x > 1 && y < 30); } }
The above code execution will turn out to be true since all the given conditions are met.
Bitwise operators
Bitwise operators are used for performing binary logic on the bits of an integer. Sum of the bitwise operators are:
- Bitwise and operator ( & )
- Bitwise or operator ( | )
- Bitwise XOR operator ( ^ )
- Bitwise complement operator ( ~ )