Previous | Table of Contents | Next |
14.2.7.2. Copying Objects
Because reference types are not passed by value, assigning one object to another in Java does not copy the value of the object. It merely assigns a reference to the object. Consider the following code:
Button a = new Button(Okay); Button b = new Button(Cancel); a = b;
After these lines are executed, the variable a contains a reference to the object that b refers to. The object that a used to refer to is lost.
To copy the data of one object into another object, use the clone() method:
Vector b = new Vector; c = b.clone();
After these lines run, the variable c refers to an object that is a duplicate of the object referred to by b. Note that not all types support the clone() method. Only classes that implement the Cloneable interface may be cloned.
Arrays are also reference types, and assigning an array simply copies a reference to the array. To actually copy the values stored in an array, you must assign each of the values individually or use the System.arraycopy() method.
14.2.7.3. Checking Objects for Equality
Another implication of passing objects by reference is that the == operator tests whether two variables refer to the same object, not whether two objects contain the same values. To actually test whether two separate objects are the same, you must use a specially written method for that object type (just as you might use strcmp() to compare C strings for equality). In Java, a number of classes define an equals() method that you can use to perform this test.
14.2.7.4. Java Has No Pointers
The referencing and dereferencing of objects is handled for you automatically by Java. Java does not allow you to manipulate pointers or memory addresses of any kind:
There are two reasons for these restrictions:
To a C programmer, the lack of pointers and pointer arithmetic may seem an odious restriction in Java. But once you get used to the Java object-oriented programming model, it no longer seems like a serious restriction at all. The lack of pointers does mean that you probably cant do things like write UNIX device drivers in Java (at least not without using native methods written in C). But big dealmost of us never have to do this kind of low-level programming anyway.
14.2.7.5. null
The default value for variables of all reference types is null. null is a reserved value that indicates an absence of referencei.e., that a variable does not refer to any object or array.
In Java, null is a reserved keyword, unlike NULL in C, where it is just a constant defined to be 0. null is an exception to the strong typing rules of Javait may be assigned to any variable of reference type (i.e., any variable that has a class, interface, or array as its type).
null cant be cast to any primitive type, including integral types and boolean. It shouldnt be considered equal to zero (although it may be implemented this way).
14.2.7.6. Reference Type Summary
The distinction between primitive types passed by value, and objects and arrays passed by reference is a crucial one in Java. Be sure you understand the following:
Now that you know objects are passed by reference, we should discuss how they are created, used, and destroyed. The following subsections provide a very brief overview of objects. The section Classes and Objects in Java explains classes and objects in much greater detail.
14.2.8.1. Creating Objects
Declaring a variable to hold an object does not create the object itself; the variable only holds the reference to the object. To actually create an object, you must use the new keyword. This is followed by the objects class (i.e., its type) and an optional argument list in parentheses. These arguments are passed to the constructor method for the class, which serves to initialize internal fields in the new object. For example:
java.awt.Button b = new java.awt.Button(); ComplexNumber c = new ComplexNumber(1.0, 1.414);
There are actually two other ways to create an object. First, you can create a String object simply by enclosing characters in double quotes:
String s = This is a test;
Because strings are used so frequently, the Java compiler provides this technique as a shortcut. Another way to create objects is by calling the newInstance() method of a Class object. This technique is generally used only when dynamically loading classes, so we wont discuss it here. In Java 1.1, objects can also be created by de-serializing themi.e., re-creating an object that had its state saved through serialization.
The memory for newly created objects is dynamically allocated. Creating an object with new in Java is like calling malloc() in C to allocate memory for an instance of a struct. It is also, of course, a lot like using the new operator in C++. (Below, well see where this analogy to malloc() in C and new in C++ breaks down.)
Previous | Table of Contents | Next |