Aug 26, 2011

Variables and Datatypes

What is a variable?
          A variable is a container that stores a meaningful value that can be used throughout a program. For example, in a program that calculates tax on items you can have a few variables - one variable that stores the regular price of an item and another variable that stores the total price of an item after the tax is calculated on it.Variables store this information in a computer's memory and the value of a variable can change all throughout a program.

Declaring a variables
           One variable in your program can store numeric data while another variable can store text data. Java has special keywords to signify what type of data each variable stores. Use these keywords when declaring your variables to set the data type of the variable.

Java Primitive Datatypes

Data Type
Default Value
true or false
Unicode Character
Signed Integer
(byte) 0
Signed Integer
(short) 0
Signed Integer
Signed Integer
Real number
Real number

         char aCharacter;
         int aNumber;
You can assign a value to a variable at the same time that it is declared. This process is known as initialization:
char aCharacter = 'a';
int aNumber = 10;
Declaring a variable and then giving it a value:
char aCharacter;
aCharacter = 'a';
int aNumber;
aNumber = 10;

Naming Variables
       Rules that must be followed when naming variables 
  •  No spaces in variable names
  • No special symbols in variable names such as !@#%^&*
  • Variable names can only contain letters, numbers, and the underscore ( _ ) symbol
  • Variable names cannot start with numbers, only letters or the underscore( _ ) symbol (but variable names can contain numbers)
Recommended practices (make working with variables easier and help clear up ambiguity in code):
  • Make sure that the variable name is descriptive of what it stores - For example, if you have a variable which stores a value specifying the amount of chairs in a room, name it "numChairs".
  • Make sure the variable name is of appropriate length - Should be long enough to be descriptive, but not too long.
Also keep in mind:
  • Distinguish between uppercase and lowercase - Java is a case sensitive language which means that the variables varOne, VarOne, and VARONE are three separate variables!
  • When referring to existing variables, be careful about spelling - If you try to reference an existing variable and make a spelling mistake, an error will be generated.
Types of Variables
       The Java contains the following types of variables:

Instance Variables (Non-static fields):
         In object oriented programming, objects store their individual states in the "non-static fields" that is declared without the static keyword. Each object of the class has its own set of values for these non-static variables so we can say that these are related to objects (instances of the class).Hence these variables are also known as instance variables. These variables take default values if not initialized.

Class Variables (Static fields):
         These are collectively related to a class and none of the object can claim them  its sole-proprietor . The variables defined with static keyword are shared by all objects. Here Objects do not store an individual value but they are forced to share it among themselves. These variables are declared as "static fields" using the static keyword. Always the same set of values is shared among different objects of the same class. So these variables are like global variables which are same for all objects of the class. These variables take default values if not initialized.

Local Variables:
         The variables defined in a method or block of code is called local variables. These variables can be accessed within a method or block of code only. These variables don't take default values if not initialized. These values are required to be initialized before using them.

          Parameters or arguments are variables used in method declarations.


Post a Comment