More on Primitive Numerics


To this point, you have learned about the numeric primitive types int, double, and float. You have learned about the basic arithmetic operators, the increment/decrement operators for int, and compound assignment.

Other Integer Types

You have been using the int type for integer numerics. There are other integer types available, each representing different sizes available in which to store numbers of that type. Table 10.1 lists all integral types, their size, and the range of values that they support.

Table 10.1. Integer Types

Type

Size

Range

Example Literals

byte

8 bits

-128 to 127

0 24

char

16 bits

0 to 65535

17 'A' '%' \u0042 \062

short

16 bits

-32768 to 32767

1440 0x1AFF

int

32 bits

-2,147,483,648 to 2,1477,483,647

-1440525 0x41F0 083

long

64 bits

-9223372036854775808 to 9223372036854775807

-90633969363693 77L 42


The char type is also a numeric type. Refer to Lesson 3 for more information on char.

You usually express numeric literals as decimal, or base 10, numbers. Java also allows you to represent them in hexadecimal (base 16) or octal (base 8).

You prefix hexadecimal literals with 0x.

 assertEquals(12, 0xC); 

You prefix octal literals with 0:

 assertEquals(10, 012); 

Integral literals are by default of int type. You can force an integral literal to be a long by suffixing it with the letter L. The lowercase letter l can also be used, but prefer use of an uppercase L, since the lowercase letter is difficult to discern from the number 1.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

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