Operators
Every programming language provides built-in operators for performing arithmetic, logic and bitwise operations. You will also find operators to compare values and assign value to a variable, subscript arrays and invoke functions. Depending on the language you may find referencing/dereferencing, member selection, pattern matching, range or type casting operators. Many of these operators will be explored in this section, but some will be deferred until later sections where their use becomes necessary.
Typecasting
In strongly typed languages like Java we often need to convert a value of one type to a value of a different type. For example, we might want to convert a double to an integer, or convert an integer to a string.
To convert among numeric types we use typecasts. Typecasts are a unary operator composed of a typename enclosed in parens placed before the value you want to typecase. Casting to smaller or less precise type is known as narrowing. Casting to a larger or more precise type is known as widening.Method | Description |
---|---|
i = Math.ceil(fp) | Round floating point up to integer |
i = Math.floor(fp) | Round floating point down to integer |
i = Math.round(fp) | Round floating point to nearest integer |
i = int(fp) | Convert to int by truncting (round toward zero) |
l = long(fp) | Convert to long by truncating (round toward zero) |
fp = double(i) | Convert to floating point |
i = Integer.parseInt(s) | Convert string to int |
l = Long.parseLong(s) | Convert string to long |
fp = Double.parseDouble(s) | Convert string to double |
s = Integer.toString(x) | Convert int to string |
s = Long.toString(x) | Convert long to string |
s = Double.toString(x) | Convert double to string |
s = Objects.toString(o) | Convert Object to String |
Arithmetic Operators
[[Arithmetic operations]] are one of the two major groups of operations that computers perform for us, the other being [[logical operations]]. Arithmetic operators include multiplication, division, remainder (modulus), addition, subtraction and negation (unary minus). Most of these operations should be familiar as they are the basic arithmetic operations taught in elementary school. Modulus is less familiar but it is simply the remainder after division. Remember those times studying long division where we ended up with a remainder? That remainder is the modulus.
The arithmetic operators are mostly similar to the symbols typically used in mathematics: addition (+
), subtraction (-
), division (/
). Only multiplication (*
) and modulus (%
) differ from standard mathematical notation. Unlike standard mathematical notation where placing two symbols next to each other implies multiplication as in xyz (which means x × y × z), when writing code we must explicitly use the multiplication operator as in x * y * z
.
public class Operators1 { public static void main(String[] args) { final int a = 3; final int b = 5; final int c = 2; final double d = 3.1415926; final double e = 4.0; System.out.println("-b = " + (-b)); System.out.println("a + b = " + (a + b)); System.out.println("a + -b = " + (a + -b)); System.out.println("-a + b = " + (-a + b)); System.out.println("-a + -b = " + (-a + -b)); System.out.println("a - b = " + (a - b)); System.out.println("a - -b = " + (a - -b)); System.out.println("-a - b = " + (-a - b)); System.out.println("-a - -b = " + (-a - -b)); System.out.println("b * c = " + (b * c)); System.out.println("b * -c = " + (b * -c)); System.out.println("-b * c = " + (-b * c)); System.out.println("-b * -c = " + (-b * -c)); System.out.println("b / c = " + (b / (double)(c))); System.out.println("b / -c = " + (b / (double)(-c))); System.out.println("-b / c = " + (-b / (double)(c))); System.out.println("-b / -c = " + (-b / (double)(-c))); System.out.println("b // c = " + (b / c)); System.out.println("b // -c = " + (b / -c)); System.out.println("-b // c = " + (-b / c)); System.out.println("-b // -c = " + (-b / -c)); System.out.println("b % a = " + (b % a)); System.out.println("b % -a = " + (b % -a)); System.out.println("-b % a = " + (-b % a)); System.out.println("-b % -a = " + (-b % -a)); System.out.println("d + e = " + (d + e)); System.out.println("d - e = " + (d - e)); System.out.println("d * e = " + (d * e)); System.out.println("e / d = " + (e / d)); System.out.println("e // d = " + ((int)(e / d))); } }
Output
String Concatenation
String concatenation is a special operator that is used to combine two strings into a single string. We use the
plus (+
)
symbol to represent this special operation. For example:
public class Operators2 { public static void main(String[] args) { final String a = "Hello, "; final String b = "world"; final String c = "Bruce"; final String d = "Sheriff Brody"; final String e = "!"; System.out.println(a + b + e); System.out.println(a + c + e); System.out.println(a + d + e); } }
Output
Comparison Operators
The comparison operators less than, greater than, equal, not equal, etc. allow us to determine how two values compare to each other. They are fundamentally arithmetic in nature. This is because comparisons are subtraction operations that then look at whether the difference is negative, positive or zero.
The symbol to check equality is (==
). Note that there are two equal signs not just one which has a different meaning, namely assignment. The symbol to check if two values are not equal is (!=
). The symbols for less than (<
) and greater than (>
) are as we would expect. The symbols for less than or equal (<=
) and greater than or equal (>=
) add the equal sign to the less than or greater than symbols. For these symbols the equal sign always appears on the right.
public class Operators3 { public static void main(String[] args) { final int a = 3; final int b = 5; final double c = 3.1415926; final double d = 4.0; final String e = "apple"; final String f = "pear"; System.out.println("a < b : " + (a < b)); System.out.println("a <= b : " + (a <= b)); System.out.println("a > b : " + (a > b)); System.out.println("a >= b : " + (a >= b)); System.out.println("a == b : " + (a == b)); System.out.println("a != b : " + (a != b)); System.out.println("c < d : " + (c < d)); System.out.println("c <= d : " + (c <= d)); System.out.println("c > d : " + (c > d)); System.out.println("c >= d : " + (c >= d)); System.out.println("c == d : " + (c == d)); System.out.println("c != d : " + (c != d)); System.out.println("e < f : " + (e.compareTo(f) < 0)); System.out.println("e <= f : " + (e.compareTo(f) <= 0)); System.out.println("e > f : " + (e.compareTo(f) > 0)); System.out.println("e >= f : " + (e.compareTo(f) >= 0)); System.out.println("e == f : " + (e.equals(f) )); System.out.println("e != f : " + (!e.equals(f) )); } }
Output
Logical Operators
The logical operators allow us to perform [[Boolean]] arithmetic. We have the logical operators AND (&&), OR (||) and NOT (!) that can be used to form any logical expression needed. The logical operators operate on and return the values false for falsity and true for truth.
|
|
|
public class Operators4 { public static void main(String[] args) { final boolean a = true; final boolean b = false; System.out.println("a AND b : " + (a && b)); System.out.println("a OR b : " + (a || b)); System.out.println("NOT a : " + (!a)); System.out.println("NOT b : " + (!b)); } }
Output
Bitwise Operators
We also have [[bitwise operators]] that work on the premise that each bit (0 or 1) in a value can be considered either false or true, respectively. Bitwise operators operate on integer values and perform their operation for each pair of bits (for binary operations) in the value. Some examples using 4-bit values are illustrated below.
|
|
|
|
import org.pureprogrammer.Utils; public class Operators5 { public static void main(String[] args) { final int a = 0x5555AAAA; final int b = 0x36C36CF; System.out.println(Utils.format("Bitwise a AND b : {0:08x}", (a & b))); System.out.println(Utils.format("Bitwise a OR b : {0:08x}", (a | b))); System.out.println(Utils.format("Bitwise a XOR b : {0:08x}", (a ^ b))); System.out.println(Utils.format("Compliment a : {0:08x}", (~a))); System.out.println(Utils.format("Compliment b : {0:08x}", (~b))); } }
Output
Increment and Decrement
The increment (++
) and decrement (--
)operators modify a variable by adding or subtracting one respectively. These unary operators can be applied in front of (prefix) or behind (postfix) the variable symbol. The prefix operators return the new value of the variable whereas the postfix form returns the original value of the variable. In this ways the increment and decrement operators can be used to modify a variable but also can also be used in expressions. It can be tricky to use them correctly so it is generally best to only use a variable once in an expression if it has increment or decrement applied to it.
Assignment Operators
Assignment operators allow us to change the value of a variable. The assignment operator is represented by a single equal sign (=
). It should not be confused with the equivalence comparison operator which is represented by a double equal sign (==
).
In addition to simple assignment we also have variations on the assignment operator that perform an operation before assignment. These shorthand assignment operators are listed below showing the equivalent using only simple assignment.
Operator | Equivalent |
---|---|
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 |
A |= B | A = A | B |
A ^= B | A = A ^ B |
A <<= B | A = A << B |
A >>= B | A = A >> B |
public class Operators6 { public static void main(String[] args) { int a = 3; ++a; System.out.println("++a: " + a); --a; System.out.println("--a: " + a); a += 2; System.out.println("a += 2 : " + a); a *= 3; System.out.println("a *= 3 : " + a); a -= 4; System.out.println("a -= 4 : " + a); a /= 5; System.out.println("a /= 5 : " + a); } }
Output
Conditional Operator
The only ternary operator (takes three operands) is the conditional operator. Based on the truth of the first operator, it will yield either the value of the second or third operator. The expression
A ? B : C
yields B if A is true and C if A is false.
public class Operators7 { public static void main(String[] args) { final int a = 3; final int b = 5; System.out.println("a < b ? -1 : 1 => " + (a < b ? -1 : 1)); System.out.println("b < a ? -1 : 1 => " + (b < a ? -1 : 1)); System.out.println("a < b ? 'less' : 'greater' => " + (a < b ? "less" : "greater")); System.out.println("b < a ? 'less' : 'greater' => " + (b < a ? "less" : "greater")); } }
Output
Type Casting
Type casting operations allow us to convert from one data type to another. They are most often used to convert from a more precise numeric type to a less precise type such as converting from a floating point value to an integer value. They also can be used to convert numbers to strings.
import org.pureprogrammer.Utils; public class Operators8 { public static void main(String[] args) { final String sFloat = "3.1415926"; final String sInt = "3"; final String sLong = "123456789123456789"; final double a = Utils.stodWithDefault(sFloat, 0.0); final int b = Utils.stoiWithDefault(sInt, 0); final long c = Utils.stolWithDefault(sLong, 0); final String strA = String.valueOf(a); final String strB = String.valueOf(b); final String strC = String.valueOf(c); System.out.println(Utils.format("sFloat as double: {0:f}", a)); System.out.println(Utils.format("sInt as int: {0:d}", b)); System.out.println(Utils.format("sLong as long: {0:d}", c)); System.out.println(Utils.format("a as string: {0:s}", strA)); System.out.println(Utils.format("b as string: {0:s}", strB)); System.out.println(Utils.format("c as string: {0:s}", strC)); } }
Output
Precedence
For simple expressions that contain only one operator, it is easy to see how it will be evaluated. But more complex expressions that contain multiple operators could lead to ambiguity and unpredictable results unless we had some rules in place to help us out. These rules to eliminate ambiguity are the rules of precedence illustrated below. In this table, operators which have higher precedence appear closer to the top. Operators with lower precedence appear closer to the bottom. In a situation where two operators are potentially operating on a value, the operator that is higher in the table takes precedence over a lower operator. If the two operators are on the same level of precedence then the associativity of the operators helps us to determine which takes precedence.
For example, the expression 5 * 3 + 1
could be ambiguous. Without rules of precedence it wouldn't be clear weather the multiplication or the addition should be performed first. If the multiplication takes precedence we would have (5 * 3) + 1 == 16
whereas if the addition takes precedence we would have 5 * (3 + 1) == 20
. But we do have rules of precedence so there is only one correct way to interpret that expression. Because multiplication appears in the table on a level above addition, we know that the first interpretation yielding 16 is the correct one. If we instead wanted the expression to be evaluated the second way to yield 20, we would have to use parenthesis to explicitly override the rules of precedence. Many of the rules of precedence come from standard arithmetic and logical notation so they should seem familiar.
Rules of precedence don't guarantee any particular order of evaluation, but they do help resolve issues when two operators are applied to the same value. This subtlty is not obvious because in most expressions there is only one order of evaluation that follows the rules of precedence. Consider (10 - 6) - (3 - 1)
. At first glance it would appear that the parenthesis cause this expression to be evaluated in only one way. But it turns out that there are two ways to evaluate this expression without violating the rules of precedence, namely to compute (10 - 6)
first or to compute (3 - 1)
first. So there are two ways to order the evaluation of this expression without violating the rules of precedence. In this case as in most cases, the end result will be the same, but it illustrates that the rules of precedence still leave some leeway in how the computer carries out the evaluation of the expression.
Where we get into trouble is with the operators that change a variable, namely the increment (++
), decrement (--
) and assignment operators. When these operators are used in an expression, leeway in when that operation is performed can cause the entire expression to yield different results. Let us assume that we have a variable A that is set to 1. Then we consider the expression ++A - ++A
. According to the rules of precedence there is no ambiguity in this expression. The first A has a prefix increment (++
) applied on the left and subtraction (-
) on the right. Since unary operators take precedence over subtraction we know that the increment must be applied before the addition. The second A only has one operator applied, the prefix increment so that must be applied before its result is added to the result of the first A after it is incremented. So here is the problem, we need to know when the first A is incremented to know what value is represented by the second A. The computer is free to evalutate the first ++A
yielding 2 then the second ++A
yielding 3. Equally it is free to evaluate the second ++A
first yielding 2 then the first yielding 3. This would give us either 2 - 3
or 3 - 2
neither of which violate any of the rules of precedence. So in this case there is a reliance on knowing the order of evaluation which leads to ambiguity. Just because it evaluates one way doesn't mean that it will always evaluate that way. A new version of the language tools can cause the expression to change how it is evaluated leading to a difficult bug to track down. So to avoid this problem we have a rule of thumb to never use a variable more than once in an expression if it has had an operator applied that produces the side effect of changing that variable.
Consider the expression 10 * 6 - 4 / 2
. Two of the values have operators on both the left and the right side requiring us to use the rules of precedence. Two values do not. Use parenthesis to show how the rules of precedence require us to interpret this expression.
Answer: ((10 * 6) - (4 / 2)) == (60 - 2) == 58
Note: It is equally correct for (10 * 6)
to be evaluated first or for (4 / 2)
to be evaluated first. Because the expressions do not have any side effects, the result will be the same either way.
Consider the expression A + B << 2 * 3 & 0xff
. Two of the values have operators on both the left and the right side requiring us to use the rules of precedence. Two values do not. Use parenthesis to show how the rules of precedence require us to interpret this expression.
Answer:
(((A + B) << (2 * 3)) & 0xff)
Operator | Description | Associativity |
---|---|---|
++ -- | Postfix increment and decrement | Left-to-Right |
() | Function/Method call | |
[] | Array subscripting | |
. | Member selection | |
++ -- | Prefix increment and decrement | Right-to-Left |
+ - | Unary plus and minus | |
! ~ | Logical NOT and bitwise compliment | |
(type) | Type cast | Right-to-Left |
new | Dynamic object allocation | |
* / % | Multiplication, division, and remainder | Left-to-Right |
+ - | Addition and subtraction | Left-to-Right |
+ | String concatenation | |
<< >> | Bitwise arithmetic left and right shift | Left-to-Right |
>>> | Bitwise logical (unsigned) right shift | |
< <= | Comparison operators < and ≤ | Left-to-Right |
> >= | Comparison operators > and ≥ | |
instanceof | Type determination | |
== != | Equivalence = and ≠ | Left-to-Right |
& | Bitwise AND | Left-to-Right |
^ | Bitwise XOR (exclusive OR) | Left-to-Right |
| | Bitwise OR (inclusive OR) | Left-to-Right |
&& | Logical AND | Left-to-Right |
|| | Logical OR | Left-to-Right |
?: | Ternary conditional | Right-to-Left |
= | Direct assignment | Right-to-Left |
*= /= %= | Assignment by product, quotient, and remainder | |
+= -= | Assignment by sum and difference | |
<<= >>= >>>= | Assignment by bitwise left shift and right shift | |
&= ^= |= | Assignment by bitwise AND, XOR, and OR |
As we have seen, we can use parenthesis to change how an expression is to be interpreted. Using parenthesis can also make an expression easier to read. Adding parenthesis to an equation doesn't incur any performance penalty even if the expression would have been interpreted the desired way without the parenthesis.
TODO: Precision and rounding errors in operations 1/3
Questions
- {{Write an expression that yields 4x2+3x+1.}}
- {{Write an expression that yields 5x3+2x2-x-3.}}
- {{Write an expression that yields the ratio of x+1 to x+3.}}
- Write an expression that yields 32x using a bit shift operator.
- Write an expression that yields x/16 using a bit shift operator.
- Write an expression that yields 1 if x is odd, 0 if x is even.
- Write an expression that yields true if the value in x is even.
- Write an expression that yields true if the value in x is odd.
- Write an expression that yields true if the value in x is a multiple of 7.
- Write an expression that yields 0 if x is a multiple of 10, otherwise yield 1.
- Write an expression that yields 0 if x is less than or equal to 4000, otherwise yield x-4000.
- Write an expression that yields true if the value of x is 326.
- Write an expression that yields true if the value of x is not 326.
- Write an expression that yields the integer part of a floating point value in x.
- Show that x ≤ 5 is equivalent to x - 5 ≤ 0. Show how all the comparison operators are equivalent to a subtract and a comparison to 0.
- Compute the value of the following expressions using the rules of precedence given above:
- {{2 * 5 - 3}}
- {{8 % 3 * 2}}
- {{3 << 2 * 3}}
- {{8 >> 1 + 1}}
- {{8 / 2 - 1}}
- {{5 + -2 - +1}}
- {{4 - 2 * 5 - 2}}
- {{(4 - 2) * 5 - 2}}
- {{4 - 2 * (5 - 2)}}
- {{(4 - 2) * (5 - 2)}}
- {{4 - (2 * 5 - 2)}}
- {{Write an expression that is true if A or B is true but not both. (This is called exclusive OR)}}
Projects
More ★'s indicate higher difficulty level.
- Absolute Value
- Ferengi Currency Conversion
- Incrementing a Variable
- Jackpot Winnings
- Leap Year Determination
- Leap Year Determination
- Light-Year Distance
- Multiplication Table with Format
- Quadradic Equations
- Sales Tax
- Seconds in a Year
- Universe Size
References
- [[Java Language Specification]], Java SE 17 Edition, Gosling, et. al., 2021.
- [[Java Tutorials]]
- [[Java at TutorialsPoint]]
- Download Java at [[Amazon Corretto]], [[Azul Zulu]], [[Eclipse Temurin]] or [[Oracle JDK]]