✕ Close

Week 15 · 15.5 · Fri, Nov 13 · The Specific Skills + Try Its

Values Together: Types, Traces, Math and Text

This section puts this week's four skills together, the way today's bench does: picking the type a value needs, tracing typed boxes, and reading what / and + do with the types on each side.

Pick the Type

Every value in Java has a data type. An int holds a whole number, a double holds a number that can have a decimal part, a boolean holds true or false, and a String holds text written between double quotation marks. A box is declared with its type, and the compiler checks every value stored in it.

How to pick the type for a box:

  1. Text, such as a name, goes in a String.
  2. A yes or no answer goes in a boolean.
  3. A number that can have a decimal part goes in a double.
  4. A whole number that will never need a decimal part goes in an int.

Example 1. Pick the type for each value: a price of 4.75, a count of 12 students, whether the bill is paid, and a team name.

Show the solution
4.75        double
12          int
paid?       boolean
"Saints"    String

The price has a decimal part, so it needs a double. A count of students is always whole, so an int fits.

Example 2. Read this line before running it.

1int price = 4.75;
Show the solution
error: incompatible types: possible lossy conversion from double to int

4.75 is a double, and an int box would lose the .75, so Java stops the program instead of guessing.

Trace the Boxes

A trace table has one column for each box and one row for each line that runs. Each assignment works out the right side with the values the boxes hold now, then stores the answer in the box on the left. A double box shows its decimal point even when the number is whole.

Example 3. Trace total and avg.

1int total = 9;
2total = total + 6;
3double avg = total / 2.0;
4System.out.println(avg);
Show the solution
line   total   avg    Output
1      9
2      15
3      15      7.5
4      15      7.5    7.5

Line 3 divides by 2.0, a double, so avg keeps the half.

Math and Text Together

The types on each side of an operator decide what it does. When both sides of / are ints, Java keeps only the whole part, and the remainder operator gives what is left over. When one side is a double, Java changes the other side to a double and keeps the decimal part. The + sign adds two numbers, but as soon as one side is a String it joins instead, working from left to right.

Example 4. Two ints, then a double.

1System.out.println(17 / 5);
2System.out.println(17 % 5);
3System.out.println(17 / 5.0);
Show the solution
3
2
3.4

5 goes into 17 three whole times, with 2 left over. With 5.0, the answer keeps its decimal part.

Example 5. Numbers first, then text first.

1System.out.println(3 + 4 + " total");
2System.out.println("total " + 3 + 4);
Show the solution
7 total
total 34

In the first line, 3 + 4 is two numbers, so it adds before any text appears. In the second line, "total " + 3 is already text, so the 4 is joined on.

Sources: Oracle, The Java Tutorials: Primitive Data Types and Strings; Oracle, The Java Language Specification, Java SE 21 Edition, Chapter 5 and §15.17, §15.18 and §15.26.1; course repository, Bench Basics B32 job data (2026), run with the course JDK.

Try It on your own

Work each one in your notebook first, then check it.

Try It 1. A box must hold a temperature of 71.5 degrees. Which type fits?

Try It 2. Which line compiles?

Try It 3. After these lines run, what do the boxes hold? int n = 8; n = n + 4; double half = n / 2.0;

Try It 4. After these lines run, what does s hold? int s = 10; s = s + s; s = s - 3;

Try It 5. What does System.out.println(9 / 2); print?

Try It 6. What does System.out.println("Room " + 2 + 1); print?