Variable Data Type

 

A variable is a named memory location. In Java there are three types of variables namely: local, instance and static variables. And we have two types of data types in Java that are: primitive data types and non primitive data types.

 

Variable:

A variable is a named memory location that has reserved some space or area in the memory. The value of the variable can be changed as its name says vary and able which means we can change the value of the variables.

variables

The variables are created in RAM. RAM is a temporary memory. That is why the data stored is also temporary. It can only be used and processed during the execution of program. The data stored in the variable is automatically removed when program ends.

Consider the following line of code in which we have created a variable of integer data type:

CODE:

int a = 10;

In the above line ‘a’ is the variable.

 

Types of Variables in Java:

In Java there are three types of variables:

  1. Local variables
  2. Instance variable
  3. Static variable

Local variable:

A local variable is that variable that is declared inside a function or method. The local variable is not known outside the function.

Instance variable:

The instance variable is that which is declared outside the function or method but inside the class. The instance variable is not declared as static.

Static variable:

The variable which is declared as static is known as static variable. A static variable can never be a local variable.

Consider the following example in which the types of variables are defined in Java:

CODE:

class Z {

int a = 20;

static int b = 30;

void funct () {

int c = 40;

}

}

In the above lines of code three variables are declared of different types. First of all a class is defined named ‘Z’, then a variable of integer type is declared and is assigned a value. This variable is the instance variable as it is declared inside the class but nor inside any method or any function. Then we declared a static type variable using the keyword “static”. The static variable is not the local variable.

In the next line of the code a function is defined named funct (). In this function we have declared a variable of integer data type named “c”. This variable is the local variable and in not known to the other functions or to the class. That is the local variable cannot be accessed by other functions. Then the function ends and the class also.

 

Data Types in Java:

In Java there are two types of data types that are primitive data types and non primitive data types. The data types are used to indicate that a particular variable can store what type of data such as an integer type variable can store integer and a float type variable can store floating point numbers.

 

Primitive Data Types:

In primitive data types we have further two types that are Boolean data type and numeric data type, further the numeric data type has two more data types: character and Integral. The integral data types support integer and floating point and they have further their types. The character data type has supports characters.

Consider the following table in which we have defined the purpose of a data types:

Data Type Purpose
int Stores numeric values
float Stores real values
double Stores large real values
char Stores character values
bool Stores either true or false
byte Used to store one byte.
long Used to store larger integer values
short Used to store two bytes in the memory

Now consider the following table in which we have defined the byte size that is taken by a data type:

Data type (for integer) Size in Bytes
int 2
short 2
long 4
Data type (for float) Size in Bytes
float 4
double 8
Data type (for boolean) Size in Bytes
bool 1
Data type (for character) Size in Bytes
char 2