Why does Java strictly specify the range and behavior of its primitive types?


Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) namely, boolean, byte, char, short, int, long, float, double.

Java strictly specifies range and behaviors of all the primitive datatypes. Making the users choose the required datatypes based on the application thus reducing the unused occupancy of memory.

For example, if you need to store an integer constant of single digit using integer would be a waste of memory instead, you can use byte type since 8 bits would be necessary to store it.

Example

Following Java example lists out the ranges of the primitive datatypes.

public class PrimitiveDatatypes {
   public static void main(String[] args) {
      System.out.println("Minimum value of the integer type: "+Integer.MIN_VALUE);
      System.out.println("Maximum value of the integer type: "+Integer.MAX_VALUE);
      System.out.println(" ");
      System.out.println("Minimum value of the float type: "+Float.MIN_VALUE);
      System.out.println("Maximum value of the float type: "+Float.MAX_VALUE);
      System.out.println(" ");
      System.out.println("Minimum value of the double type: "+Double.MIN_VALUE);
      System.out.println("Maximum value of the double type: "+Double.MAX_VALUE);
      System.out.println(" ");
      System.out.println("Minimum value of the byte type: "+Byte.MIN_VALUE);
      System.out.println("Maximum value of the byte type: "+ Byte.MAX_VALUE);
      System.out.println(" ");
      System.out.println("Minimum value of the short type: "+Short.MIN_VALUE);
      System.out.println("Maximum value of the short type: "+Short.MAX_VALUE);
      System.out.println(" ");
      System.out.println("Minimum value of the long type: "+Long.MIN_VALUE);
      System.out.println("Maximum value of the long type: "+Long.MAX_VALUE);
   }
}

Output

Minimum value of the integer type: -2147483648
Maximum value of the integer type: 2147483647

Minimum value of the float type: 1.4E-45
Maximum value of the float type: 3.4028235E38

Minimum value of the double type: 4.9E-324
Maximum value of the double type: 1.7976931348623157E308

Minimum value of the byte type: -128
Maximum value of the byte type: 127

Minimum value of the short type: -32768
Maximum value of the short type: 32767

Minimum value of the long type: -9223372036854775808
Maximum value of the long type: 9223372036854775807

Updated on: 01-Aug-2019

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements