Chapter 3: Variables

In the last chapter we have learned to repeat certain events while a condition is met. Now we want to do the following:

Kara is to lay a trail of five leaves.

Loop Example

This would, of course, be quite easy if we simple called putLeaf() and move() five times. But this is not very elegant. It would be nice if Kara counts how many leaves he has already placed. If so, Kara will need a “brain”, i.e. some kind of memory. Memory in programming can be used through the use of variables.

Counting with Kara

int i;
i = 0;

while (i < 5) {

	i = i + 1;


  1. With int i; we reserve space for a variable named i of the type int. We say: The variable i is declared. In Java, there are different types of variables that can be used (see tables below).
  2. With i = 0; the variable i is assigned the value 0. Since it is the first assignment for the variable, we also say: The variable i is initialized.
  3. We can declare a variable and initialize it in one step: int i = 0;
  4. For the condition i < 5, the comparison operator < means less than (more comparison operators, see tables below).
  5. For the assignment i = i + 1, we must first look at the right part. It means: “Take the current value of i, add 1 to it and save the new value again under the name i.”

More Information about Variables

  • It is possible to provide a variable with a final value, i.e. to make it a constant: final int NUMBER = 5;
    Then we could rewrite the above example as while (i < NUMBER)
    Constants are written entirely in uppercase letters.
  • Variables that are not constants always start with lower case.

Elementary Data Types in Java

These data types are called “elementary” because they are the basic data types in Java. (Later we will learn how to create variable types for entire objects).

Integers and Characters

Type From Up to and including Required memory
byte -128 127 8 bit
short -32'768 32'767 16 bit
int -2'147'483'648 2'147'483'647 32 bit
long -9'223'372'036'854'775'808 9'223'372'036'854'775'807 64 bit
char 0 65'635 16 bit

Floating Point Numbers

Type From Up to and including Required memory
float -3.4 \* 1038 3.4 \* 1038 32 bit
double -1.7 \* 10308 1.7 \* 10308 64 bit

Logical Values

Type Range of values Required memory
boolean true or false 1 bit

Comparison Operators

The following operators can be used for comparisons in Java. The result is always a boolean (either true or false).

Operator Meaning Example
== equal k == 2
!= not equal k < 12
> greater than k > 67
< less than k < 12
>= greater than or equal to k >= 45
<= less than or equal to k <= 23

Note: The equality operator always has two equal signs ==. A single equal sign = is used for assignments!

Arithmetic Operations

To calculate we use the following arithmetic operators:

Operator Meaning Example
+ Add h = value + 34
- Subtract y = 3.4 – t
* Multiply z = h * 3.56
/ Divide d = m / v
% Get the remainder of an integer division (modulo) count = w % 2

TASK 3.01: Counting Leaves

TASK 3.01

Kara is going horizontally from left to right until he reaches the tree. On his way he counts the leaves and reports the result at the end.


  • At the end, you can write the result with the following command to the console: System.out.println("The result is: " + count);
  • Text must always be written in quotes. The plus sign appends the value of the variable count (which may of course be named differently).
  • Scope of variables: variables are visible only within the block (between the curly brackets), in which they are declared. They can also be declared outside the methods in order to be visible to the entire class.

TASK 3.02: Kara in a Box I

TASK 3.02

A square area is surrounded by trees. Within the area is a set pattern of leaves that Kara must invert. Kara starts in the upper left corner with a view to the right.


  • For this task it is helpful to work with boolean variables, e.g.:
    • declaration and initialization: boolean goingRight = false;
    • switching from false to true and vice versa: goingRight = !goingRight;
    • boolean as a condition: if (goingRight)

TASK 3.03: Kara in a Box II

TASK 3.03

A square area is surrounded by trees. Within the area is a checkerboard-like pattern of leaves to be laid by Kara. Kara starts in the upper left corner with a view to the right.

TASK 3.04 (difficult): The Longest Tree Line

TASK 3.04

In this world there are several rows of trees. Kara is to determine the length (in number of trees) of the longest line of trees and output the result to the console. Between the rows of trees is always at least one empty row. A leaf marks the end of the world.

TASK 3.05 (very difficult): Push Mushroom Through Tunnel

TASK 3.05

This world of Kara has two boxes connected by a tunnel. In the box on the left is Kara and a leaf. In the box on the right is a mushroom. Kara is to get to the other side, find the mushroom, then push it to the left side. Once on the left side, the mushroom must be pushed onto the leaf.

Kara always starts in the top left corner and the leaf is always in the bottom left corner. The mushroom, on the other hand, can be at an arbitrary position on the right side of the tunnel.


  • This problem may be solved in pairs and in collaboration.
  • Some sub-problems may be divided among the collaborators:
    • Find the tunnel entrance
    • Find the mushroom
    • Place the mushroom in the tunnel entrance
    • Push the mushroom onto the leaf

More about Variables

So far, we have had a quick introduction to variables. Now, a few additional comments on the different types:

Elementary Data Types

Elementary Types

Elementary data types are like cups (in memory) where the value is saved directly into the variable.

Reference Types

Reference Types

The value in k is a reference to the Kara-object. With the dot operator (k.) k can be used like a remote control for the Kara object!

Credits: Ideas and concepts of Kara were developed by Jürg Nievergelt, Werner Hartmann, Raimond Reichert et al. Some Kara exercises are based on material by Horst Gierhardt.

What’s Next?

Continue with Chapter 4: Sokoban Game