How do I find the max and min values of primitive types?
Author: Deron Eriksson
Description: This Java tutorial shows how to display the maximum and minimum values for primitive types.
Tutorial created using: Windows XP || JDK 1.6.0_10 || Eclipse JEE Ganymede SR1 (Eclipse 3.4.1)


JavaSW features wrapper classes for the Java primitive types. The sizes (in bits), the minimum values, and the maximum values can be determined via the SIZE, MIN_VALUE, MAX_VALUE fields on the Byte, Short, Character, Integer, Long, Float, and Double wrapper classes.

The PrimitiveTypeSizeMinMax class displays these values to standard output.

PrimitiveTypeSizeMinMax.java

package com.cakes;

public class PrimitiveTypeSizeMinMax {

	public static void main(String[] args) {
		// Boolean does not have Boolean.SIZE, Boolean.MIN_VALUE, or Boolean.MAX_VALUE
		displaySizeMinAndMax(Byte.TYPE, Byte.SIZE, Byte.MIN_VALUE, Byte.MAX_VALUE);
		displaySizeMinAndMax(Short.TYPE, Short.SIZE, Short.MIN_VALUE, Short.MAX_VALUE);
		displaySizeMinAndMax(Character.TYPE, Character.SIZE, (int) Character.MIN_VALUE, (int) Character.MAX_VALUE);
		displaySizeMinAndMax(Integer.TYPE, Integer.SIZE, Integer.MIN_VALUE, Integer.MAX_VALUE);
		displaySizeMinAndMax(Long.TYPE, Long.SIZE, Long.MIN_VALUE, Long.MAX_VALUE);
		displaySizeMinAndMax(Float.TYPE, Float.SIZE, Float.MIN_VALUE, Float.MAX_VALUE);
		displaySizeMinAndMax(Double.TYPE, Double.SIZE, Double.MIN_VALUE, Double.MAX_VALUE);
	}

	public static void displaySizeMinAndMax(Class<?> type, int size, Number min, Number max) {
		System.out.printf("type:%-6s size:%-2s min:%-20s max:%s\n", type, size, min, max);
	}

}

The console output of executing PrimitiveTypeSizeMinMax can be seen here.

Console Output

type:byte   size:8  min:-128                 max:127
type:short  size:16 min:-32768               max:32767
type:char   size:16 min:0                    max:65535
type:int    size:32 min:-2147483648          max:2147483647
type:long   size:64 min:-9223372036854775808 max:9223372036854775807
type:float  size:32 min:1.4E-45              max:3.4028235E38
type:double size:64 min:4.9E-324             max:1.7976931348623157E308