Java: Primitive data types

The eight primitive data types in Java are:

Values of class type are references. Strings are references to an instance of class String.

Primitive data types in Java
TypeDescriptionDefaultSizeExample Literals
booleantrue or falsefalse1 bittrue, false
bytetwos complement integer08 bits(none)
charUnicode character\u000016 bits 'a', '\u0041', '\101', '\\', '\'', '\n', 'ß'
shorttwos complement integer016 bits(none)
inttwos complement integer032 bits-2, -1, 0, 1, 2
longtwos complement integer064 bits-2L, -1L, 0L, 1L, 2L
floatIEEE 754 floating point0.032 bits 1.23e100f, -1.23e-100f, .3f, 3.14F
doubleIEEE 754 floating point0.064 bits 1.23456e300d, -1.23456e-300d, 1e1d

Boolean

Values of type boolean are not converted implicitly or explicitly (with casts) to any other type. But the programmer can easily write conversion code:
final int i = b?1:0;
final double d = b?1.0:0.0;
final boolean b = i>0?true:false;

Character

The 16-bit Unicode character set underlies both the Java source program and char data type. So, not only are Java programs written in Unicode characters, but Java programs can manipulate Unicode data.

ISO 8859 Family

          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 DIAERESIS
Bosnian 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"

Unicode BMP

Numeric

Range of numeric data types in Java
TypeSizeRange
byte8 bits-128 .. 127
short16 bits-32,768 .. 32,767
int32 bits-2,147,483,648 .. 2,147,483,647
long64 bits-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
float32 bits 3.40282347 x 1038, 1.40239846 x 10-45
double64 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.

zero00±0
infinity2b+10±inf
denormalized0!=0±0.fx2-b+1
normalized1<=e<=2b±1.fx2e-b
NaN2b+1!=0NaN

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.

Conversions: Widenings and Narrowings
from
to byte short char int long float double
byte [id]            
short   [id]          
char     [id]        
int       [id]      
long         [id]    
float           [id]  
double             [id]

Widenings
tofrom
  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.

Narrowings
tofrom
  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