Java Tutorials

100+ tutorials, explanation videos, and examples to help you develop creative and technical skills.

Variables in Java

A variable is a storage location in the main memory paired with an associative name. In this storage location, we store a data value. The variable name is a way to access that value from the main memory. As in real life if we want to go to someone’s house then we must know the house address. In the same way, computer memory has lots of addresses and at every address, different data values are stored. So to find which data value we want to use from memory we use variable names (Memory Addresses).

In computer programming, Variables may not directly correspond to the concept of variables in mathematics.

How to declare variable in Java?

Variable name must start with a Data Type. As we know a variable is referenced to a data value stored in memory. To define what kind of data value (like alphabet, digit etc.) the variable contains in the main memory we use data types.

To access a variable’s value later in our program we can simple use variable’s name. We can also change its value later in our program.

We have a complete chapter on data types which we will discuss further.

Variable naming rules

While giving a name to a variable you must follow some rules as defined below.

1. All identifiers(variable names) should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).

2. A keyword cannot be used as an identifier (switch, class, else, for, while etc).

3. White space not allowed.

4. Identifiers are case sensitive (rollNumber and RollNumber are different).

5. Example of correct identifier = rollNumber, salary, $age, _bonus.

5. Example of incorrect identifier = 5number, -money, this (‘this’ is a keyword)

If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com

Follow us to learn Coding and get in touch with new Technologies.