A variable is a named space in the computer memory (RAM) that can store data.
In Java we can create a variable like below :
int number = 5 ;
In Java its different from some other languages like Python where you don’t need to give the data type. Here “int” is the data type where it refers to “Integer”, “number” is the name of the variable and “5” in the value stored in that memory location.
There are some rules in naming a variable. Let’s take a look at them now.
- It cannot be started by a number.
- number1 – (correct form)
- 1number – (incorrect form)
- spaces are not allowed – you can use underscore for that purpose
- number_1 – (correct form)
- number 1 – (incorrect form)
- To start a variable name, a letter, $ sign or an underscore (_) can be used.
- Variable names are case sensitive.
- “number” and “Number” are two different variable names.
These rules are applied in most of the programming languages but not all of them.