Integer Types

     

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

byte

Occupies 8 bits or 1 byte, which is:

-2 7 to 2 7 -1 or -128 to 127

Default value of 0

Example: -17, 123

short

Occupies 16 bits or 2 bytes, which is:

-2 15 to 2 15 -1 or -32,768 to 32,767

Default value of 0

Example: 31,098, -9001

int

Occupies 32 bits or 4 bytes, which is:

-2 31 to 2 31 -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 operation is performed, and then you have an int . So it's extra work, and a little confusing. But there are many situations where you need specifically one of those types.

long

Occupies 64 bits or 8 bytes, which is:

-2 63 to 2 63 -1 or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Default value of 0

Example: 9,223,372,036,854,775,807, 0L

The long takes up a good deal of memory, and so it is typically reserved for use in situations that exceed the capacity of the int , such as representing the total population of the earth, the national deficit in dollars, or the total number of hours I've spent goofing off on the Internet.

You can distinguish between a long and other integral primitive types by writing a literal L after the number value, like this: 87999065L. If you don't do that, the compiler will assume that your number is an int .



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net