Java - Variables

Variables are a fundamental concept in programming. In Java, a variable is a named storage location in the computer's memory that is used to hold a value. In this tutorial, we will cover the basics of variables in Java, including their use, how to declare them, and the rules for naming variables.

What is a Variable?

A variable is a named storage location in the computer's memory that is used to hold a value. A value can be of any data type, such as a number, a character, or a boolean value. In Java, we must declare a variable before we can use it. This means that we must specify the name and data type of the variable.

Using a Variable:

Variables are used in Java to store values that can be used later in the program. For example, we might want to store a user's name, age, or address in a variable. Once we have stored this information in a variable, we can use it in different parts of the program.

Declaring a Variable:

To declare a variable in Java, we use the following syntax:

data_type variable_name;

For example, to declare an integer variable named "age", we would use the following code:

int age;

This code declares an integer variable named "age". The data type "int" specifies that the variable can hold an integer value.

Assigning a Value to a Variable:

To assign a value to a variable, we use the following syntax:

variable_name = value;

For example, to assign a value of 25 to the "age" variable, we would use the following code:

age = 25;

Alternatively, we can declare and assign a value to a variable in a single line of code, like this:

int age = 25;

This code declares an integer variable named "age" and assigns it a value of 25.

Rules for Naming Variables:

When naming variables in Java, there are some rules that we must follow. These rules include:

  • Variable names must begin with a letter or underscore.
  • Variable names can include letters, digits, and underscores.
  • Variable names are case-sensitive.
  • Variable names should be descriptive and meaningful.
  • Variable names should not be a keyword or reserved word in Java.

For example, the following are valid variable names:

myAge
_my_age
age123

However, the following are invalid variable names:

123age
my-age
public

In Java, a final variable is a variable whose value cannot be changed once it is assigned. Once a final variable is assigned a value, it becomes a constant and cannot be reassigned.

Declaring a variable as final is useful when you want to define a constant value that should not be changed throughout the program. For example, if you want to define the value of Pi, you can declare it as a final variable as follows:

final double PI = 3.14159;

Once this variable is assigned, its value cannot be changed anywhere in the program. If you try to reassign a value to a final variable, the compiler will generate an error.

Rules for using final variables:

  • A final variable must be initialized when it is declared, and once initialized, it cannot be reinitialized.
  • The naming convention for final variables is to use all capital letters.
  • final variables can be of any primitive data type or object type.
  • A final variable declared in a method must be initialized before it is used.
  • final variables can be used to declare constants and can be useful in defining global constants in a program.