February 28, 2008 at 7:21 PM
Variables are the temporary memory location where we can store the data for the time being .We can assign the value to the variable once when it is declared. The value of the variable can be changed anywhere in the program if the variable is accessible in that scope. We can declare the variables of any data types.
Single variable declaration in single line
int weight = 100; or
char = 'a'; or
float = 10 .45 f ;
multiple variables declaration in single line
int weight = 100,height = 200,height = 600; // correct
int circle_Height=24 ; radius = 15 ; // incorrect
during the variable declaration we have to follow some rules such as
(1) variable name always begin from alphabet letters, sometimes we can use underscore and ampersand sign before the variable name.
(2) variable name should be meaning full.
(3) we can't use the white space between the variable name ,but can use underscore
(4) variable name should not be a keyword
In java we use the keyword final for making the variables constant . The values of the constant can't be changed once its declared.
final (constant) variables declaration like this :
final int length=145;
The value of the final variable will be fixed until the program execution.
Literal is used for giving the value to a variable of any data type may be string , integer , float etc . Literal is an explicit number or string constant used in Java programs. This specifies the syntax of your declaration of different types of values and operations. . Whenever you want to show the message with java special symbols then you have to use the literals .
Literal declaration like this :
int age = 20 ; // integer literal
float p1 = 3.14f; // float literal
char a = 'A' ; // char literal
String name = "Neha" ; // string literal
Difference between variable , constant & literal is that :
(1) Variables are used for allocating the memory .
(2) Constant(final keyword) is used for giving the fixed value to the variable till the program execution .
(3) Literal is used for giving the value to the variable of any data type .