Java Programming Tutorials

Java programming tutorials with many code examples!

Java primitive types sizes

Problem:

What are sizes of Java primitive types?

Solution:

The following program answers the question:

package com.farenda.solved;

public class JavaSolved {
    public static void main(String[] args) {
        System.out.println("Sizes of Java primitive types:");
        int charMin = Character.MIN_VALUE;
        int charMax = Character.MAX_VALUE;
        System.out.println("Character min: " + charMin +           ", max: " + charMax);
        System.out.println("     Byte min: " + Byte.MIN_VALUE +    ", max: " + Byte.MAX_VALUE);
        System.out.println("    Short min: " + Short.MIN_VALUE +   ", max: " + Short.MAX_VALUE);
        System.out.println("  Integer min: " + Integer.MIN_VALUE + ", max: " + Integer.MAX_VALUE);
        System.out.println("     Long min: " + Long.MIN_VALUE +    ", max: " + Long.MAX_VALUE);
        System.out.println("    Float min: " + Float.MIN_VALUE +   ", max: " + Float.MAX_VALUE);
        System.out.println("   Double min: " + Double.MIN_VALUE +  ", max: " + Double.MAX_VALUE);
    }
}

Result:

Sizes of Java primitive types:
Character min: 0, max: 65535
     Byte min: -128, max: 127
    Short min: -32768, max: 32767
  Integer min: -2147483648, max: 2147483647
     Long min: -9223372036854775808, max: 9223372036854775807
    Float min: 1.4E-45, max: 3.4028235E38
   Double min: 4.9E-324, max: 1.7976931348623157E308

As you can see all numeric types are with sign!

Share with the World!