The operators in Java represent the operations, i.e, operators perform operations on the operands.
Let us discuss the various operators in Java.
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 |
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 |
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. |
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 | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
x+=10; //this is same as x=x+10;
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 |
Operator | Use | Description |
& | x&y | Bitwise and |
| | x|y | bitwise or |
^ | x^y | bitwise xor |
~ | ~y | bitwise complement |
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
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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…