In Java, data types specify the different sizes and values that can be stored in a variable. Java is a statically-typed language, which means that all variables must be declared before they can be used. Java has two categories of data types:

  1. Primitive Data Types
  2. Reference/Object Data Types

1. Primitive Data Types

There are eight primitive data types in Java:

  • byte
  • short
  • int
  • long
  • float
  • double
  • char
  • boolean

byte

  • Size: 1 byte (8 bits)
  • Range: -128 to 127
  • Default Value: 0
  public class Main {
    public static void main(String[] args) {
        byte b = 100;
        System.out.println("Byte value: " + b);
    }
}
  

short

  • Size: 2 bytes (16 bits)
  • Range: -32,768 to 32,767
  • Default Value: 0
  public class Main {
    public static void main(String[] args) {
        short s = 10000;
        System.out.println("Short value: " + s);
    }
}
  

int

  • Size: 4 bytes (32 bits)
  • Range: -2^31 to 2^31-1
  • Default Value: 0
  public class Main {
    public static void main(String[] args) {
        int i = 100000;
        System.out.println("Int value: " + i);
    }
}
  

long

  • Size: 8 bytes (64 bits)
  • Range: -2^63 to 2^63-1
  • Default Value: 0L
  public class Main {
    public static void main(String[] args) {
        long l = 100000L;
        System.out.println("Long value: " + l);
    }
}
  

float

  • Size: 4 bytes (32 bits)
  • Range: approximately ±3.40282347E+38F (6-7 significant decimal digits)
  • Default Value: 0.0f
  public class Main {
    public static void main(String[] args) {
        float f = 10.5f;
        System.out.println("Float value: " + f);
    }
}
  

double

  • Size: 8 bytes (64 bits)
  • Range: approximately ±1.79769313486231570E+308 (15 significant decimal digits)
  • Default Value: 0.0d
  public class Main {
    public static void main(String[] args) {
        double d = 10.5;
        System.out.println("Double value: " + d);
    }
}
  

char

  • Size: 2 bytes (16 bits)
  • Range: 0 to 65,535 (Unicode characters)
  • Default Value: ‘\u0000’
  public class Main {
    public static void main(String[] args) {
        char c = 'A';
        System.out.println("Char value: " + c);
    }
}
  

boolean

  • Size: 1 bit
  • Values: true or false
  • Default Value: false
  public class Main {
    public static void main(String[] args) {
        boolean bool = true;
        System.out.println("Boolean value: " + bool);
    }
}
  

2. Reference/Object Data Types

Reference data types include class, interface, array, and enum types. They are created using defined constructors of the classes.

String

  • Size: Varies (depends on the content)
  • Default Value: null
  public class Main {
    public static void main(String[] args) {
        String str = "Hello, World!";
        System.out.println("String value: " + str);
    }
}
  

Array

  • Size: Varies (depends on the content)
  • Default Value: null
  public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        System.out.println("Array values: ");
        for (int i : arr) {
            System.out.println(i);
        }
    }
}