Variables & Data types in Java

Variables in Java

There are three variable's available in Java

  1. Local Variable
  2. Instance Variable
  3. Static Variable



types of variable 
Local Variable:

           The Local Variable assigned inside of the method and outside of the Class. The scope of the Local Variables if with in the Method only.

Example:

class Taxes
{
 void method_name()
  { 
    int count; //Local Variable
   /*...*/
  }
} 

Instance Variable:

            Instance Variable available inside the Class and out side of the method. Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.

Example:

class Taxes
{
  int count; //Instance Variable
  /*...*/
}

Static Variable: 

            The Static Variable also called as Class Variable. The Static/Class Variable declared inside the class and outside of the method.

Example:

class Taxes
{
  static int count; //Static/Class Variable
  /*...*/
}

The Interview Point of view:

What is difference between the Static/Class Variable and the Instance Variable ??
__________________________________________________________________________________


Static/Class Variable

Instance Variable

Instance variables and class variables are both member variables. They are both member variables because they are both associated with a specific class.

Every object has it’s own copy of the instance variables

Each object of the class does not have its own copy of a class variable.
 

     Now, it should be clear what the difference between instance and class variables is. Class variables only have one copy that is shared by all the different objects of a class, whereas every object has it’s own personal copy of an instance variable. So, instance variables across different objects can have different values whereas class variables across different objects can have only one value.
________________________________________________________________________________
Data types in Java

datatype in java
Data Type
Default Value
Default size
boolean
false
1 bit
char
'\u0000'
2 byte
byte
0
1 byte
short
0
2 byte
int
0
4 byte
long
0L
8 byte
float
0.0f
4 byte
double
0.0d
8 byte

`
























No comments:

Post a Comment