Operators in Java With Examples
Operator is a symbol that instructs the compiler to perform a specific action.
For example, a “+” operator instructs the compiler to perform addition, a “>”
operator instructs the compiler to perform comparison, “=” for assignment and
so on. In this guide, we will discuss operations in java with the help of
examples.
Operator and Operand:
In any operation, there is an operator and operands. For example: In a+b,
the “+” symbol is the operator and a & b are operands.
Types of Operator in Java
Operators in java are classified in following eight categories:
1) Arithmetic Operators
2) Assignment Operators
3) Unary Operators
4) Logical Operators
5) Relational operators
6) Bitwise Operators
7) Ternary Operator
8) Shift Operators
1) Arithmetic Operators
Basic arithmetic operators are: +, -, *, /, %
Note: Division (/) operator returns quotient while modulo operator(%)
returns remainder. For example 10 % 5 would return 0 while 10/5 would
return 2.
Example of Arithmetic Operators
public class JavaExample {
public static void main(String args[]) {
int num1 = 100;
int num2 = 20;
System.out.println("num1+num2: " + (num1 + num2) );
System.out.println("num1-num2: " + (num1 - num2) );
System.out.println("num1*num2: " + (num1 * num2) );
System.out.println("num1/num2: " + (num1 / num2) );
System.out.println("num1%num2: " + (num1 % num2) );
}
}
Output:
2) Assignment Operators
Assignments operators in java are: =, +=, -=, *=, /=, %=
Example of Assignment Operators
public class JavaExample {
public static void main(String args[]) {
int num1 = 10, num2;
num2 = num1;
System.out.println("= Output: "+num2);
num2 += num1;
System.out.println("+= Output: "+num2);
num2 -= num1;
System.out.println("-= Output: "+num2);
num2 *= num1;
System.out.println("*= Output: "+num2);
num2 /= num1;
System.out.println("/= Output: "+num2);
num2 %= num1;
System.out.println("%= Output: "+num2);
}
}
Output:
3) Unary Operators
As the name suggests, The Unary operators in Java involve single operand.
Java supports following unary operators:
Unary minus(-)
Increment(++)
Decrement(- -)
NOT(!)
Bitwise Complement(~)
num++ is equivalent to num=num+1;
num–- is equivalent to num=num-1;
Example of Unary Operators
public class JavaExample {
public static void main(String args[]){
int num1=100;
int num2=200;
//minus(-) unary operator
int inverseNum = -num1;
System.out.println("Opposite of num1: "+inverseNum);
//increment
num1++;
//decrement
num2--;
System.out.println("num1++ is: "+num1);
System.out.println("num2-- is: "+num2);
}
}
Output:
Opposite of num1: -100
num1++ is: 101
num2-- is: 199
The NOT(!) Operator reverses the logical state (true or false) of an operand. If
an operand or condition is true, then the Logical NOT operator will make it
false and vice-versa.
If value of a boolean variable 'bool' is true, then the value of
!bool is false
If the value of 'bool' is false, then the value of !bool is true
4) Logical Operators
Logical Operators are used to evaluate the outcome of conditions. There are
three logical operators: AND (&&), OR (||) and NOT (!). The AND and OR
operators are used when multiple conditions are combined and we need to
evaluate the outcome as a whole.
Example of Logical Operators
In this example, we are performing logical operations on boolean variables.
However in practical scenarios, these operators are used to combine the
multiple conditions (or expressions), which we have covered in the separate
tutorial (link is at the end of the following program).
public class LogicalOperatorDemo {
public static void main(String args[]) {
boolean b1 = true;
boolean b2 = false;
System.out.println("b1 && b2: " + (b1&&b2));
System.out.println("b1 || b2: " + (b1||b2));
System.out.println("!(b1 && b2): " + !(b1&&b2));
}
}
Output:
b1 && b2: false
b1 || b2: true
!(b1 && b2): true
5) Comparison(Relational) operators
Relational operators are used to compare two operands. In java, we have
following relational operators:
Example of Relational operators
Note: This example is using if-else statement which is our next tutorial, if you
are finding it difficult to understand then refer if-else in Java.
public class JavaExample {
public static void main(String args[]) {
int num1 = 10;
int num2 = 50;
if( num1 != num2 ){
System.out.println("num1 and num2 are not equal");
}
else{
System.out.println("num1 and num2 are equal");
}
if( num1 > num2 ){
System.out.println("num1 is greater than num2");
}
else{
System.out.println("num1 is not greater than num2");
}
if( num1 < num2 ){
System.out.println("num1 is less than num2");
}
else{
System.out.println("num1 is not less than num2");
}
}
}
Output: