Java Reference
In-Depth Information
state changes to an object via a formal parameter will persist, after the method has completed,
in the actual parameter.
Classes are the templates for objects, defining the fields and methods that each instance possesses.
Arrays behave like object types; they also have reference semantics. There is no class definition
for arrays.
B.4
Wrapper classes
Every primitive type in Java has a corresponding wrapper class that represents the same type
but is a real-object type. This makes it possible to use values from the primitive types where
object types are required, through a process known as autoboxing . The following table lists
the primitive types and their corresponding wrapper type from the java.lang package. Apart
from Integer and Character , the wrapper class names are the same as the primitive-type
names, but with an uppercase first letter.
Whenever a value of a primitive type is used in a context that requires an object type, the
compiler uses autoboxing to automatically wrap the primitive-type value in an appropri-
ate wrapper object. This means that primitive-type values can be added directly to a col-
lection, for instance. The reverse operation— unboxing —is also performed automatically
when a wrapper-type object is used in a context that requires a value of the corresponding
primitive type.
Primitive type
Wrapper type
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
Boolean
B.5
Casting of object types
Because an object may belong to an inheritance hierarchy of types, it is sometimes necessary
to convert an object reference of one type to a reference of a subtype lower down the inherit-
ance hierarchy. This process is called casting (or downcasting ). The cast operator consists of the
name of a class or interface type written in parentheses in front of a variable or an expression.
For instance,
Car c = (Car) veh;
Search WWH ::




Custom Search