Home >Java >javaTutorial >Java Operators
The following article provides an outline for Java Operators. Java operator denotes a symbol that helps in performing several operations on one or more than one operand. Operators can be +, -, /, *, etc., depending on the requirement.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
There are different types of operators.
Given below are the types of operators in java:
Arithmetic Operators are used to perform several arithmetic operations.
The following are the arithmetic operators in Java.
Operator Name/ Symbol | Definition | Example |
Addition(+) | It helps in adding two values. | A+B |
Subtraction(-) | It helps in subtracting two values. | A-B |
Modulus(%) | It helps in getting the remainder obtained by dividing two values. | A%B |
Multiplication( * ) | It helps in multiplying two values. | A*B |
Division ( / ) | Helps in dividing two values. | A/B |
Bitwise operators are commonly used in performing bit shift and bitwise operations in Java.
Below are the Bitwise operators that are commonly used.
Operator Name/ Symbol | Definition | Example |
Bitwise AND operator (&) | It helps in comparing 2 operand’s corresponding bits. 1 will be returned if both the bits are 1; else 0 will be returned. | A&B |
Bitwise OR operator (|) | It helps in comparing 2 operand’s corresponding bits. 1 will be returned if one of the bit is 1; else 0 will be returned. | A|B |
Bitwise XOR operator (^) | It helps in comparing 2 operand’s corresponding bits. 1 will be returned if the corresponding bits are dissimilar, else 0 will be returned. | A^B |
Bitwise complement operator (~) | It helps in inverting the pattern of bits. That is, 1 will be changed to 0 and 0 will be changed to 1. | ~B |
Assignment Operators are used for assigning values to certain variables.
The following are the assignment operators in Java.
Operator Name/ Symbol | Definition | Example |
-= | It helps subtract the right and left operators, thereby assigning the obtained result to the operator on the left. | A-=B |
/= | Helps in dividing the right and left operators and thereby assigning the obtained result to the operator in the left. | A/=B |
*= | It helps multiply right and left operators and thereby assign the obtained result to the operator on the left. | A*=B |
+= | It helps add right and left operators and thereby assign the obtained result to the operator on the left. | A+=B |
^= | Left operand value will be raised to the right operator power. | A^=B |
%= | A modulus operator will be applied. | A%=B |
In Java, the Ternary operator is mainly used for if-then-else condition replacement. It is a one-line statement that is widely used in Java programming, which takes only 3 operands.
Operator Name/ Symbol | Definition | Example |
condition?v1: v2 | V1 will be returned if the condition mentioned is true, and v2 will be returned if the condition is false. | A>B?A:B |
Auto Increment and Auto Decrement operators in Java helps in incrementing and decrementing the values by 1, respectively.
Operator Name/ Symbol | Definition | Example |
++ | The value will be incremented by 1. | A++; It is similar to A+=1 |
— | The value will be decremented by 1. | A–; It is similar to A-=1 |
Relational operators are operators that helps in checking the operand’s equality. In addition to that, these operators are also used for comparing 2 or more values.
Operator Name/ Symbol | Definition | Example |
equals to(==) | If the left operand is equal to the right operand, true will be returned; else, false. | A==B |
not equal to(!=) | If the left operand is not equal to the right operand, true will be returned; else, false. | A!=B |
less than(<) | If the left operand is less than the right operand, true will be returned; else, false. | A |
greater than(>) | If the left operand is greater than the right operand, true will be returned; else, false. | A>B |
greater than or equal to(>=) | If the left operand is greater than or equal to the right operand, true will be returned else, false. | A>=B |
Less than or equal to(<=) | If the left operand is less than or equal to the right operand, true will be returned else, false. | A<=B |
Logical Operators in Java are commonly used in Boolean expressions.
Below are the logical operators in Java.
Operator Name/ Symbol | Definition | Example |
Conditional AND(&&) | If both the Boolean expressions are satisfied, true will be returned else, false. | AD |
Conditional OR(||) | If any of the Boolean expression is satisfied, true will be returned; else, false. | AD |
Logical NOT(!) | It helps in inverting the operand’s logical state. That is, 1 will be changed to 0 and 0 will be changed to 1. | !B |
Shift operators in Java are used to shift the bits left or right based on the requirement.
The following are the Shift operators.
Operator Name/ Symbol | Definition | Example |
Left Shift Operator ( << ) | X in binary notation will be shifted y bits left by shifting the zeroes from the right. | A<<2 |
Right Shift Operator ( >> ) | X in binary notation will be shifted y bits right by discarding shifted bits. | B>>2 |
Unsigned Right Shift Operator ( >>> ) | X in binary notation will be shifted y bits right by discarding shifted bits and shifting the zeroes from the right. | A>>>2 |
Let us see how the precedence of these operators will be.
Operator | Precedence |
Postfix Operators | Exp++ Exp– |
Unary Operators | ++Exp –Exp +_Exp –Exp~ ! |
Multiplicative Operators | * / % |
Additive Operators | + – |
Shift Operators | << >> >>> |
Relational Operators | < > <= >= instanceof |
Equality Operators | == != |
Bitwise AND Operator | & |
Bitwise exclusive OR Operator | ^ |
Bitwise inclusive OR Operator | | |
Logical AND Operator | && |
Logical OR Operator | || |
Ternary Operators | ?: |
Assignment Operators | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
The above is the detailed content of Java Operators. For more information, please follow other related articles on the PHP Chinese website!