Operators in Java With Examples
Operators in Java With Examples
1) Arithmetic Operators
Basic arithmetic operators are: +, -, *, /, %
2) Assignment Operators
Assignments operators in java are: =, +=, -=, *=, /=, %=
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(~)
//increment
num1++;
//decrement
num2--;
}
}
Output:
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.
5) Comparison(Relational) operators