The eight primitive data types in Java are:
Values of class type are references. Strings are references to an instance of class String.
Type | Description | Default | Size | Example Literals |
---|---|---|---|---|
boolean | true or false | false | 1 bit | true, false |
byte | twos complement integer | 0 | 8 bits | (none) |
char | Unicode character | \u0000 | 16 bits | 'a', '\u0041', '\101', '\\', '\'', '\n', 'ß' |
short | twos complement integer | 0 | 16 bits | (none) |
int | twos complement integer | 0 | 32 bits | -2, -1, 0, 1, 2 |
long | twos complement integer | 0 | 64 bits | -2L, -1L, 0L, 1L, 2L |
float | IEEE 754 floating point | 0.0 | 32 bits | 1.23e100f, -1.23e-100f, .3f, 3.14F |
double | IEEE 754 floating point | 0.0 | 64 bits | 1.23456e300d, -1.23456e-300d, 1e1d |
final int i = b?1:0; final double d = b?1.0:0.0; final boolean b = i>0?true:false;
Removed Added A4 U+00A4 CURRENCY SIGN U+20AC EURO SIGN A6 U+00A6 BROKEN BAR U+0160 LATIN CAPITAL LETTER S WITH CARON A8 U+00A8 DIAERESIS U+0161 LATIN SMALL LETTER S WITH CARON B4 U+00B4 ACUTE ACCENT U+017D LATIN CAPITAL LETTER Z WITH CARON B8 U+00B8 CEDILLA U+017E LATIN SMALL LETTER Z WITH CARON BC U+00BC VULGAR FRACTION ONE QUARTER U+0152 LATIN CAPITAL LIGATURE OE BD U+00BD VULGAR FRACTION ONE HALF U+0153 LATIN SMALL LIGATURE OE BE U+00BE VULGAR FRACTION THREE QUARTERS U+0178 LATIN CAPITAL LETTER Y WITH DIAERESISBosnian bs; Czech cs; Estonian et; Finnish fi; Croatian hr; Lithuanian lt; Latvian lv; Slovak sk; Slovenian sl use the "s with caron". Welsh cy, French fr use the "y with diaeresis"
Type | Size | Range |
---|---|---|
byte | 8 bits | -128 .. 127 |
short | 16 bits | -32,768 .. 32,767 |
int | 32 bits | -2,147,483,648 .. 2,147,483,647 |
long | 64 bits | -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807 |
float | 32 bits | 3.40282347 x 1038, 1.40239846 x 10-45 |
double | 64 bits | 1.7976931348623157 x 10308, 4.9406564584124654 x 10-324 |
In the IEEE 754 floating-point number standard the finite, nonzero values are of the form s · m · 2e. For words of length 32 bits s is 0 (for positive) or 1 (for negative), m is a positive integer less that 224, and e is between -127 and 128, inclusive. For words of length 64 bits s is 0 (for positive) or 1 (for negative), m is a positive integer less that 253, and e is between -1023 and 1024, inclusive.
zero | 0 | 0 | ±0 |
infinity | 2b+1 | 0 | ±inf |
denormalized | 0 | !=0 | ±0.fx2-b+1 |
normalized | 1<=e<=2b | ±1.fx2e-b | |
NaN | 2b+1 | !=0 | NaN |
In the IEEE 754 floating-point number standard for 32 bit words, the largest positive number that can be stored is 1.11111111111111111111111 x 2127 = 3.40282347 x 1038. The smallest positive number (in normal form) is 1.00000000000000000000000 x 2-126 = 1.17549435 x 10-38. The smallest positive number (not in normal form) is 0.00000000000000000000001 x 2-126 = 2-23 x 2-126 = 1.40239846 x 10-45. Negative numbers are stored symmetrically.
In the floating-point number standard for 64 bit words, the largest positive number that can be stored is 1.11111....11111 x 21023 = 1.7976931348623157 x 10308. The smallest positive number (in normal form) is 1.00000...00000 x 2-1022 = 2.22507438585072014 x 10-308. The smallest positive number (not in normal form) is 0.00000...00001 x 2-1022 = 2-52 x 2-1022 = 4.9406564584124654 x 10-324
Operators on floating-point numbers behave exactly as specified by IEEE 754. Java requires full support of IEEE 754 denormalized floating-point numbers.
from | ||||||||
---|---|---|---|---|---|---|---|---|
to | byte | short | char | int | long | float | double | |
byte | [id] | |||||||
short | [id] | |||||||
char | [id] | |||||||
int | [id] | |||||||
long | [id] | |||||||
float | [id] | |||||||
double | [id] |
to | from | |||||
byte -128 .. 127 |
short -32,768 .. 32,767 |
char \u0000 .. \uFFFF |
int -1 | long -1 |
float 1.2 |
|
short | -128..127* | [id] | ||||
int | -128..127* | -32,768 .. 32,767* | 0 .. 65,535+ | [id] | ||
long | -128 .. 127* | -32,768 .. 32,767* | 0 .. 65,535+ | -1 | [id] | |
float | -128.0 .. 127.0 | -32,768.0 .. 32,767.0 | 0.0 .. 65,535.0 | -1.0p | -1.0p | [id] |
double | -128.0 .. 127.0 | -32,768.0 .. 32,767.0 | 0.0 .. 65,535.0 | -1.0 | -1.0p | 1.2 |
* sign extend (two's complement)
+ zero fill
p IEEE round-to-nearest integer, possible loss of precision
With the exception of boolean, any primitive type can be converted to any other. But there might be significant loss of data. Those conversions that may lose data are called narrowing are must be explicited demanded by the programmer using a cast. The following table suggests how these conversions are performed.
There is very little reason to perform any of these conversions. Usually all calculations are done in the appropriate data type: int, long, float, or double.
Of course, the programmer must be aware of the finite limits of the arithemetic data types regardless of which one is chosen. There are no byte and short operations so all these values are promoted first to int. When stored back in byte and short variables the programmer must use a cast (a narrowing) and prepare for possible loss of data.
The conversion causing the most trouble is the char to short conversion. Since they are both 16 bits it is tempting to convert chars to shorts to do arithmetic. But a twos-complement 16-bit number is both positive and negative and asymetric on top of that. Whereas the char type is most naturally considered a simple enumeration beginning with zero. In all cases the int type, not the short type is the one to choose for manipulating the bits of characters. These applies to those cases involving byte IO of text data; there is no unsigned data in Java. The data type byte is not unsigned; it supports a small range of positive and negative numbers.
to | from | ||||||
byte -128 .. -1,0,1, .. 127 |
short -32,768 .. -1,0,1, .. 32,767 |
char \u0000 .. \uFFFF |
int -1,0,1 | long -1,0,1 |
float 1.2 |
double 7.89 |
|
byte | [id] | 0 .. -1,0,1, .. -1 | 0..-1 | -1,0,1d | -1,0,1d | (int) | (int) |
short | [id] | 0..-1 | -1,0,1d | -1,0,1d | (int) | (int) | |
char | \uFF80 .. \uFFFF,\u0000, \u0001 .. \u007F | \u8000 .. \uFFFF,\u0000, \u0001 .. \u7FFF | [id] | \uFFFF,\u0000,\u0001 | \uFFFF,\u0000,\u0001 | (int) | (int) |
int | [id] | -1,0,1 | 1r | 7r | |||
long | [id] | 1r | 7r | ||||
float | [id] | 7.89r | |||||
double | [id] |
d discard all but n lowest order bits
r IEEE round-to-nearest
Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Wed May 28 13:27:30 EDT 2003