Java Reference
In-Depth Information
Because variables of the primitive types do not refer to objects, there are no methods associated
with the primitive types. However, when used in a context requiring an object type, autoboxing
might be used to convert a primitive value to a corresponding object. See Section B.4 for more
details.
The following table details minimum and maximum values available in the numerical types.
Type name
Minimum
Maximum
byte
-128
127
short
-32768
32767
int
-2147483648
2147483647
long
-9223372036854775808
9223372036854775807
Positive minimum
Positive maximum
float
1.4e-45
3.4028235e38
double
4.9e-324
1.7976931348623157e308
B.2
Casting of primitive types
Sometimes it is necessary to convert a value of one primitive type to a value of another primi-
tive type—typically, a value from a type with a particular range of values to one with a smaller
range. This is called casting . Casting almost always involves loss of information—for example,
when converting from a floating-point type to an integer type. Casting is permitted in Java
between the numeric types, but it is not possible to convert a boolean value to any other type
with a cast, or vice versa.
The cast operator consists of the name of a primitive type written in parentheses in front of a
variable or an expression. For instance,
int val = (int) mean;
If mean is a variable of type double containing the value 3.9, then the statement above would
store the integer value 3 (conversion by truncation) in the variable val .
B.3
Object types
All types not listed in Section B.1, Primitive types , are object types. These include class and
interface types from the Java library (such as String ) and user-defined types.
A variable of an object type holds a reference (or “pointer”) to an object. Assignments and
parameter passing have reference semantics (i.e., the reference is copied, not the object). After
assigning a variable to another one, both variables refer to the same object. The two variables
are said to be aliases for the same object. This rule applies in simple assignment between vari-
ables, but also when passing an object as an actual parameter to a method. As a consequence,
Search WWH ::




Custom Search