Go to Top

Sunday 20 March 2011

Constants and Variables

                             Constants are the ones whose values are constant and never change. For example 1, 124, 9863, 123857, 10000000, etc. are all constants. Their values remain the same. But the variables do not have a constant value. They are just like the holders of a particular constant. The constant's value is assigned to the variable. They can be a letter a combination of letters (or words, though reserved keyword variables (such as null, void etc.) are regarded invalid by Java), an alpha-numeric representation i.e. containing alphabets as well as numbers (though numbers cannot be the first letter of the variable, cannot contain any special character or blank spaces are invalid variables). Examples of valid variables are a, maximum, abc123 etc. Examples of invalid variables are 12abc, abc xyz, null, a.b, etc. They can be assigned any value within their datatype. For example the constant 5 can be assigned to the variable a. This is shown in the following piece of code :

a=5;

                              Note : The variables need to be defined by using a datatype such as int, float, double, etc.before they are assigned some value. For example :

int a;
or,
double a;
                               

No comments:

Post a Comment

ShareThis