Data Types in Java

For every programming language, the data type is very important. Let’s discuss what is the data types in java. The data type specifies the type of value that can store in a variable. Variable is just a memory location to store values. Based on the data type of a variable, the operating system (OS) allocates memory and decides what can be stored in memory.

Two types of Data types in Java

1. Primitive data types in java
2. Non-Primitive/Object/reference data types in java

data types in Java

1. Primitive Data type

The primitive data type in Java is the most basic data type. These are building blocks of data manipulation and predefined by the language.
There are 8 types of primitive data types:

  • boolean data type
  • byte data type
  • char data type
  • short data type
  • int data type
  • long data type
  • float data type
  • double data type
  • boolean:
    • The boolean data type is only two possible values: true and false.
    • The boolean data type can represent only one bit (1 bit) of information.
    • It is used to track true/false conditions.
    • The default value of boolean is false.
    • Example: boolean isUserExist = true; Here “boolean” is data type, “isUserExist” is the name of a variable and it can store only true/false.
class ExampleOfBoolean 
{
   public static void main(String[] args) 
   {
      boolean isUserExist = true;
      if(isUserExist)
      {
         System.out.println("Value of  boolean variable :"+ isUserExist);
      }
    }
}

Output : Value of  boolean variable :true

  • byte:
    • byte data can hold the whole number between -128 to 127. 
    •  It is an 8-bit signed two’s complement integer.
    • Its minimum value is -128 and maximum value is 127.
    • It is used to save memory in large arrays because it is four-time smaller than int data type
    • The default value of the byte is 0.
class ExampleOfByte 
{
   public static void main(String[] args) 
   {
      byte a = 127;
      System.out.println("Value of  byte variable :"+ a);
    
      // It will increment one in "a" again assigning the new value 
      a++;
      System.out.println("Value of  byte variable :"+ a); 
       	
      // It will increment one in "a" again assigning the new value 
      a++;
      System.out.println("Value of  byte variable :"+ a);
    }
}

Output: Value of byte variable: 127
Value of byte variable: -128
Value of byte variable: -127

As you see in the example byte can hold values from -128 to 127. When compiler encounters the value of a = 128 which overflows from the range then it was gone in the negative range.

  • short:
    • short data can hold the whole number between -32,768 to 32,767. 
    •  It is a 16-bit signed two’s complement integer.
    • Its minimum value is -32,768 and maximum value is 32,767.
    • It is also used to save memory in large arrays because it is two times smaller than int data type
    • The default value of the byte is 0.
class ExampleOfShort 
{
   public static void main(String[] args) 
   {
      short a = 127;
      System.out.println("Value of short variable :"+ a);
   }
}

Output: Value of short variable :127

  • int:
    • int data can hold whole number between – 2,147,483,648 to 2,147,483,647.
    •  It is a 32-bit signed two’s complement integer.
    • Its minimum value is – 2,147,483,648 and maximum value is 2,147,483,647.
    • It is used to store an integer value.
    • Default value of byte is 0.
class ExampleOfInt 
{
   public static void main(String[] args) 
   {
      int a = 127;
      System.out.println("Value of int variable :"+ a);
   }
}

Output: Value of int variable :127

  • long:
    • int data can hold whole number between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
    •  It is a 64-bit signed two’s complement integer.
    • Its minimum value is -9,223,372,036,854,775,808 and maximum value is 9,223,372,036,854,775,807
    • It is used when you need a range of values more than those provided by int.
    • The default value of the byte is 0L.
class ExampleOfLong 
{
   public static void main(String[] args) 
   {
      long a = 123456789;
      System.out.println("Value of long variable :"+ a);
   }
}

Output: Value of int variable :123456789

  • float:
    • It is a single-precision 32-bit IEEE 754 floating-point.
    • It is used to save memory in large arrays for floating numbers.
    • The default value of the byte is 0.0f.
    • Enough for holding 6 to 7 decimal digits
    • Example: float a = 1.3f
class ExampleOfFloat 
{
   public static void main(String[] args) 
   {
      float a = 12.123456f;
      System.out.println("Value of float variable :"+ a);
   }
}

Output: Value of float variable :12.123456

  • double:
    • It is a single-precision 64-bit IEEE 754 floating-point.
    • It is used to save memory in large arrays for floating numbers.
    • The default value of byte is 0.0d.
    • Enough for holding 15 decimal digits
    • This data type is generally the default choice. If you declared any variable with decimal, then it is by default double. You must specify its type.
    • For example: float a = 1.2;

NOTE: It will show an error message – “Type mismatch: cannot convert from double to float”. You can declare a float variable as like: Float a = 1.2f;

class ExampleOfDouble 
{
   public static void main(String[] args) 
   {
      double a =  12.123456788888888;
      System.out.println("Value of double variable :"+ a);
   }
}

Output: Value of float variable :12.123456788888888

  • char:
    • This data type is used to store characters.
    • the char data type is a single 16-bit Unicode character
    • Minimum value is ‘\u0000’ (or 0)
    • Maximum value is ‘\uffff’ (or 65,535 inclusive)
    • Example: char ABC = ‘A’;

NOTE: Character should be enclosed in single quotes.

class ExampleOfChar 
{
   public static void main(String[] args) 
   {
      char abc = 'a';
      System.out.println("Value of char variable : "+ abc);
   }
}

Value of char variable : a
NOTE: You can calculate the range of any data type by using the formula:
-2(n-1) to (2(n-1)-1)
Where n is number of bits of primitive data type.
Example of byte data type :
-2(8-1) to (2(8-1)-1) = -128 to 127

2. Non-Primitive data type

  • The non-primitive data type in Java is created by the programmer. They are also known as the “reference variables” or “object variables”.
  • Non-primitive data type variables are created using defined constructors of the classes.
  • String, Array, Map, etc. are examples of reference variables.
class ExampleOfString 
{
   public static void main(String[] args) 
   {
      String name = new String(“Ravi”);
      System.out.println("Value of Ravi variable :"+ name);
   }
}

Output: Value of String variable: Ravi

Data Type Default Value Default Size
boolean false 1 bit
byte 0 1 byte
char ‘\u0000’ 2 bytes
short 0 2 bytes
int 0 4 bytes
long 0L 8 bytes
float 0.0f 4 bytes
double 0.0d 8 bytes

Leave a Comment