O'Reilly logo

Java Garage by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Integer Types

The Java integer types are byte, short, int, and long.

byte

Occupies 8 bits or 1 byte, which is:

-27 to 27-1 or -128 to 127

Default value of 0

Example: -17, 123

short

Occupies 16 bits or 2 bytes, which is:

-215 to 215-1 or -32,768 to 32,767

Default value of 0

Example: 31,098, -9001

int

Occupies 32 bits or 4 bytes, which is:

-231 to 231-1 or -2,147,483,648 to 2,147,483,647

Default value of 0

Example: 50, 2147000000, -53000

The int is probably the most commonly used integral type in Java. That's because it is often not worth trying to save memory space by using a smaller type, such as a byte. Here's why: when you perform a comparison operation or an arithmetic operation with a byte, it gets promoted to an int anyway by the runtime, then the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required