Page 549 - Introduction to Programming with Java: A Problem Solving Approach
P. 549
the calculator window, click the Hex button, enter 601BB1, and then click the Dec button. The calculator displays 6298545, which is the decimal number equivalent to 601BB1. Thus, in the previous code fragment, when obj.toString() returns a string with 601BB1 at the right of the @ sign, it means the obj object’s location in memory can be found by going to the 6,298,545th position in the object hash table. The object hash table is the entity in change of translating hashcode values into actual locations in memory.
Overriding the toString Method
Retrieving the class name, an @ sign, and a hashcode is usually worthless, so you’ll almost always want to avoid calling the Object class’s toString method and instead call an overriding toString method. The reason we’re discussing the Object class’s toString method is because it’s easy to call it acciden- tally, and when that happens, we want you to understand what’s going on.
Since the Object class defines a toString method, every class has a toString method, even if it does not define one or inherit one through some other class it explicitly extends. Many Java API classes define overriding toString methods. For example, the String class’s toString method trivi- ally returns the string that’s stored in the String object. As described in Chapter 10, the ArrayList class’s toString method (inherited from the AbstractCollection class) returns a square-bracketed comma-delimited list of strings that represent the individual array elements. The Date class’s toString method returns a Date object’s month, day, year, hour, and second values as a single concatenated string. In general, toString methods should return a string that describes the calling object’s contents.
Since retrieving the contents of an object is such a common need, you should get in the habit of pro-
viding an explicit toString method for most of your programmer-defined classes. Typically, your Apago PDF Enhancer
toString methods should simply concatenate the calling object’s stored data and return the resulting string. Your toString methods should not print the concatenated string value; they should just return it. We’re mentioning this point because novice programmers have a tendency to put print statements in their toString methods, and that’s wrong. A method should do only what it’s supposed to do and nothing more. The toString method is supposed to return a string value, and that’s it!
For example, look at the toString method in the Car2 program in Figure 13.2. It returns a string that describes the calling object’s contents.
Implicit toString Method Calls
In the Car2 program, the main method has no explicit toString method call. So how does this pro- gram illustrate use of the toString method? Whenever a reference appears alone inside a print state- ment (System.out.print or system.out.println), the JVM automatically calls the referenced object’s toString method. In Figure 13.2, this statement generates a call to the toString method in the Car2 class:
System.out.println(car);
Let’s look at another example that uses the toString method. See the Counter program in Figure 13.3. Once again, there’s a toString method and no explicit call to it. So how does it get called? When you concatenate a reference variable and a string (with the operator), the JVM automatically calls the reference’s toString method. Thus, in Figure 13.3, this statement’s counter reference generates a call to the Counter class’s toString method:
String message = "Current count = " + counter;
13.4 The toString Method 515