Variable and Data Types

Variable is basically a container which holds the value until life of the Java program or application. There is always assigned a data type to every variable which describe the type and size of value hold in it.

Declaration & Initialization of variable:
Datatype variable_name = value[optional];
For eg. char ch = ‘a’;
int number;

Variable naming convention:

  • Variable names cann’t contain the white space
  • Variable names are case sensitive.
  • As per java standards the variable name should begin with lower case letter, for eg. String name. For a large variable name if it has more than one word than we use camel case order in java, for eg. String firstName, lastName etc.

Types of variable
In Java, there are three types of variables like:
1. Local variable
2. static or class variable
3. Instance variable

1. Local variable
Local variable in Java is a variable that’s declared with in the body of the method, constructor or any block. These variables can use only inside the method. Other methods in the class can not access these variables or we can say the scope of local variables are limited to the method only where it is declared.

2. static or variable
We have already discuss about static variable in above Java keyword section.

3. Instance variable
An instance variable is a variable which belong to an object or instance. Every instance has its own copy of instance variable. Also the changes made in instance variable don’t reflect the other instance of that class.
Lets see an example:

class VariableTest {
  // Instance variable
  private double area;

  public void calculateCircleArea(double radius) {

    // Local variable
    double PI = 3.14;

    area = PI * radius * radius;
    System.out.println("Area: " + area);
  }

  public void calculateRectArea(double length, double width) {
    area = length * width;
    System.out.println("Area: " + area);
  }

  public static void main(String args[]) {
    VariableTest circleTest = new VariableTest();
    circleTest.calculateCircleArea(5);

    VariableTest rectangleTest = new VariableTest();
    rectangleTest.calculateRectArea(15, 10);
  }
}

Output:

Area: 78.5
Area: 150.0

As per above example we can observe that ‘area’ is an instance variable which is used with both the instances and have the separate memory for both. While ‘PI’ is a local variable which scope is limited and for the method named calculateCircleArea() only.

Data Types
Data type describes which type of value the variable can entitle, such as integer, float, char, double etc.
There are two data types available in Java:

  • Primitive data type
  • Reference/Object Data type[User created data type]

data type

1. Primitive data types
In Java we have eight primitive data types: boolean, char, byte, short, int, float and double. Below is the tabular representation of all primitive data types:

1. boolean
Boolean data type represent only single value of information either true or false.
For eg. boolean var = true;

2. char
Char data type is a single character also we can say 16-bit unicode character.
For eg. char a = ‘a’;

3. byte
Byte data type is an 8-bit signed two’s complement integer. This data type is useful for saving space in large arrays, like in place of integers we can use byte as it is four time smaller than an integer. Byte data type contains values from -128 to 127;
For eg. byte num = 127;
Note: If we set value greater than 127, we get compile time error like ‘Type mismatch: cannot convert from int to byte’

4. short
Short data type is a 16-bit signed two’s complement integer. This data type is also useful for saving memory in large arrays, like in place of integers we can use short as it is two time smaller than an integer. Short data type contains value from -32,768(-2^15) to 32767(2^15-1).
For eg. short num = 32767;
Note: If we set value greater than 32767, we get compile time error like ‘Type mismatch: cannot convert from int to short’

5. int
Int data type is a 32-bit signed two’s complement integer. It is generally used as the default type for integral values unless there is no issue of memory. Int data type contains value from -2,147,483,648
(-2^31) to 2,147,483,647 (2 ^ 31-1).
For eg. int num = 2147483647;
Note: If we set value greater than 2147483647, we get compile time error like ‘The literal 2147483648 of type int is out of range’

6. long
Long data type is a 64-bit signed two’s complement integer. This type of data type is used when a wider range than int is required. Long data type contains value from -9,223,372,036,854,775,808
(- 2^63) to 9,223,372,036,854,775,807 (2^63-1)
For eg. long num = 9223372036854775807l;
Note: If we set value greater than 9223372036854775807l, we get compile time error like ‘The literal 9223372036854775808l of type long is out of range’

7. float
Float data type is a single-precision 32-bit IEEE 754 floating point value. The important thing is that decimal numbers default to double. Since double does not fit into the float so we need to tell explicitly to define a float.
For eg. float b = 3.6f;
Note: If we set value of b as 3.6, we get compile time error like ‘Type mismatch: connot convert from double to float’

8. double
double data type is a double-precision 32-bit IEEE 754 floating point value. This data type is generally used as default data type for decimal value.
For eg. double d1 = 3.14;

Reference data types
In Java reference data type is a variable that contain the reference or address. These kind of data type are not predefined like primitive data type. The reference data types are arrays, classes and interfaces in Java.
Reference data types can be of

1. array type
This type of reference variable points to an array instance.

2. class type
This type of reference variable points to a class instance.

3. interface type
This type of reference variable points to an object and a method, which is implemented to the corresponding interface
For eg. String s = “Hi”;
int array[] = new int[10];