Java Arena
Step 1 of 4

Primitive variables

Every Java variable is one of two kinds, and the kind decides what = copies, what a method can change, and what == compares. This module is about the difference, starting with the simpler kind.

The primitive types

Java has eight primitive types. Four of them you've used all along:

Type Holds Examples
int whole numbers, from about -2.1 billion to 2.1 billion 42, -7
double numbers with a decimal point 3.75, -0.5
boolean true or false true, false
char one character, in single quotes 'A', '?'

You'll rarely need the other four: long holds whole numbers too big for an int, float is a less precise decimal number, and byte and short hold whole numbers with small ranges. All eight names start with a small letter.

Every other type is a reference type: String, Scanner, ArrayList, arrays such as int[], and every class you write. Those are the next step.

A primitive variable holds its value

A primitive variable holds the value itself. Picture a box with a number in it. Assigning one variable to another copies the value into the other box, and after that the two boxes have nothing to do with each other:

int planned = 12;
int done = planned;
done = done - 5;
planned++;
System.out.println("Planned: " + planned);
System.out.println("Done: " + done);
Output
Planned: 13
Done: 7
  • int done = planned; copies the value 12 into done. It doesn't tie done to planned in any way.
  • Changing done doesn't touch planned, and changing planned doesn't touch done.

The same goes for a value worked out from other variables. int total = a + b; stores the sum as it is at that moment, and if a changes later, total keeps the old sum.

Swapping two values

Assigning puts a new value in the box, and the old one is gone. That's why swapping the values of two variables takes three steps and a third variable:

int left = 3;
int right = 8;
int temp = left;
left = right;
right = temp;
System.out.println(left + " " + right);
Output
8 3

With only left = right; and right = left;, the first line overwrites the 3, and both variables end up 8. temp keeps a copy of the old value until it's needed.

Your task

Challenge 1 of 3

What does this program print? Keep track of each variable's value on paper, line by line.

Challenge 2

In a game, the player starts with some lives. The program reads the starting number, then one event per line until end: hit takes a life and heart gives one. The first line and the two prints at the end are ready. Declare lives as a copy of start, and change only lives in the loop, so that start still holds the starting number at the end. For the input 3, hit, hit, heart, hit and end, one per line, the program prints:

Lives at the start: 3
Lives now: 1

Challenge 3

The program reads two whole numbers, the ends of a range, and prints every number from the smaller one up to the larger one. The user may type the ends in either order. The loop is ready, and it counts from low up to high: when low is the larger number, swap the values of low and high before the loop, with a third variable as in the example. For the input 6 and 3 the program prints:

3
4
5
6