This post will discuss Java variables with examples to help you achieve your learning goals.
Variables are the names of reserved areas of memory that have been allocated for a specific purpose. Essentially, it is a memory location name.
A variable is a combination of “vary” and “able“, meaning its value can be changed.
When it comes to Java variables, they serve as containers for storing data values.
Variables in Java can be divided into different types, such as:
- String – a type of data that stores text, such as “Hello.” Double quotes surround the values of strings.
- int – holds integers without decimals, such as 123 or -123
- float – contains floating point numbers with decimals, such as 19.99 or -19.99
- A char stores characters such as ‘a’ or ‘b.’ The values of chars are surrounded by single quotes.
- boolean – stores values that are either true or false.
type variable = value;
Where type is a Java variable type (such as int or String), and Variable is the names of variables (such as x or any name the user prefers). Variables are assigned values using the equal sign.
Creating Java Variables
We must create Java variables by specifying their type and assigning a value.
Here is an example of how to create a variable that should store text.
Create a variable called variable of type String and assign it the value “Java“:
Here we create another String type variable and Assign name of Java Founder to it:
Consider the following example to create a variable that stores a number.
Make a variable of type int, name it num_variable, and assign it the value 205:
Declare a variable of type int, name it mrx_sum, and assign it the equation 20+10:
Considering Java variables, you can also declare a variable without assigning a value and assign the value later:
Similarly, we declare a String variable named ample first and assign a value to it in the next line:
If you assign a new value to an existing variable, it will overwrite the previous value:
Suppose you don’t want others (or yourself) to overwrite existing values. In that case, you can add the final keyword (this declares the variable as “final” or “constant,” which means unchangeable and read-only):
Consequently, it is impossible to change the value of a String Variable if you have declared the variable with final keyword:
Here is a demonstration of how to declare variables of other types:
In the next chapter, you will learn more about data types.
Java variables are commonly displayed using the println() method.
To combine both text and a variable, use the + character:
A variable can also be added to another variable by using the + character:
Numeric values are represented by the + character (notice that we use int (integer) variables):
As you can see from the example above:
- The Java variable number 1 stores the value 25.
- And variable number 2 stores the value 26.
- Using the println() method, we display the value of variable number1 + number2, which is 51.
Declare Many Variables
Comma-separated lists are used to declare multiple variables of the same type:
We can also add as many String values as we can by using the + operator:
There must be a unique name for every Java variable.
A unique name is called an identifier.
Names can be short (such as x and y) or descriptive (such as age, sum, or total volume).
Important: To create maintainable and understandable code, you should use descriptive names:
Names for Java variables (unique identifiers) are constructed according to the following rules:
- In naming a variable, you can use letters, digits, underscores, and dollar signs.
- There must be a letter at the beginning of a name.
- The first letter of a name should be lowercase, and it cannot contain any whitespace.
- Names can also begin with $ and _(but we will not use them in this tutorial).
- Variable names are case sensitive (“myVar” and “myvar” are different variables).
- There are certain words that cannot be used as names (like Java keywords, for example int or boolean).