Java Variables

Summary: in this tutorial, you’ll learn about how to use Java variables to store and manage data in programs.

Introduction to Java variables

In Java, variables are used to store and manage data. Java is strongly typed, therefore, you need to specify the type of variables when you define them.

To define a variable, you specify its data type e.g., integer, followed by a name. For example, the following defines a variable named age:

int age;Code language: Java (java)

In this example, int is the integer type and age is the name of the variable. Later you’ll learn more about other types in Java.

After defining a variable, you can assign a value to it. For example:

int age;
age = 22;Code language: Java (java)

In this example, we assign 22 to the age variable. If you print out the variable age using the System.out.println() method, you’ll see the number 22 in the output:

int age;
age = 22;
System.out.println(age); // 22Code language: Java (java)

Here’s a complete program:

public class App {
    public static void main(String[] args) {
        int age;
        age = 22;
        System.out.println(age);
    }
}Code language: Java (java)

Java allows you to define a variable and initialize its value at the same time like this:

int age = 22;Code language: Java (java)

That one line of code is equivalent to two lines of code and is more concise:

int age;
age = 22;Code language: Java (java)

Java is statically typed. It means that once you define a variable, you cannot assign a value of a different type to it.

For example, the following attempts to assign a string to an integer variable, which results in an error:

int age;
age = "22"; // errorCode language: Java (java)

Naming variables in Java

By convention, you should use only letters and numbers to name variables. Note that you can use some special symbols for naming variables technically but it is not widely used in practice.

Keep in mind that the variable names cannot start with numbers like:

int 2much = 100; // invalidCode language: Java (java)

In Java, variable names use camel case style. It means that you start each word after the first with upper case and all other letters are lower case. For example:

int age = 22;
int myAge = 22;
int myMomAge = 55;Code language: Java (java)

Assign a variable to another variable

It’s possible to assign a variable to another. For example:

int price = 100;
int cost = 80;
price = cost;Code language: Java (java)

In this example, we have two variables with type int and values 100 and 80 respectively. In the third line, we assign the value of the variable cost to the variable price.

Final variables in Java

Final variables are variables whose values cannot be changed once set. To define a final variable, you use the final modifier before the data type. For example:

final int maxAge = 150;Code language: Java (java)

In this example, we define the maxAge as a final variable. Since we initialize its value immediately, later we cannot modify its value like this:

final int maxAge = 150;
maxAge = 200; // errorCode language: Java (java)

It’s possible to define a final variable without initialization and set its value later as follows:

final int maxAge;

// other code
// ...

// set the final variable
maxAge = 200;Code language: Java (java)

The final variables are useful for creating constants and ensuring immutability. As you progress to more complex Java applications, you’ll find them very valuable for writing robust and more maintainable code.

“variable might not have been initialized” error

Java doesn’t allow you to use uninitialized variables. If you declare a variable but don’t initialize its value or assign a value to it, and you attempt to access its value, you’ll get the error: “variable might not have been initialized”. For example:

public class App {
    public static void main(String[] args) {
        int age;
        System.out.println(age);
    }
}Code language: Java (java)

If you build the program, you’ll get the error. To fix it, you can initialize the variable a value. For example:

public class App {
    public static void main(String[] args) {
        int age = 1;
        System.out.println(age);
    }
}Code language: Java (java)

Summary

  • Use variables to store and manage data in a program.
  • To define a variable, specify a data type, followed by a name and an optional initial value.
  • Use only letters and numbers to name variables.
  • Use camel case for naming variables.
  • Final variables cannot changed once set.