Variables in Java

We use variables in Java to save data, and they must be declared with the data type and name before we can use them. The variable can only contain values that are compatible with its data type.

What is a variable in Java?

In programs, we store data in variables, and each variable corresponds to a memory location in the computer. A variable is a quantity that you introduces yourself, you give a name to it, and you can also assign values to it. Because we can use variables over and over again, they become beneficial. Variables are a part of the foundation of programming.


Variable in Java

  • Is a memory location in the computer where you store data, for example, the value of a calculation
  • Has a name and a data type
  • Can be used later in the program for other applications


Because we can use variables over and over again, they become beneficial. Variables are a part of the foundation of programming.


Furthermore, to make it easier to visualize what a variable is, consider the variable as a box that we have given a name. In this box, we can then save different values that we will use.

Variables in Java

Declare a variable in Java – Syntax

We will now see how we can declare some variables with different data types. Additionally, how we assign the variables an initial value. If you do not remember what a data type is, you can read more about it in the previous page.  Moreover, note the differences in the declarations between different data types. We will later in chapter 7 (classes and objects) look at different visibility classes for variables. But now let’s focus on how we can declare different variables.


To create a variable in Java, write:


Data type name = value;


To create a variable in Java, you must

  • First, specify which data type you want to store
  • Then you give the variable a variable name
  • Finally, you assign (=) a value
  • Don’t forget to end with a semicolon

Variables in Java – Example

Below are a few examples of how to create different variables of a particular data type.


int varOne = 10; 
double varTwo = 15.0; 
boolean varLogical = false;


In this example, we have created three different variables:

  • First, integer (int) named varOne, that is assigned the value 10 (which is an integer)
  • Secondly, decimal number (double) called varTwo, which is assigned the value 15.0 (which is a decimal number). Note that we need to write 15.0 to declare a floating point number of type double, that is, 15 is not enough, but we have to enter 15.0.
  • Finally, a boolean variable (true or false) named varLogical that is assigned the value false.


If we illustrate it with the previously mentioned boxes

Example of Variables in Java

Declaring variables of characters and texts have the same basic principle but note the apostrophes (‘) for characters and quotes (“) for texts. This is needed around the value of the variable to declare it, but IntelliJ is helpful in giving characters and texts a green color if they are properly declared.


char varCharacter = 'e'; 

String varWord = "Hello";

Declaring Variables in Java without initial values

The last thing we will take a look at before moving on from variables is that it is also possible to declare variables without an initial value. The computer will then give the variable a random value. A variable without an initial value declared quickly by writing.


Data type name;

int varOne;

double varTwo;