Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory.
Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.
There are two data types available in Java -
Primitive Data Types
Reference/Object Data Types
There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword.
You must declare all variables before they can be used. Following is the basic form of a variable declaration -
data type variable [ = value][, variable [ = value] ...] ;
the data type is one of Java's data types and a variable is the name of the variable. To declare more than one variable of the specified type, you can use a comma-separated list.
Following are valid examples of variable declaration and initialization in Java -
int a, b, c; // Declares three ints, a, b, and c. int a = 10, b = 10; // Example of initialization byte B = 22; // initializes a byte type variable B. double pi = 3.14159; // declares and assigns a value of PI. char a = 'a'; // the char variable a is initialized with value 'a'