The operators in Java represent the operations, i.e, operators perform operations on the operands.
Let us discuss the various operators in Java.
Arithmetic Operators:
- These operators are for basic arithmetic operations
- They are additions, subtraction, multiplication, division, and remainder.
- Each of these operators is binary, i.e, it requires two values (operands) to calculate the final answer.
Operators | Description |
+ | adds two operands, example: int a=2,b=4; int c=a+b; output: c=6 |
– | subtract two operands, example: int a=2,b=4; int c=b-a; output: c=2 |
* | multiply two operands, example: int a=2,b=4; int c=a*b; output: c=8 |
/ | divides two operands and provides their quotient, example: int a=2,b=4; int c=b/a; output: c=2 |
% | divides two operands and provides their remainder, example: int a=2,b=4; int c=b%a; output: c=0 |
Unary Operators:
- Operators that act on one operand are known as unary operators.
- incrementing/decrementing a value by one
- negating an expression
- inverting the value of a boolean
Operator | Category | Description |
++, – – | prefix | Increment and decrement example: int x=5,y=3; ++x; –y; output: x=6 y=2; |
postfix | Increment and decrement example: int x=5,y=3; x++; y–; output: x=6 y=2; | |
~ | prefix | negate an expression example: int a=8; ~a; output:a=-9 |
! | prefix | invert boolean value. example: boolean a=true; !a; output: a=false |
Binary Operators:
- Operators that act upon two operands are referred to as Binary Operators.
- The operands of the binary operator are distinguished as left or right operands
- These are the same as the Arithmetic operators.
Logical Operators:
- These are used to check conditions.
- These are of six types. They are:
Operator | Use | Returns true if |
&& | x && y | x and y are both true, conditionally evaluates y |
|| | x || y | either x or y is true, conditionally evaluates y |
! | !x | x is false. |
& | x and y are both true, and always evaluate x and y. | |
| | x | y | either x or y is true, always evaluates x and y. |
^ | x ^ y | if x and y are different – that is if one or the other of the operands is true but not both. |
Relational Operator:
- These operators represent the relationship that operands can have.
- Java provides six relational operators.
- It returns a boolean value.
Operator | Use | Description |
comparison | < | true if right operand is greater than left operand, otherwise false. |
> | true if left operand is greater than right operand, otherwise false. | |
<= | true if right operand is greater or equal to left operand, otherwise false. | |
>= | true if left operand is greater or equal to right operand, otherwise false. | |
equality | == | returns true if both operands are equal, otherwise false. |
!= | returns if both operands are not equal, otherwise false. |
Assignment Operator:
- These operands allow us to assign one value to another operand;
Assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
- The +=, -=, *=, etc are called shorthand operators which simplify the code as it assigns as well as evaluates the value. For instance,
x+=10; //this is same as x=x+10;
Shift Operators:
- Shift operators perform bit manipulation on data by shifting the bits of its first operand right or left.
Operator | Use | Description |
>> | x>>y | shift bits of x right by y bits |
<< | x<<y | shift bits of x left by y bits |
>>> | x>>>y | For positive number, >> and >>> works same For negative number, >>> changes parity bit (MSB) to 0 |
Bitwise Operator:
- The bitwise operators work with integral types
- They are sometimes referred to as logical operators when working with boolean values.
Operator | Use | Description |
& | x&y | Bitwise and |
| | x|y | bitwise or |
^ | x^y | bitwise xor |
~ | ~y | bitwise complement |
Other Operators:
Operator | Description |
? : | Shortcuts if-else statement |
[] | Used to declare arrays, create arrays, and access array elements |
. | Used to form qualified names |
(params) | Delimits a comma-separated list of parameters |
(type) | Casts(converts) a value to a specified type |
new | Creates a new object or a new array |
instanceof | Determines whether its first operand is an instance of its second operand |
Note: also read about the Byte Code in Java
Follow Me
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment