Java Tutorial – 05 – Operators
Operators are used to operate on values. They can be grouped into five types: arithmetic, assignment, comparison, logical and bitwise operators.
Arithmetic operators
There are the four basic arithmetic operators, as well as the modulus operator (%) which is used to obtain the division remainder.
float x = 3+2; // 5 // addition x = 3-2; // 1 // subtraction x = 3*2; // 6 // multiplication x = 3/2; // 1 // division x = 3%2; // 1 // modulus (division remainder)
Notice that the division sign gives an incorrect result. This is because it operates on two integer values and will therefore round the result and return an integer. To get the correct value, one of the numbers must be explicitly converted into a floating-point type.
float x = (float)3/2; // 1.5
Assignment operators
The second group is the assignment operators. Most importantly, the assignment operator (=) itself, which assigns a value to a variable.
Combined assignment operators
A common use of the assignment and arithmetic operators is to operate on a variable and then to save the result back into that same variable. These operations can be shortened with the combined assignment operators.
int x = 0; x += 5; // x = x+5; x -= 5; // x = x-5; x *= 5; // x = x*5; x /= 5; // x = x/5; x %= 5; // x = x%5;
Increment and decrement operators
Another common operation is to increment or decrement a variable by one. This can be simplified with the increment (++) and decrement (--) operators.
++x; // x += 1 --x; // x -= 1
Both of these can be used either before or after a variable.
++x; // pre-increment --x; // pre-decrement x++; // post-increment x--; // post-decrement
The result on the variable is the same whichever is used. The difference is that the post-operator returns the original value before it changes the variable, while the pre-operator changes the variable first and then returns the value.
x = 5; y = x++; // y=5, x=6 x = 5; y = ++x; // y=6, x=6
Comparison operators
The comparison operators compare two values and return either true or false. They are mainly used to specify conditions, which are expressions that evaluate to either true or false.
boolean x = (2==3); // false // equal to x = (2!=3); // true // not equal to x = (2>3); // false // greater than x = (2<3); // true // less than x = (2>=3); // false // greater than or equal to x = (2<=3); // true // less than or equal to
Logical operators
The logical operators are often used together with the comparison operators. Logical and (&&) evaluates to true if both the left and right side are true, and logical or (||) is true if either the left or right side is true. The logical not (!) operator is used for inverting a Boolean result. Note that for both “logical and” and “logical or” the right side will not be evaluated if the result is already determined by the left side.
boolean x = (true && false); // false // logical and x = (true || false); // true // logical or x = !(true); // false // logical not
Bitwise operators
The bitwise operators can manipulate individual bits inside an integer. For example, the right shift operator (>>) moves all bits except the sign bit to the right, whereas zero-fill right shift (>>>) moves all bits right including the sign bit.
int x = 5 & 4; // 101 & 100 = 100 (4) // and x = 5 | 4; // 101 | 100 = 101 (5) // or x = 5 ^ 4; // 101 ^ 100 = 001 (1) // xor x = 4 << 1;// 100 << 1 =1000 (8) // left shift x = 4 >> 1;// 100 >> 1 = 10 (2) // right shift x = 4 >>>1;// 100 >>>1 = 10 (2) // zero-fill // right shift x = ~4; // ~00000100 = 11111011 (-5) // invert
These bitwise operators have shorthand assignment operators, just as the arithmetic operators.
int x = 5; x &= 5; // "and" and assign x |= 5; // or and assign x ^= 5; // xor and assign x <<= 5; // left shift and assign x >>= 5; // right shift and assign x>>>= 5; // right shift and assign (move sign bit)
Operator precedence
In Java, expressions are normally evaluated from left to right. However, when an expression contains multiple operators, the precedence of those operators decides the order in which they are evaluated.
However, different operators have different precedence that also determines the order in which they are evaluated. The order of precedence can be seen in the table below. This same order also applies to many other languages, such as C++ and C#.
| Precedence | Operator | Precedence | Operator |
|---|---|---|---|
| 1 | ++ — ! ~ | 7 | & |
| 2 | * / % | 8 | ^ |
| 3 | + - | 9 | | |
| 4 | << >> >>> | 10 | && |
| 5 | < <= > >= | 11 | || |
| 6 | == != | 12 | = op= |
For example, logical and (&&) binds weaker than relational operators, which in turn binds weaker than arithmetic operators.
x = 2+3 > 1*4 && 5/5 == 1; // true
To avoid having to learn the precedents of all operators and to clarify the intent, parentheses can be used to specify which part of the expression will be evaluated first. Parentheses have the highest precedence of all operators.
x = ( (2+3) > (1*4) ) && ( (5/5) == 1 ); // true
If you like this tutorial please +1 it:


![[Affiliate link] Total Training]( http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/totaltraining.png)
![[Affiliate link] Lynda](http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/lynda.png)


Thanks for pointing that out. I’ve fixed the mistake in the code so that the expression now properly evaluates to true. I also added an annotation to the video with the correction.
the above mentioned expression has the value false not true as the comment says