The Variable is nothing but a name given to the memory location.it specifies what type of data it stores. It is used to allocate the space in memory. Variables is a quantity which can be changed during the execution of a program. Naming the variable has some rules. They are as follow:
1. The first character in a variable name must be an alphabet, underscore(_) or dollar sign($)
2.Commas or blank spaces not allowed within a variable name.
3. They can be of any number of character.
4. Special characters are not allowed in the variable name.
5.Java Keywords cannot be used as a name.
6. Variable names are case sensitive.
Variable Declaration:
Java variable declaration consist of two things:1.type of data it stores
2.variable name
The general format for the variable declaration is as follow:
Data_type variable name;
Example:
int a;
float b;
In java it is possible to declare more than one variable in a single declaration.So,the general format to declare multiple variable is,
Data_type var1,var2,var3.........varn;
Where,
datatype-any valid data type like int,char,float etc.
var1,var2,var3......varn-name of the variable.
Example:
int a;
char name;
float p;
Initialization of variable:
Initializing is nothing but assigning a value to the variable.The general format to assign the variable is as follow:data_type variable name = value or expression;
Example
int a=10;
char c='m';
string str="man";
class variable
{
public static void main(String args[])
{
int a=5;
float b=10.5;
char c='x';
string str="man"
system.out.println("The int value is:"+a);
system.out.println("The float value is:"+b);
system.out.println("The char value is:"+c);
system.out.println("The string value is:"+str);
}
}
The output of the following program is shown here:
The int value is:5
The float value is:10.5
The char value is:x
The string value is: "man"