Operators in java
Operators are special symbol which are used to perform some operation on two or three or more operands or expression manipulation. There are following types of operators available in java.
- Arithmetic Operators
- Assignment Operator
- Unary Operators
- Equality and relational operators
- Conditional Operators
- Bitwise Operators
Arithmetic Operators: These are the operators which perform addition, subtraction, multiplication, and division.
Operator | Description |
+ | Additive operator (also used for String concatenation) |
- |
Subtraction operator |
* |
Multiplication operator |
/ |
Division operator |
% |
Remainder operator |
Assignment Operator: This operator assign the value from right hand side to left side operand.It is denoted by “=”.
e.g. int x =3;
Unary Operators: These perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean. It requires only one operand.
Operator | Description |
+ | Unary plus operator; indicates positive value (numbers are positive without this, however) |
– | Unary minus operator; negates an expression |
++ | Increment operator; increments a value by 1 |
— | Decrement operator; decrements a value by 1 |
! | Logical complement operator; inverts the value of a boolean |
int age =+1; // age is 1 age++; // age is 2 age--; // age is 1 again
Equality and relational operators: These operators are used to compare one operand to another like if one operand is greater than another, less than,equal to or not equal to etc.
Operators | Description |
== | equal to |
!= | not equal to |
> | greater than |
< | less than |
>= | greater than or equal to |
<= | less than or equal to |
Conditional Operators: These operator perform AND and OR operation on 2 boolean expressions.
Operator | Description |
&& | Conditional-AND |
|| | Conditional-OR |
class Demo1 { public static void main(String[] args){ int val1 = 1; int val2 = 2; if((val1 == 1) && (val2 == 2)) System.out.println("value1 is 1 AND value2 is 2"); if((val1 == 1) || (val2 == 1)) System.out.println("value1 is 1 OR value2 is 1"); } }
Bitwise Operators: These operators perform bitwise and bit shift operations on integral types. These are not used much in practical scenario.
E.g. Let’s consider a = 60; and b = 13. Now, if performed binary operation how it will be?
a = 0011 1100
b = 0000 1101
Perform several operation as given below:
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011