10 Mar 1996
Everything in Java is a class. If a class has fields that are instance variables, then they are stored inside dynamically allocated objects created by new to represent instances of the class. However, when Java documentation uses the name "Object" capitalized like a proper name, then it is referencing a special class built into the language.
The Object class is the parent of all other classes. When any new class definition omits the extends keyword, it implicitly extends the Object class. A reference variable declared to designate member of the Object class can actually point to any dynamically allocated object from any class. In Java:
corresponds roughly to the C/C++ language declaration:
void * x;
That is, "x" can designate (point to ) any type of object.
There is an important difference here between C and Java. In C there is an address operation ("&") which can be used on individual variables of primitive data types. This allows a C pointer variable to designate individual fields, internal variables, and arguments. This is also remarkably unsafe and the potential source of many programming errors.
Java only allows a reference variable to point to an instance of a class dynamically allocated with the new operation. It is not possible to simply redeclare a primitive variable to be an object.
However, since there are certainly cases where one would like to create an object that is a simple int (or float, char, or whatever), the Java language supplied a set of predefined classes in the library that act as wrappers for the primitive data types.
For example, to handle simple integer values (including values from the primitive types byte, short, and int), the Java language defines the "Integer" wrapper class. Again, note that while int is a primitive and a reserved word, "Integer" is capitalized like a proper name and represents a predefined class of the Java language library.
Integer count = new Integer(25);
In this example, the variable "count" is not an arithmetic type. It is a reference to an object from the class Integer. An Integer object is dynamically allocated and contains, essentially, one internal instance variable that contains the value (25 in this case). The Integer class provides methods to extract this value as a byte, short, or int value. However, "count" cannot be used in arithmetic expression such as "count++;" or "b=count+5;".
In addition to its use as a wrapper class, the Integer class also provided an opportunity for the Java language to define a number of additional static methods (plain old ordinary functions) that provide services generically related to integer values. For example, the function that converts strings containing the character representation of a number to an integer (what C programmers recognize as "atoi") is in Java a member of the Integer class.
A reference to an object created from the class Integer can be assigned to a reference variable of class Object and can be passed in a call to any method that takes a reference to an Object as an argument. Program logic can then use the instanceof operation to determine that it really was an Integer.
An object of type Integer is not the same as a variable of type int. The intValue method can be used to "convert" an Integer to an int, although a more precise statement would be that it extracts the value of the internal instance variable from the object.
int k = count.intValue();
An integer literal or variable is provided to assign the Integer object its value when it is created. In the example
Integer count = new Integer(25);
The expression "Integer(25)" is a constructor for the Integer object. Constructors will be explained separately. They are a special type of method that can appear in a dynamic allocation request. The new operation is not constrained to appear only in assignments and initialization. Thus "new Integer(25)" can be regarded as a conversion of the int value 25 to a reference to an object of type Integer containing the value 25 and can then be used in expressions or as arguments to a method call where a reference is required instead of a primitive type.
Objects from a wrapper class are generally constant (you cannot change their value). If you needed to change the value of the object that "count" designates, you simply allocate a new Integer object with the new value and assign the resulting reference to it as a new value of "count".
count = new Integer(24);
Don't worry about what happened to the old object. That is taken care of in garbage collection, explained later.
Continue Back PCLT
Copyright 1996 PC Lube and Tune -- Java H. Gilbert