Sub topic 3
Arithmetic, Remainder, and String Concatenation
Java can calculate totals, split amounts, and join text. These tasks use arithmetic operators and the + operator.
An operator is a symbol that tells Java to perform an operation. An operand is a value used by an operator. In 4 + 3, the operator is +, and the operands are 4 and 3. The whole calculation is an expression: a piece of code that produces a value.
This chapter assumes you know how to write and run a simple Java program and use variables.
Arithmetic operators
An integer is a whole number, such as 7 or -2. The table shows Java’s arithmetic operators with integer operands.
| Operator | Operation | Example | Result |
|---|---|---|---|
| `+` | Addition | `8 + 3` | `11` |
| `-` | Subtraction | `8 - 3` | `5` |
| `*` | Multiplication | `8 * 3` | `24` |
| `/` | Division | `8 / 3` | `2` |
| `%` | Remainder | `8 % 3` | `2` |
Use *, not x, for multiplication. Use / for division. The % operator does not mean “percent” in Java; it finds a remainder.
Notice that 8 / 3 gives 2, not 2.666.... The operand types matter when you divide.
Division and remainder
When both operands of / are integers, Java performs integer division. It drops the fractional part of the result. For example, 17 / 5 is 3.
The % operator gives the remainder after that division. Since five goes into seventeen three times, with two left over, 17 % 5 is 2.
You can check the result:
- Divide:
17 / 5gives3. - Multiply:
3 * 5gives15. - Find what is left:
17 - 15gives2.
This is useful when you need both a count of complete groups and the amount left over. For example, if you pack 17 apples into bags of 5, you can fill 3 bags, with 2 apples left.
Getting a fractional result
A floating-point number is a number stored in a form that can represent fractional values, such as 7.0. If at least one operand of / is floating-point, the division can produce a fractional result.
| Expression | Result |
|---|---|
| `7 / 2` | `3` |
| `7.0 / 2` | `3.5` |
| `7 / 2.0` | `3.5` |
Writing 7.0 instead of 7 changes the kind of division. Some floating-point results are approximate, so do not assume every decimal calculation is exact.
Negative remainders
Integer division drops the fractional part toward zero, even for negative results. So -17 / 5 is -3, not -4.
The remainder has the same sign as the first operand, unless the remainder is zero:
-17 % 5is-2.17 % -5is2.
For the first example, 5 * -3 is -15. Adding the remainder, -2, gives the starting value, -17.
Grouping calculations
Java uses precedence rules to decide how to group operators. Multiplication, division, and remainder group before addition and subtraction. Parentheses can change that grouping.
| Expression | Calculation | Result |
|---|---|---|
| `2 + 3 * 4` | `2 + 12` | `14` |
| `(2 + 3) * 4` | `5 * 4` | `20` |
| `17 % 5 + 1` | `2 + 1` | `3` |
Use parentheses when they make your intention clearer, even if Java would group the expression the same way without them.
Joining text with `+`
A string is a value that holds text. In Java, you write a string value inside double quotes, such as "apples".
String concatenation means joining text together. When + has a string operand, Java joins the operands as text:
"Hello, " + "Mira"produces"Hello, Mira"."Apples: " + 3produces"Apples: 3".
In the second example, Java turns the number into text for the joined result. It does not add the number to anything.
The order of the parts matters:
| Expression | Result | Why |
|---|---|---|
| `2 + 3 + " apples"` | `"5 apples"` | Java adds the numbers first. |
| `"Apples: " + 2 + 3` | `"Apples: 23"` | Java joins each number to the text. |
| `"Apples: " + (2 + 3)` | `"Apples: 5"` | Parentheses make the addition happen first. |
Also, "2" + "3" produces "23", not 5. The quotes make those values text.
Try the operations
Save this program as ArithmeticDemo.java. Compile and run it as you would another Java program.
public class ArithmeticDemo {
public static void main(String[] args) {
int apples = 17;
int bagSize = 5;
System.out.println(apples / bagSize);
System.out.println(apples % bagSize);
System.out.println(17.0 / bagSize);
System.out.println(-17 % 5);
System.out.println(2 + 3 * 4);
System.out.println((2 + 3) * 4);
System.out.println("Apples: " + 2 + 3);
System.out.println("Apples: " + (2 + 3));
}
}The first two lines of output are 3 and 2: three full bags and two apples left. The next two are 3.4 and -2: fractional division and a negative remainder.
The remaining lines are 14, 20, Apples: 23, and Apples: 5. Before running the program again, change apples to 22. Predict the first two output lines, then check your prediction. They should be 4 and 2.
Check your understanding
Exercise
A box holds 6 markers. You have 20 markers. What do 20 / 6 and 20 % 6 produce, and what do the results mean?
Show answer
20 / 6 produces 3, the number of full boxes. 20 % 6 produces 2, the number of markers left over.
Exercise
What do "Total: " + 4 + 2 and "Total: " + (4 + 2) produce?
Show answer
They produce "Total: 42" and "Total: 6". The first joins the numbers to the string one at a time. The second adds them inside the parentheses first.