✕ Close

Week 15 · 15.1 · Mon, Nov 9 · The Specific Skills + Try Its

Boxes and Types: Declaring Typed Variables

This section starts with one look back, at what a whole program prints. Then it shows the five kinds of steps on today's bench: choosing a type for a value, declaring a box and storing a new value in it, deciding what fits, tracing typed boxes and reading a type error.

What a Whole Program Prints

Big idea. Read a program the way the computer runs it: start at main, follow each call into its method and back, and write each line of output as it happens.

A program starts at main, not at the top of the file. A method above main runs only when main calls it, and a method that is never called prints nothing. When main calls a method, that method's lines run, and then main goes on with its next line. A println ends its line, like pressing Enter, and a print does not. A println with nothing in its parentheses prints an empty line.

Worked example. Predict exactly what this program prints, empty lines included.

1public class Main
2{
3    public static void cheer()
4    {
5        System.out.print("Geaux ");
6        System.out.println("Tigers");
7    }
8
9    public static void extra()
10    {
11        System.out.println("Overtime");
12    }
13
14    public static void main(String[] args)
15    {
16        System.out.println("Kickoff");
17        cheer();
18        System.out.println();
19        System.out.println("Final");
20    }
21}
Kickoff
Geaux Tigers

Final

Many readers start at the top and print Geaux Tigers first, or print Overtime. But main runs first, so Kickoff is the first line. The call cheer(); prints Geaux and Tigers on one line, because print does not end the line. Then the empty println prints an empty line, and Final comes last. Nothing calls extra, so Overtime is never printed.

Four Types

A variable is a named box, and its type decides what the box can hold. The table below gives the four types on the bench.

type      what it holds                        example
int       whole numbers                        int age = 15;
double    numbers with a decimal part          double gpa = 3.5;
boolean   only true or false                   boolean ready = true;
String    text inside double quotation marks   String name = "Jada";

How to choose a type:

  1. If the value is true or false, use boolean.
  2. If the value is text in quotation marks, use String.
  3. If the value is a number with a decimal part, use double.
  4. If the value is a whole number, use int.

Example 1. Make four boxes of four types, then print two of them.

1int age = 15;
2double gpa = 3.5;
3boolean ready = true;
4String name = "Jada";
5System.out.println(name);
6System.out.println(age);
Show the solution
Jada
15

The quotation marks are not printed. They only mark where the text starts and stops.

Declaring and Storing

A declaration states the type and the name, and it can store a first value. After that, the box already exists, so a line that stores a new value has no type in front.

Example 2. Declare a box, then store a new value in it.

1int laps = 3;
2laps = laps + 2;
3System.out.println(laps);
Show the solution
5

Line 1 declares laps and stores 3. Line 2 works out laps + 2, which is 5, and stores 5 in the same box.

Example 3. Declare the same name twice.

Show the solution
int laps = 3;
int laps = 5;     does not compile: laps is already declared

The second line tries to make a second box named laps. To change the value, the line should be laps = 5; with no type in front.

What Fits

A box holds only the values its type allows. A whole number fits in a double box, and the double shows it with a decimal point. A number with a decimal part does not fit in an int box, because the part after the point would be lost.

Example 4. Store a whole number in a double box.

1double price = 2;
2System.out.println(price);
Show the solution
2.0

Example 5. Text and numbers are different types, even when they look alike.

1String s = "12";
2int n = 12;
3System.out.println(s + 1);
4System.out.println(n + 1);
Show the solution
121
13

s holds the text 12, so + joins the 1 on. n holds the number 12, so + adds.

Example 6. A boolean box can store the answer to a question.

1int temp = 85;
2boolean hot = temp > 80;
3System.out.println(hot);
Show the solution
true

Tracing Typed Boxes

A trace table for Java has one column for each box and one for the Output. Each row is the boxes right after that line runs. A double always shows its decimal point.

Example 7. Trace two boxes of two types.

1int score = 10;
2double avg = 8.5;
3score = score + 2;
4avg = avg + score;
5System.out.println(avg);
Show the solution
line   score   avg    Output
1      10
2      10      8.5
3      12      8.5
4      12      20.5
5      12      20.5   20.5

Line 4 uses the new value of score, 12, so avg becomes 8.5 + 12, which is 20.5.

Reading a Type Error

When a value does not fit its box, the compiler reports an error, and nothing runs. The message names the two types that do not match.

Example 8. Two lines that do not compile, each on line 3 of its file.

Show the solution
Main.java:3: error: incompatible types: String cannot be converted to int
        int count = "12";
                    ^
Main.java:3: error: incompatible types: int cannot be converted to boolean
        boolean done = 1;
                       ^

The first line puts text in an int box. The fix is int count = 12; with no quotation marks. The second line puts a number in a boolean box. The fix is boolean done = true; or false.

The bench catches three common mistakes, and each one changes the answer. The first is putting a decimal in an int box. The second is reading text in quotation marks as a number. The third is writing the type again when a box only needs a new value.

Sources: Oracle, The Java Tutorials: Primitive Data Types, Variables and Assignment, Arithmetic, and Unary Operators; Oracle, The Java Language Specification, Java SE 21 Edition, Chapter 5; course repository, Bench Basics B15 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. What does this program print?

1public class Main
2{
3    public static void bonus()
4    {
5        System.out.println("Bonus");
6    }
7
8    public static void main(String[] args)
9    {
10        System.out.print("Ready ");
11        System.out.println("Set");
12        System.out.println();
13        System.out.println("Go");
14    }
15}

Try It 2. A box will hold the answer to "Is the store open?" Which type fits?

Try It 3. Which value needs a double box, because no other type of the four can hold it?

Try It 4. x was declared with int x = 8;. Which line stores a new value in the same box?

Try It 5. What is printed?

1int x = 8;
2x = x * 2;
3System.out.println(x);

Try It 6. What is printed?

1int n = 4;
2double d = n;
3System.out.println(d);

Try It 7. What is printed?

1String word = "5";
2System.out.println(word + word);

Try It 8. Trace the boxes. What does w hold after line 2?

1double w = 2.5;
2w = w * 2;

Try It 9. In a Java trace table, what goes in the Output column of a row?

Try It 10. A line reports "incompatible types: String cannot be converted to int". What is the mistake?

Try It 11. Which line compiles?