"Hi, Bilaabo!"
"Bilaabo is happy to greet his friend!"
"Today Bilaabo will tell you about operator precedence. But first he will tell you about the operators themselves."
"What are these operators you speak of?"
"Actually, you're already familiar with them. You just might not have known they are called operators."
"For example, say we have the expression c = a + b;"
"It has two operators: an addition operator and an assignment operator."
"In other words, operators are just mathematical signs? Like multiplication, division, and addition?"
"Yes, that is true. However, there are differences."
"I won't define operators for you: a definition won't make you smarter. It's better to see them in action. Operators can be divided into several groups, which we'll examine now."
1) "Mathematical operators"
Symbol | Example | Name | Description (what it does) |
---|---|---|---|
+ |
a + b |
Addition operator | Addition operator. |
- |
c - d |
Subtraction operator | Subtracts the second number from the first. |
* |
a * t |
Multiplication operator | Multiplies two numbers. |
/ |
a / b |
Division operator | Divides the first number by the second. |
% |
c % d |
Divides the first number by the second. | Calculates the remainder after dividing the first number by the second. |
- |
-a |
Unary minus | Changes the variable's sign to its opposite. Plus to minus, and minus to plus. |
+ |
+a |
Unary plus | Doesn't change anything. This was added to compliment the unary minus operator. It's just for looks. |
"I recognize these from school. My firmware includes school courses."
"But, what's with that percent sign, some sort of wizardry?"
"It's the "remainder after division" operator. If 11 is divided by 5, then we get 2 and a remainder of 1. This 1 can be obtained by writing 11 % 5;"
"When you divide integers in Java, the result is also an integer. The remainder from the division operation is simply ignored. If we divide 8 by 5, we get 1."
Expression | Result | |
---|---|---|
19 / 10 |
1 | If we divide 19 by 10, the result is 1 with a remainder of 9. |
19 % 10 |
9 | If we divide 19 by 10, the result is 1 with a remainder of 9. |
2 / 5 |
0 | If we divide 2 by 5, the resultis 0 with a remainder of 2. |
16 % 2 |
0 | If we divide 16 by 2, the result is 8 with a remainder of 0. |
"But what in the world do we need the remainder for?"
"Suppose you need to check whether an integer is even. Then you can write:"
if (a % 2 == 0)
"And if you need to check if b is odd, then you can write:"
if (b % 2 == 1)
"Or to check whether d is divisible by 3:"
if (d % 3 == 0)
"Interesting. I'll remember that."
2) "Comparison operators"
Symbol | Example | Name | Description (what it does) |
---|---|---|---|
< |
a < b |
Less than | Checks to see if a is less than b. |
<= |
c <= d |
Less than or equal to | Checks to see if c is less than or equal to d. |
> |
a > b |
Greater than | Checks to see if a is greater than b. |
>= |
c >= d |
Greater than or equal to | Checks to see if c is greater than or equal to d. |
== |
i == j |
equals | Checks to see if i is equal to j. |
!= |
a != 0 |
Not equal to | Checks to see if a is not equal to zero. |
"I already use all of these."
"And what's the main difference between these operators and the mathematical operators?"
"If we add two numbers, we get a number; but, if we compare two numbers, we get true or false."
"That's right. The result of a comparison is a "logical value" value, which, as you know, is represented by the boolean type. There are two possibilities: true or false.
"Yep, that's all clear. I know that already."
3) "Logical operators"
Symbol | Example | Name | Description (what it does) |
---|---|---|---|
&& |
a && b |
AND | The expression's result is true only when both a and b are true. |
|| |
c || d |
OR | The expression's result is true if either a or b is true. Both of them or at least one. |
! |
!a |
NOT | The expression's result is true only if a is false. |
"Logical operators can only be used with boolean variables or expressions."
Example | Description |
---|---|
boolean a = true; boolean b = true;if (a && b) |
The if condition is true if both values are true In other words, if both a and b are true, then the result is true. |
boolean a = true; boolean b = false;if (a || b) |
The if condition is true if at least one value is true In other words, if either a or b is true, then the result is true. |
boolean b = false;
|
The if condition is true if b is not true. In other words, if b is false, then the result is true. |
int a = 2, b = 3, c = 4;
|
If a is less than b and a is less than c, then the expression's result is true. a, b, and c are integers, but the result of comparing integers is a logical value (true, false), which means we can use logical operators. |
"I know all this already."
"Really? Let's carry on, then."
4) "Bitwise operators"
Symbol | Example | Name | Description (what it does) |
---|---|---|---|
& |
a & b |
AND | Bitwise AND |
| |
c | d |
OR | Bitwise OR |
~ |
~a |
NOT | Bitwise NOT |
^ |
a ^ b |
XOR | Bitwise "EXCLUSIVE OR" |
"Bitwise operators perform bit-by-bit operations on integers."
"What's that?"
"Each number is represented as a set of bits, and then the result is calculated as follows:"
"If the first bit of both numbers is 1, then the result's first bit will be 1."
"If the second bit of both numbers is 1, then the result's second bit will be 1. And so on."
"Is that true for all bitwise operators?"
"It's a lot simpler than that. A bit can only have two values, 0 and 1, right?"
"Right."
"Then think of 1 as true, and 0 as false. Operations on individual bits will then be nearly identical to logical operations:"
Logical expression | Bitwise expression |
---|---|
true && true == true | 1&1 == 1 |
true && false == false | 1&0 == 0 |
true || true == true | 1|1 == 1 |
true || false == true | 1|0 == 1 |
false || false = false | 0|0 == 0 |
!false == true | ~0 == 1 |
!true == false | ~1 == 0 |
"Oh! That's so easy."
"Yes, just don't forget that bitwise operations use corresponding bits from two numbers."
"Yes, I remember: the first bit of one number is paired with the first bit of the second, and the result is also written to the first bit. And the same goes for the rest of the bits."
"That's right. Do you have any other questions?"
"What's with the XOR and 'exclusive or'?"
"It's a piece of cake: when the values are different, it's true; when they are the same, it's false."
Logical expression | Bitwise expression |
---|---|
true XOR true == false | 1 ^ 1 == 0 |
false XOR false == false | 0 ^ 0 == 0 |
true XOR false == true | 1 ^ 0 == 1 |
false XOR true == true | 0 ^ 1 == 1 |
Here are a couple more examples of bitwise operations:
Example | Numbers as bits | Result as bits | Result |
---|---|---|---|
5 & 3 |
00000101 & 00000011 | 00000001 | 1 |
7 & 2 |
00000111 & 00000010 | 00000010 | 2 |
5 | 9 |
00000101 | 00001001 | 00001101 | 13 |
5 ^ 9 |
00000101 ^ 00001001 | 00001100 | 12 |
~9 |
~00001001 | 11110110 | 246 |
"Thank you, Bilaabo. Now I know."
"There is one more group of bitwise operators, the shift operators:"
5) "Shift operators"
Symbol | Example | Name | Description (what it does) |
---|---|---|---|
>> |
a >> b |
right shift | Shifts the bits of the number a to the right by b digits. |
<< |
c << d |
left shift | Shifts the bits of the number c to the left by d digits. |
>>> |
a >>> 2 |
unsigned right shift | Shifts the bits of the number a to the right by 2 digits. |
"What kind of street magic is this?"
"It's actually all really simple. Check it out:"
Example | Numbers as bits | Result as bits | Result |
---|---|---|---|
10 >> 1 |
00001010 >> 1 | 00000101 | 5 |
10 >> 2 |
00001010 >> 2 | 00000010 | 2 |
10 << 1 |
00001010 << 1 | 00010100 | 20 |
10 << 2 |
00001010 << 2 | 00101000 | 40 |
"Shifting a number's bits to the left by 1 is the same as multiplying the number by 2. Shifting by two digits is equivalent to multiplication by 4, by three digits — multiplication by 8, and so on."
"Shifting right corresponds to dividing by 2, 4, 8, 16, etc."
"But what's the difference between the >>> and >> operators?"
"They differ when working with negative numbers. This is because signed numbers use the leftmost bit to indicate the sign. As a result, a negative number ceases to be negative when shifting to the right. So, they came up with two different operators. Check it out:"
Expression | Result | Description |
---|---|---|
10001010 >> 1 | 11000101 | The negative number stays negative. For negative numbers, the incoming bits are filled with 1s. |
10001010 >> 2 | 11100010 | |
10001010 >> 3 | 11110001 | |
10001010 >>> 1 | 01000101 | The negative number is no longer negative. For negative numbers, the incoming bits are filled with 0s. |
10001010 >>> 2 | 00100010 | |
10001010 >>> 3 | 00010001 |
"The shift is not cyclic. Bits that move beyond the left or right edge of the number are simply discarded."
6) "Assignment operators"
"I already know what assignment is. But why do you say 'operators'?"
"Because there are several of them ☺"
Operator | What it means |
---|---|
a += b; |
a = a + b; |
a -= b; |
a = a - b; |
a *= b; |
a = a * b; |
a %= b; |
a = a % b; |
a |= b; |
a = a | b; |
a &= b; |
a = a & b; |
"I think you get the logic."
7) "Increment and decrement operators"
Notation | Example | Description |
---|---|---|
++ |
a++; ++b; |
Increases the number by 1. |
-- |
d--; --i; |
Decreases the number or variable by 1. |
"Is there a difference between putting the two minus signs before or after the variable?"
"Yes, there is, though not very big. If a variable with one of these operators is a part of an expression or assignment, then there are differences. I'd rather show you by example:"
Example | What really happens | Description |
---|---|---|
int a = 3; int b = ++a; |
int a = 3; a = a + 1; int b = a; |
a is first increased by 1, and then it is used in the expression. |
int a = 3; int b = a++; |
int a = 3; int b = a; a = a + 1; |
a is first used in the expression, and then increases by 1. |
int a = 3; return a++; |
int a = 3; int result = a; a = a + 1; return result; |
The function will return 3, but the value of a will be increased by 1. |
int x = 5; x = ++x + ++x; |
int x = 5; int a = x + 1;// The first term is 6 x = a; int b = x + 1;// The second term is 7 x = b; x = a + b; |
The result here is 13. First, x will increase by 1, and this value will replace the first term, and then x will again increase by 1. |
"Whoa! That's cool!"
"I'm glad you liked it. But, if there is no expression or assignment, then there are no differences:"
"x++ is equivalent to x = x + 1."
"++x is equivalent to x = x + 1."
"I'll keep that in mind. Thanks, Bilaabo."
8) "Ternary operator"
"This operator doesn't just use one or two variables or expressions. It uses three variables or expressions all at once:"
Notation | Equivalent code: |
---|---|
a ? b : c; |
if (a) b else c |
int min = a < b ? a : b; |
if (a < b) min = a; else min = b; |
return a != null ? a.length : 0; |
if (a != null) return a.length; else return 0; |
"Well, that's super convenient."
"Yes. And it's compact and the code is readable. Enjoy using it!"
9) "Other"
"No matter how well organized your music collection, you still have to create a "Miscellaneous" folder."
"Yeah, anyone who's ever had to categorize music would totally agree."
"So, there are three more operators I want to tell you about:"
Notation | Example | Description |
---|---|---|
() |
(a + b) * c |
Parentheses increase operator precedence. Things in parentheses are executed first. |
[] |
c [i] = c [i + 1]; |
Get an array element by index. |
. |
int n = a.length; |
The "dot operator" accesses an object's variables and methods. |
"And, finally, here's a table that summarizes operator precedence:"
Operators | Examples |
---|---|
Highest precedence (operators are executed according to their order in this table) | |
() [] . |
(a + b) c [i] = c [i] + 1 |
++ -- ~ ! + - |
i++; ++i; --j; a--; ~c !f return +a; return -a; |
* / % |
a * b c / d a % b |
+ - |
a + b c - d String s = "count"+"35"; |
>> << >>> |
a >> 3 b << 2 c >>> 3 |
< <= > >= |
a < b a <= b c > b c >= b |
== != |
a == 3 a != 0 |
& |
a & 7 |
^ |
a ^ b |
| |
a | b |
&& |
(a < b) && (a < c) |
|| |
(b != 0) || (c != 0) |
? : = |
a > 0 ? a : -a; |
= *=, /=, %= -=, += <<=. >>=, >>>= &=, ^=. |= |
|
Lowest precedence (performed last) |
GO TO FULL VERSION