Java Basic Operators
Table of Contents
Java provides a set of operators to manipulate variables.
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Assignment Operators
- Miscellaneous Operators
1. Arithmetic Operators
In mathematical expression we are used arithmetic operator in the same way as they are used in algebra.
Arithmetic operators list :−
Operator Symbol | Operator Name | Description | Example |
---|---|---|---|
+ | Addition | Adds(+) values on either side of the operator. | A + B |
– | Subtraction | Subtracts(-) right-hand operand from left-hand operand. | A – B |
* | Multiplication | Multiplies(*) values on either side of the operator. | A * B |
/ | Division | Divides left-hand operand by right-hand operand. | B / A |
% | Modulus | Divides left-hand operand by right-hand operand and get remainder. | B % A |
++ | Increment | Increases the value of the operand by 1. | B++ |
— | Decrement | Decreases the value of the operand by 1. | B– |
2. Relational Operators
Relational operators determine the relationship that one operand has to another operand. Relational operators evaluates the relation between the two operations and returns true if the relation exists else false.
Relational Operators list :-
Operator Symbol | Operator Name | Description | Example |
---|---|---|---|
== | equal to | Checks if the values of two operands are equal, if yes then condition becomes true otherwise returns false. | (A == B) |
!= | not equal to | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true otherwise returns false. | (A != B) |
> | greater than | Checks if the value of left operand is greater than(>) the value of right operand, if yes then condition becomes true. | (A > B) |
< | less than | Checks if the value of left operand is less than(<) the value of right operand, if yes then condition becomes true. | (A < B) |
>= | greater than or equal to | Checks if the value of left operand is greater than or equal to(>=) the value of right operand, if yes then condition becomes true. | (A >= B) |
<= | less than or equal to | Checks if the value of left operand is less than or equal to(<=) the value of right operand, if yes then condition becomes true. | (A <= B) |
3. Bitwise Operators
Java defines many bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
Bitwise operators list :-
Operator Symbol | Operator Name | Description | Example |
---|---|---|---|
& | bitwise and | Binary AND(&) Operator copies a bit to the result if it exists in both operands. | (A & B) |
| | bitwise or | Binary OR(|) Operator copies a bit if it exists in either operand. | (A | B) |
^ | bitwise XOR | Binary XOR(^) Operator copies the bit if it is set in one operand but not both. | (A ^ B) |
~ | bitwise compliment | Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits. | (~A ) |
<< | left shift | Binary Left Shift(<<) Operator. The left operands value is moved left by the number of bits specified by the right operand. | A << 2 |
>> | right shift | Binary Right Shift(>>) Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 |
>>> | zero fill right shift | Shift right zero fill(>>>) operator. The value of the left operands is moved to the right by the number of bits specified by the right operand and shifted values are filled up with zeros. | A >>>2 |
4. Logical Operators
Logical Operators list :-
Operator Symbol | Operator Name | Description | Example |
---|---|---|---|
&& | logical and | Called Logical AND(&&) operator.The condition become true only if both the operands are non-zero. | (A && B) |
|| | logical or | Called Logical OR(||) Operator. If any of the two operands are non-zero, the condition becomes true. | (A || B) |
! | logical not | Called Logical NOT(!) Operator. Use to reverses the logical position of its operand. If a condition is true then Logical NOT(!) operator will make false. | !(A && B) |
5. Assignment Operators
Assignment Operators list :-
Operator Symbol | Description | Example |
---|---|---|
= | Simple assignment(=) operator. Specifies the values from right side operands to left side operand. | C = A + B |
+= | Add and assignment(+=) operator. This will add right operand to the left operand and assigns the result to left operand. | C += A |
-= | Subtract and assignment(-=) operator. This will subtract right operand from the left operand and assign the result to left operand. | C -= A |
*= | Multiply and assignment(*=) operator. This will multiplies right operand with the left operand and assigns the result to left operand. | C *= A |
/= | Divide and assignment(/=) operator. This will divides left operand with the right operand and assigns the result to left operand. | C /= A |
%= | Modulus and assignment(%=) operator. It takes modulus using two operands and assigns the result to the left operand. | C %= A |
<<= | Left shift and assignment (<<=) operator. | C <<= 2 |
>>= | Right shift and assignment (>>=) operator. | C >>= 2 |
&= | Bitwise and assignment(&=) operator. | C &= 2 |
^= | bitwise exclusive OR and assignment(^=) operator. | C ^= 2 |
|= | bitwise inclusive OR and assignment(|=) operator. | C |= 2 |
6. Miscellaneous Operators
There are a few other operators supported by the Java Language.
a. Conditional/Ternary Operator ( ? : )
Conditional operator is also known as the (?:) ternary operator. This operator has three operands and is used to evaluate Boolean expressions.
Example
variable x = (expression) ? value if true : value if false
b. instanceof Operator
This operator is only used for object reference variables. The operator checks whether the object is of a particular type (interface type or class type).
Example
(Object reference variable) instanceof (class/interface type)
c. Precedence of Java Operators
The operator’s precedence determines the grouping of the terms in the expression. This affects how an expression is evaluated. Some operators are higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator.
Precedence of Java Operators list :-
Category | Operator | Associativity |
---|---|---|
Postfix | expression++ expression– | Left to right |
Unary | ++expression –-expression +expression –expression ~ ! | Right to left |
Multiplicative | * / % | Left to right |
Additive | + – | Left to right |
Shift | << >> >>> | Left to right |
Relational | < > <= >= instanceof | Left to right |
Equality | == != | Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Conditional | ?: | Right to left |
Assignment | = += -= *= /= %= ^= |= <<= >>= >>>= | Right to left |
Question :-
- What is Arithmetic Operators in Java?
- What is Logical Operators in Java?
- What are Compound Assignment Operators?
- What is the difference between ++x and x++ under increment operators?
- What are Bitwise Logical Operators?
0 Comments