What is Variables in Java

Types of variables in Java

 i] Primitive type
ii] Non-primitive type or Reference type

Primitive type

           Few predefined keywords are defined in the language which is used to store the data.


Following are the primitive data types used and the memory allocated to them

Integer data types - Used to hold numeric data

byte(1 byte)
short(2 bytes)
int(4 bytes)
long(8 bytes)

Floating data type- Used to hold decimal valued data

float( 4 bytes)
double(8 bytes)

Text data type- Used to hold character

char(2 bytes)

Logical  (true or false)

boolean(1 byte)    


Reference types or Non Primitive types

           These variables are used to store the addresss of the  object or array.

  Syntax:  
  objectname reference;

  objectname is name of the object
  Reference is the name of the reference variable;

In the object creation example, we discussed about how to create object.

          Sampleclass exp=new Sampleclass();

Here exp is the reference variable which is holding the object.

Previous
Next Post »