What are data types?

The data types are used to determine the type of data that is stored in memory. Data types are used to represent the different values to be stored in the variable. The variable is a name of memory location that means when you create a variable you reserve space in the memory and based on that data type of the variable the operating system allocates the memory.

For example:

int x;
x=5; // value of variable

In the above example the variable name is x and the data type assigned to this variable is integer which means the variable x can store only the integer values.

In java, we can classify data types into 2 categories.

  1. Primitive Data Types
  2. Reference Data Type or Non Primitive Data Types

We will see more about these data types below.

Primitive Data Types:

The primitive data types are the built in data types or pre defined data types by the language.
There are eight types of primitive data types supported by java.

  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. boolean
  8. char

byte :
It is an 8 bit signed two’s complement integer. The minimum value of byte type variable is -128 and the maximum value byte type variable is 127, both the values are inclusive. This data type is used to save memory in large arrays.
Below is the example of byte usage

public class byteExample {
  public static void main(String[] args) {
    //create a Byte object using one of the below given constructors
    //create a Byte object from byte
    byte b = 2;
    Byte obj1 = new Byte(b);
    //creating  Byte object from string.
    Byte obj2 = new Byte("4");
    //print value of Byte objects
    System.out.println(obj1);
    System.out.println(obj2);
  }
}

short :
It is a 16 bit signed two’s complement integer. The minimum value of short type variable is -32,768 and the maximum value short type variable is 32,767, both the values are inclusive. This data type is also used to save memory in large arrays.
Below is the example of short usage

public class shortExample {
    public static void main(String[] args) {
         short s1 = 100;
         short s2 = 360;
         System.out.println("The value of short variable s1 is :" + s1);
         System.out.println("The value of short variable s2 is :" + s2);
    }
}

int :
It is a 32 bit signed two’s complement integer. The minimum value of int type variable is -2,147,483,648 and the maximum value int type variable is 2,147,483,647, both the values are inclusive. This data type is used as the default data type for the integer values.
Below is the example of int usage

public class intExample{
	public static void main(String[] args) {
        int a = 10;
        int b = 100;
	System.out.println("The value of int variable a is :" + a);
        System.out.println("The value of int variable b is :" + b);
    }
}

long :
It is a 64 bit signed two’s complement integer. The minimum value of long type variable is -263 and the maximum value long type variable is 263-1. This data type is used when we need a wider range.
Below is the example of long usage

public class longExample {
  public static void main(String[] args) {
    long limit = 5;
    long fact = 1;
    for (int i = 1; i <= limit; i++) {
      fact = 1; //  factorial will be initialized
      int j = 2;
      while (j <= i)
        fact *= j++;
      System.out.println(i + "!" + " is " + fact);
    }
  }
}

float :
It is a single precision 32 bit IEEE 754 floating point. It is used to save memory in large arrays. This data type should never be used for accurate values such as currency.
Below is the example of float usage

import java.util.*;
public class HelloWorld {
    public static void main(String[] args) {
         float f = 29.4f;
         System.out.println("The value of float variable f is :" + f);                
    }
}

double :
It is a double precision 64 bit IEEE 754 floating point. It is used as the default data type for decimal values. This data type should not be used for the exact values such as currency.
Below is the example of double usage

public class doubleExample{
       public static void main(String[] args) {
              double d = 10000.00;
              System.out.println("The value of double variable d is :" + d);               
        }
}

boolean :
This data type represents the one bit information. There are only two possible values i.e. true and false. This data type is used for the true and false conditions. The default value is false.
Below is the example of boolean usage

public class booleanExample{
        public static void main(String[] args) {  
               boolean b1 = true;
               boolean b2 = false;
               boolean b3 = (10 > 20)? true:false;
               System.out.println("The value of boolean variable b1 is :" + b1);
               System.out.println("The alue of boolean variable b2 is :" + b2);
               System.out.println("The value of boolean variable b3 is :" + b3);            
        }
}

char :
It is a single 16 bit Unicode character. The minimum value is 0 and maximum value is 65,535 inclusive. It is used to store any character value.
Below is the example of boolean usage

public class HelloWorld {
         public static void main(String[] args) {
                char c1 = 'g';
                char c2 = 71; /* ASCII code of 'G'*/
                System.out.println("The value of char variable c1 is :" + c1);   
                System.out.println("The value of char variable c2 is :" + c2);             
        }
}

Reference Data Type (Non-primitive Data Types) :

The value of the reference type variable in contrast to the primitive data type is a reference to the value or set of values represented by the variable. They are created using defined constructors of the classes. Objects can be accessed through reference data types. The variables are to be declared of specific type which cannot be changed further.
The default value of reference data type is null. There are three types of reference data types.

  1. Arrays
  2. Class
  3. Interface

Arrays : The array is an object which holds the maximum number of fixed values of single type. It stores the similar elements. In java, array is index based the first element of the array is stored at index 0.

Class : The class is a blueprint from which individual objects are created. The group of objects has common properties.
The class contains

  • Data member
  • Method
  • Constructor

Interface : The interface is a blueprint of the class. The interface has static constants and abstract methods only.