Skip to content

Variables and Data Types

In Java, as in other programming languages, variables are a fundamental concept. Variables are used to store information which can be used elsewhere in your code. In this lesson, we'll cover what variables are, what data types are, how to declare a variable, and how to use a variable.

Variables

Think of a variable as a name for a location in memory. This location can store a data value, and the variable name can be used to access and manipulate that value. For example, if you have a variable named speed, you can use that variable to both store and retrieve the speed of your robot.

Data Types

Each variable in Java has a specific data type, which determines the kind of values the variable can hold. The common ones we will use are:

  • int - An integer (whole number), such as 5 or -100
  • double - A decimal number, such as 3.14 or -0.01
  • boolean - A truth value, either true or false
  • char - A single character, such as 'A' or 'z'
  • String - stores text, such as "Hello", String values are surrondewd by double quotes.

Declaring Variables

Before you can use a variable, you must declare it. Declaring a variable tells the Java compiler what the variable's name is and what type of data it will hold. Here is the syntax for declaring a variable in Java:

<dataType> <variableName>;

For example, to declare an integer variable named speed, you would write:

int speed;

Assigning Values to Variables

After you've declared a variable, you can assign a value to it using the = operator. Here's how you would assign the value 10 to the speed variable:

speed = 10;

You can also declare a variable and assign a value to it at the same time, like this:

int speed = 10;

Using Variables

Once a value has been assigned to a variable, you can use that variable anywhere you could use the value itself. For example, you could use the speed variable to calculate the time it takes for the robot to travel a certain distance:

int speed = 2; // speed of the robot in meter per second (m/s)
int distance = 10; // distance the robot needs to travel in metres

Here, speed and distance are variables holding the values 2 and 10, respectively. We can use these variables to calculate the time it would take for the robot to travel that distance.

double time = distance / speed;

This line of code calculates the time by dividing the distance by the speed (10 / 2), so time will be 5.0. This tells us it would take 5 seconds for the robot to travel 10 metres at a speed of 2 m/s.

Tasks

For this week's tasks, we'll be practicing working with variables and data types.

Before we start, let's setup our environment:

  1. Open Visual Studio Code (VS Code).
  2. Click on File -> New Java Class to create a new file.
  3. Save this file as Week2Task1.java on your Desktop or a folder where you prefer to keep your tasks.

Now that you've set up the file, let's get started with the tasks.

  1. Declare an integer variable named distance and assign it a value of 100.
  2. Declare a double variable named time and assign it a value of 10.0.
  3. Declare a double variable named speed but don't assign it a value yet.
  4. Calculate the speed by dividing distance by time and assign the result to speed.
  5. Print the value of speed to the terminal.

Answers

Additional Resources

W3 Schools - Data Types

W3 Schools - Variables

W3 Schools - Strings

W3 Schools - Booleans

Oracle Java Documentation - Primitive Data Types:

Oracle Java Documentation - Variables: