The ranges of primitive types
A primitive is stored as one or many bytes (1 byte = 8 bits). As all primitives are signed, the leftmost bit is used to represent the sign 0 for positive and 1 for negative. The rest of the bits represent the value.
eg.
a positive byte: 0 0010011
If you want to calculate the capacity of a bit, you have to exclude the first byte.
A byte has 8 bits, it can have 2^7 values. As the zero is included as positive, its value range will be 2^7 - 1 for the positive range and 2^7 for the negative range.
This table shows the range of all numeric primitives available with Java:
Type
|
Bytes
|
Bits
|
Minimum range
|
Maximum range
|
byte
|
1
|
8
|
-2^7
|
2^7 - 1
|
short
|
2
|
16
|
-2^15
|
2^15 - 1
|
int
|
4
|
32
|
-2^31
|
2^31 - 1
|
long
|
8
|
64
|
-2^63
|
2^63 - 1
|
float
|
4
|
32
|
n/a (may be platform dependant)
|
n/a (may be platform dependant)
|
double
|
8
|
64
|
n/a (may be platform dependant)
|
n/a (may be platform dependant)
|
The default value of numbers
Non floating point: integer
By default a non-floating point decimal is seen as an integer type (32 bits).
int x = 232413; // Ok // Ok, implicit casting from 'int' to 'byte' (narrowing): explicit narrowing for a non-floating point is only possible during declaration byte age = 23; // Same than: byte age = (byte)23; int x = 23; // compilation error: "possible loss of precision" byte age = x; // Indeed, as 23 is seen as an integer by Java, you have to explicitly cast it to a byte: byte age = (byte)x; // Ok // Note that this will not compile: byte age = 10+12; // You have to explicitly cast: byte age = (byte)(10+12); // Ok, implicit casting from 'int' to 'long' (widening) long distance = 34532; // Same than: long distance = (long)32532 // If the number you assign is greater than the the range of the variable's type then you have to cast explicitly // The following won't compile, the limit of a byte is 127. The compiler's error-message is "possible loss of precision". byte age = 200; // Ok, 'age' value is '-56' byte age = (byte)200; // If the number that you assigned is greater than an integer range then you have to cast manually: // The following won't compile, message: "integer number too large: 5000000000" long speed = 5000000000; // Cast explicitly to long long speed = 5000000000L; // Cast explicitly to long long speed = 5000000000l; // Cast explicitly to long long speed = (long)5000000000;
Floating point: double
By default a floating point decimal is seen as a double type (64 bits).
double d = 11.43; // Ok double d = 11.43d; // Ok double d = 11.43D; // Ok double d = (double)11.43; // OkYou have to explicitly cast a floating point decimal if you assign it to a variable's type different than 'double':
// Won't compile, message: "possible loss of precision". float speed = 11.56; float speed = 11.56F; // Ok float speed = 11.56f; // Ok float speed = (float)11.56; // Ok
Widening and narrowing castings
Note that only a primitive can widen or narrow to an on other primitive. A wrapper can not wider or narrow to an other wrapper.
eg.
// This won't compile Long x = new Integer(10);
Widening
A widening is the action to cast a variable's type to a wider type. This is done implicitly by the compiler.
int age = 31; double dAge = age; // Ok long lAge = age; // Ok float fAge = age; // Ok
Narrowing
A narrowing is the action to cast a variable's type to a narrower type. This has to be done explicitly.
long distance = 515209; // Won't compile, message: "possible loss of precision" int iDistance = distance; // Ok int iDistance = (int)distance; // Won't compile, message: "possible loss of precision" short sDistance = distance; // Ok short sDistance = (short)distance; // Won't compile, message: "possible loss of precision" short bDistance = distance; // Ok short bDistance = (byte)distance; double distance = 650.86; // Won't compile, message: "possible loss of precision" float iDistance = distance; // Ok, value: 650.86 float iDistance = (float)distance; // Won't compile, message: "possible loss of precision" int iDistance = distance; // Ok, new value: 650 int iDistance = (int)distance;
Character type
A character type is an unsigned 16 bits integer. It can accept number, unique or Unicode character.
Eg.
char a = 'a'; // Ok char a = '@'; // Ok char a = '\u004E'; // Ok, Unicode char a = 0x892; // Ok, hexadecimal char a = 982; // Ok, integer