Chapter 4. Reference TypesReference types hold references to objects and provide a means to
access those objects stored somewhere in memory. The memory locations are irrelevant to programmers. All reference types are a subclass of type Show
Table 4-1 lists the five Java reference types. Table 4-1. Reference types
Comparing Reference Types to Primitive TypesThere are two type categories in Java: reference types and primitive types. Table 4-2 shows some of the key differences between them. See Chapter 3 for more details. Table 4-2. Reference types compared with primitive types
Default ValuesDefault values are the values assigned to instance variables in Java, when no initialization value has been explicitly set. Instance and Local Variable ObjectsInstance variables (i.e., those declared at the class level) have a default value of Local variables (i.e., those declared within a method) do not have a default value, not even a value of Although object references with a value of
Invoking a method on a reference variable that is
ArraysArrays are always given a default value whether they are declared as instance variables or local variables. Arrays that are declared but not initialized are given a default value of In the following code, the
Multidimensional arrays in Java are actually arrays of arrays. They may be initialized with the new operator or by placing their values within braces. Multidimensional arrays may be uniform or nonuniform in shape:
Anonymous arrays allow for the creation of a new array of values anywhere in the code base:
Conversion of Reference TypesAn object can be converted to the type of its superclass (widening) or any of its subclasses (narrowing). The compiler checks conversions at compile time, and the Java Virtual Machine (JVM) checks conversions at runtime. Widening Conversions
Narrowing Conversions
Objects cannot be converted to an unrelated type—that is, a type other than one of its subclasses or superclasses. Doing so will generate an
Converting Between Primitives and Reference TypesThe automatic conversion of primitive types to reference types, and vice versa, is called autoboxing and unboxing, respectively. For more information, refer back to Chapter 3. Passing Reference Types into MethodsWhen an object is passed into a method as a variable:
The following example illustrates passing reference types and primitive types into methods and the effects on those types when changed by the called method:
Comparing Reference TypesReference types are comparable in Java. Equality operators and the
Using the Equality OperatorsThe In the following example,
In the following example, the memory addresses are not equal, so the statement
Using the equals() MethodTo
compare the contents of two class objects, the Tip By default, the For example, if you want to compare values contained in two instances of the same class, you should use a programmer-defined Comparing StringsThere are two ways to check whether strings are equal in Java, but the definition of “equal” for each of them is different:
Here is a program that shows how strings are evaluated using the
Tip Objects of
the Comparing Enumerations
Copying Reference TypesWhen reference types are copied, either a copy of the reference to an object is made, or an actual copy of the object is made, creating a new object. The latter is referred to as cloning in Java. Copying a Reference to an ObjectWhen copying a reference to an object, the result is one object with two references. In the following example,
Cloning ObjectsCloning results in another copy of the object, not just a copy of a reference to an object. Cloning is not available to classes by default. Note that cloning is usually very complex, so you should consider a copy constructor instead, for the following reasons:
Shallow and deep cloningShallow and deep cloning are the two types of cloning in Java. In shallow cloning, primitive values and the references in the object being cloned are copied. Copies of the objects referred to by those references are not made. In the following example,
In deep cloning, the cloned object makes a copy of each of its object’s fields, recursing through all other objects referenced by it. A deep-clone method must be defined by the programmer, as the Java API does not provide one. Alternatives to deep cloning are serialization and copy constructors. (Copy constructors are often preferred over serialization.) Memory Allocation and Garbage Collection of Reference TypesWhen a new object is created, memory is allocated. When there are no references to an object, the memory that object used can be reclaimed during the garbage collection process. For more information on this topic, see Chapter 11. Which checks if 2 references point to the same object?Using == on two objects checks if both operands point to the same object in memory. Save this answer. Show activity on this post. Use == to compare if two object references point to the same object.
Which of the following is used to check if the two object references?== operator is used to check whether two variables reference objects with the same value.
How to check if two objects have same reference in JavaScript?var thesame = obj1===obj2; From the MDN : If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
How to compare two object references in Java?The == operator can be used to check if two object references point to the same object. To be able to compare two java objects of the same class the boolean equals(Object obj) method must be overridden and implemented by the class. The implementer decides which values must be equal to consider two objects to be equal.
|