Tuesday 9 February 2016

Types of variables in JAVA

Types of variables in JAVA:
======================
There are 2 types of variables 
1.Primitive variables
2.Reference variables

Primitive variables:
--------------------------
Primitive variables are used to represent primitive values like 

integer etc...
ex:int a=10;


Primitive variables are divided in to 3 types.
      1.Instance variables
      2.Static variables
      3.Local variables

1.Instance variables:
---------------------------
If the value of a variable keep on changes or its varies from 

object to object then that type of variables are called Instance 

variables.

syntax:
----------
  datatype variablename = variablevalue;
----------------------------------------------------

Instance variables should be declared within the class directly.

For every object a separate copy of instance will be created.

Instance variables will be stored in the heap memory.

Instance variables will be created at the time of object 

creation and destroyed at the time object destruction.

Instance variables are also called as object level variables.


Default values:
--------------------
If we dont perform initialization For instance variables JVM 

provide values.
(integer====0)
(float====0.0)
(String ===null) etc...
ex:
package blog;

public class DefaultDemo {

int a,b;
String name;

public static void main(String[] args) {

DefaultDemo defaultDemo = new DefaultDemo();

System.out.println(defaultDemo.a);//0
System.out.println(defaultDemo.name);//null

}

}

In order to access instance variables compulsory we need to create the object.

static variables:
---------------------
If the value of a variable is not varied from object to object 

then declare such type of variables as static variables.

If we want to convey a variable as static declare that variable with static modifier.

syntax:
--------

 static datatype variable name = variable value;
--------------------------------------------------------------
For static variables a single copy will be created at class 

level.

static variables will be created at the time of class loading 

and will be destroyed at the time of class unloading.


Default values:
--------------------
If we dont perform initialization For instance variables JVM 

provide values.
(integer====0)
(float====0.0)
(String ===null) etc...
ex:
package blog;

public class DefaultDemo {

static int a,b;
static String name;

public static void main(String[] args) {

DefaultDemo defaultDemo = new DefaultDemo();

System.out.println(defaultDemo.a);//0
System.out.println(defaultDemo.name);//null

}

}


We can access static variables in 3 ways
1.By using classname
2.Direclty by calling their names
3.By using object

Local variables:
---------------------
Based on requirements some times we need to use some variables at certain extent level only.
When ever we declare a variable inside the method or constructor such type of variables we can call it as local variables

Local variables will be stored in stack memory.
For local variables compulsory we need to perform initialization

The scope of local variable is within that particular method only and when you try to access outside of that we will get compile time error.

package blog;

public class DefaultDemo {

public static void main(String[] args) {

int a;
System.out.println(a);//compile time error...a is local variable should perform      initialisation

}

}


Reference variables:
---------------------------
Reference variables are used to represent or to refer objects.

Ex:Employee emp = new Employee();

No comments:

Post a Comment