|
|
Language Fundamentals - Primitive Types
| Data Type |
Bit Size |
Range |
Min/Max values |
Default |
| boolean |
n/a |
true or false |
n/a |
false |
| byte |
signed 8-bit integer |
-(27) to 27-1 |
-128 to 127 |
0 |
| char |
16-bit Unicode 2.0 character |
0 to 216-1 |
0 to 65,535 |
\0000 |
| short |
signed 16-bit integer |
-(215) to 215-1 |
-32,768 to 32,767 |
0 |
| int |
signed 32-bit integer |
-(231) to 231-1 |
-2,147,483,648 to 2,147,483,467 |
0 |
| long |
signed 64-bit integer |
-(263) to 263-1 |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
0l |
| float |
signed 32-bit floating-point |
NEGATIVE_INFINITY to POSITIVE_INFINITY |
Can also have the value NaN (Not a number) |
0.0f |
| double |
signed 64-bit floating-point |
NEGATIVE_INFINITY to POSITIVE_INFINITY |
Can also have the value NaN (Not a number) |
0.0d |
- arithmetic with floating-point numbers will never throw an exception; instead one of the constant values: NEGATIVE_INFINITY, POSITIVE_INFINITY, or NaN are returned
(BB pg 123)
- Variables declared as primitive types are not object references. They are placeholders for storing primitive values (JJ pg29)
- by default integer values are of type int and floating-point values are of type double
- float values are single-precision
- double values are double-precision
Wrapper classes
- all the primitive types have corresponding wrapper classes which allow you to create objects of type Integer, Boolean, Float, etc.
- the wrapper classes have the same names as the primitive types except they begin with a Captial.
| !!! Warning - do NOT mix up the Types !!! |
boolean b;
IS NOT THE SAME AS
Boolean b;
You can say: boolean b = true;
You CANNOT say:
Boolean b = true; -> Boolean is a class, must use
Boolean b = new Boolean(true);
|
Also see
Differentiate between reference and primitive types
Traps
- variables of primitive types handled as Objects
|