Boundaries of primitive data types in Java

By xngo on February 20, 2019

The code below will show the minimum and the maximum value of Java primitive data types. Note: Unlike other languages(e.g. C/C++), Java doesn't provide unsigned types of its integers.

The code

package net.openwritings.java.lang;
 
public class BoundariesOfPrimitiveDataTypes
{
    public static void main(String[] args)
    {
        String s = "";
 
        // Byte
        s = String.format("%-24s: min = %,-27d, max = %,-27d",
                            Byte.class, Byte.MIN_VALUE, Byte.MAX_VALUE);
        System.out.println(s);
 
        // Short
        s = String.format("%-24s: min = %,-27d, max = %,-27d",
                            Short.class, Short.MIN_VALUE, Short.MAX_VALUE);
        System.out.println(s);
 
        // Integer
        s = String.format("%-24s: min = %,-27d, max = %,-27d",
                            Integer.class, Integer.MIN_VALUE, Integer.MAX_VALUE);
        System.out.println(s);
 
        // Long
        s = String.format("%-24s: min = %,-27d, max = %,-27d",
                            Long.class, Long.MIN_VALUE, Long.MAX_VALUE);
        System.out.println(s);
 
        // Float
        s = String.format("%-24s: min = %-27s, max = %,f", 
                            Float.class, Float.MIN_VALUE, Float.MAX_VALUE);
        System.out.println(s);
 
        // Double
        s = String.format("%-24s: min = %-27s, max = %s", 
                            Double.class, Double.MIN_VALUE, Double.MAX_VALUE);
        System.out.println(s);
 
        // Boolean
        s = String.format("%-24s: min = %-27b, max = %b", 
                            Boolean.class, Boolean.FALSE, Boolean.TRUE);
        System.out.println(s);
 
    }
}

The output

class java.lang.Byte    : min = -128                       , max = 127                        
class java.lang.Short   : min = -32,768                    , max = 32,767                     
class java.lang.Integer : min = -2,147,483,648             , max = 2,147,483,647              
class java.lang.Long    : min = -9,223,372,036,854,775,808 , max = 9,223,372,036,854,775,807  
class java.lang.Float   : min = 1.4E-45                    , max = 340,282,346,638,528,860,000,...,000.000000
class java.lang.Double  : min = 4.9E-324                   , max = 1.7976931348623157E308
class java.lang.Boolean : min = false                      , max = true

Github

  • https://github.com/xuanngo2001/java-small/blob/master/src/net/openwritings/java/lang/BoundariesOfPrimitiveDataTypes.java

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.