Thursday, April 10, 2014

BASIC JAVA LANGUAGE FUNDAMENTALS - Variables

Variables
  • A variable has a name, type , and a value associated with it and a variable can stores value of particular type associated with it.
  • In java variables can store values of primitive type as well as reference values of object.
  • In java, depending upon position of declaration, variables are categorized as  instance variables and local variables. With these the variables which stores reference values of objects are called reference variables. 
  • Apart from reference variable ,there is concept of class variable which is prefixed with keyword static.
  • The declaration of variables implicitly determines about memory allocation and values that can be stored in them.
Example :  int first ; 
here it represents declaration of variables which is not initialized explicitly. here in this example  first  is the name of the variable and int represents the type of variable.

Instance Variables: 
  • When we create a class in java, we write fields within class, those fields are termed as instance variables.
  • Each object created from class will have its own copies of the fields defined in the class.
  • The values of these instance variables, represent the state of an object.
  • Two different object can have same state , if their instance variables have same values.
To know the practical demo, follow my post Here
static  variables
  • static variables are those variables which are prefixed with static keyword
  • static variables belongs to class , not to any object of class.
  • static variables is initialized when the class is loaded at run-time.

Reference variables
  • Reference variables store reference value of an object and can be used to manipulate the object denoted by reference value.
  • When we declare reference variable then there is reference type associated with the reference variable and that reference type can specify to name of class,interface  and an array.
  • Before we can use a reference variable to manipulate an object, it must be declared and initialized with the reference value of the object.
  • A reference variable can be of only one type, and once declared, that type can never be changed (although the object it references can change)
  • A reference is a variable, so it can be reassigned to other objects, only exception is use of final with reference variable( i will explain use of final in separate post)
  • A reference variable's type determines the methods that can be invoked on
    the object the variable is referencing.
  • If the variable is declared as an interface type, it can reference any object of any class that implements the interface. ( i will explain interface separately in different post)
  • A reference variable can refer to any object of the same type as the declared reference, it can refer to any subtype of the  declared type ( i will refer it again when i will explain inheritance and polymorphism in separate post).
Note :
In case of instance variable,static variable and in Array, if we don't provide any explicit initialization ,then those instance variable are awarded with default initial values depending upon their type.
 

The implicitly assigned default values are as follows:
boolean ---> false
byte ----> 0
short ----> 0
int -----> 0
long ------> 0l
char -----> /u0000
float ------> 0.0f
double ----> 0.0d
any object reference ----> null
Local variables 
  • local variables are variables which are declared in methods, constructors and blocks.
  • local variables must be explicitly initialized before we can use . if we will try to use uninitialized local variables then compiler will issue an error.
To know the practical demo , follow my post HERE

No comments:

Post a Comment