A variable is a location in memory (storage area) to hold data. To indicate the storage area, each variable should be given a unique name (identifier). How to declare variables in Java? Here's an example to declare a variable in Java.
Here,
is a variable of
data type and is assigned value 80. Meaning, the
variable can store integer values. You will learn about Java data types in detail later in the article.
In the example, we have assigned value to the variable during declaration. However, it's not mandatory. You can declare variables without assigning the value, and later you can store the value as you wish. For example,
The value of a variable can be changed in the program, hence the name variable. For example,
Java is a statically-typed language. It means that all variables must be declared before they can be used. Also, you cannot change the data type of a variable in Java within the same scope. What is the variable scope? Don't worry about it for now. For now, just remember you cannot do something like this.