Thursday, January 8, 2009

What is a variable in Java

Welcome back to Java Code Online. Today I will be discussing about what is a variable in Java. In Java, whatever you do, you need to do that with the help of variables.

A variable as the name signifies is something that can vary. I do not mean that it will always vary, there are modifiers in Java like "final" which if used along with the variable, makes it impossible to vary. A variable is like a container, which has the capability to hold something.

You remember that when you are feeling sleepy, then your mom gives you tea. The tea is in a cup or glass. This cup or glass is a container, that holds the tea. Similarly in Java, the variable which is like a container holds some value or some reference parameter.

A variable in Java could be of two types:-
1. primitive variable
2. object reference variable

Primitives variables are used to hold basic values needed for normal computation bu Java, like some number, or an integer or a floating point number or it could be a boolean also. Object Reference variables are used to reference to some Object.

A variable has to follow some rules, in order to qualify as variable in Java. The rules are simple and must always be implemented. The rules are:-
1. A variable must have a type.
2. A variable must have a name.

I will clarify on this point with an example. If you say:-

float marks;

Then in the above Java code, "float" is the type of variable and "marks" is the name of the variable. Simple isn't it.

I hope the article helped you in understanding what is a variable in Java. If you liked the article then do leave a comment. Foe more info on Java, keep buzzing Java Code Online.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.