In this tutorial you will learn about the Java Operators and its application with practical example.
Java Operators
An operator is a special symbol that is used to carry out some specific operation on its operand. In Java, we have a rich set of built-in operators to carry out different types of operations. There are operators for assignment, arithmetic operations, logical operations, comparison operations, etc. Java operators can be used with many types of variables or constants, but some of the operators are restricted to working on specific data types. Most operators are binary, meaning they take two operands, but a few are unary and only take one operand.
Type of operators in Java
In Java, we have the following types of operators available –
- Assignment
- Arithmetic
- Unary
- Relational
- Logical
- Conditional
- Bitwise
- instanceof
- Dot (.)
Java Assignment Operators
Assignment operators are used to assigning value to a variable, you can assign a variable value or the result of an arithmetical expression. In many cases, the assignment operator can be combined with other operators to build a shorthand version of an assignment statement known as a Compound Statement. For example, instead of a = a+5, we can write a += 5.
Operator | Description | Expression |
---|---|---|
= |
Assignment Operator |
a=b |
+= |
add and assign |
a+=b is equivalent to a=a+b |
-= |
subtract and assign |
a-=b is equivalent to a=a-b |
*= |
multiply and assign |
a*=b is equivalent to a=a*b |
/= |
divide and assign |
a/=b is equivalent to a=a/b |
%= |
mod and assign |
a%=b is equivalent to a=a%b |
<<= |
Left shift AND assign |
a<<=5 is equivalent to a=a<<5 |
>>= |
Right shift AND assign |
a>>=5 is equivalent to a=a>>5 |
&= |
Bitwise AND assign |
a&=5 is equivalent to a=a&5 |
^= |
Bitwise exclusive OR and assign |
a^=5 is equivalent to a=a^5 |
|= |
Bitwise inclusive OR and assign |
a|=5 is equivalent to a=a|5 |
Java Arithmetic Operators
Arithmetic Operators are used to performing arithmetic operations like addition, subtraction, multiplication, division, %modulus, exponent, etc. Let variable a hold 20 and variable b holds 10, then −
Operator | Name | Description | Example |
---|---|---|---|
+ |
Addition |
Addition of given operands |
a+b returns 30 |
- |
Subtraction |
Subtraction of second operand from first |
a-b returns 10 |
* |
Multiply |
Multiplication of given operands |
a*b returns 200 |
/ |
Division |
Returns Quotient after division |
a/b returns 2 |
% |
Modulus |
Returns Remainder after division |
a%b returns 0 |
Java Unary Operators (post and pre)
In Java, ++ and — are known as increment and decrement operators respectively. These are unary operators which means they work on a single operand. ++ adds 1 to operand and — subtracts 1 to operand respectively. When ++ is used as a prefix(like ++i), ++i will increment the value of i and then return it but, if ++ is used as a postfix(like i++), It will return the value of the operand first and then only increment it.
Operator | Example | Description |
---|---|---|
++ [prefix] | ++a | The value of an increment |
++ [postfix] | a++ | The value of a before an increment |
— [prefix] | –a | The value of an after decrement |
— [postfix] | a– | The value of a before the decrement |
Java Comparison (Relational) Operators
Comparison Operators are used to evaluating a comparison between two operands. The result of a comparison operation is a Boolean value that can only be true or false. Comparison Operators are also referred to as relational operators.
Let variable a hold 20 and variable b hold 10, then −
Operator | Description | Example |
---|---|---|
> |
greater than |
a>b returns TRUE |
< |
Less than |
a<b returns FALSE |
>= |
greater than or equal to |
a>=b returns TRUE |
<= |
less than or equal to |
a<=b returns FALSE |
== |
is equal to |
a==b returns FALSE |
!= |
not equal to |
a!=b returns TRUE |
Java Logical Operators
Logical operators are used to combine expressions with conditional statements (AND, OR, NOT) which results in true or false. Let variable a hold true or 1 and variable b holds false or 0, then −
Operator | Name | Description | Example |
---|---|---|---|
&& |
Logical AND |
return true if all expression are true |
(a && b) returns false |
|| |
Logical OR |
return true if any expression is true |
(a || b) returns true |
! |
Logical NOT |
return complement of expression |
!a returns false |
Java Conditional operator ( ? : )
In Java, the conditional operator is considered shorthand for an if-else statement. The conditional operator is also called as “Ternary Operator”.
Syntax:
1 |
condition ? result1 : result2 |
If the condition is true the expression will return the result1, if it is not it will return result2.
Java Bitwise Operators
Bitwise operators are used to performing the bit-level operations over its operand.
Let A = 60; and B = 13;
Binary equivalent
A = 0011 1100
B = 0000 1101
Operator | Meaning | Example | Description |
---|---|---|---|
& | Binary AND | (A & B) | It returns 12 which is 0000 1100 |
| | Binary OR | (A | B) | It returns 12 which is 0000 1100 |
^ | Binary XOR | (A ^ B) | It returns 49 which is 0011 0001 |
~ | One’s Complement | (~A ) | It returns -60 which is 1100 0011 |
<< | shift left | A << 2 | It returns 240 which is 1111 0000 |
>> | shift right | A >> 2 | It returns 15 which is 0000 1111 |
Java instanceof Operator
In Java, the instanceof operator is used to determine whether an object is an instance of a class, a subclass, or an interface or not.
Java Dot(.) Operator
The (.) operator is also known as the member operator it is used to access the member of a package or a class.