Page 548 - Introduction to Programming with Java: A Problem Solving Approach
P. 548

                514 Chapter 13 Inheritance and Polymorphism 13.4 The toString Method
The Object Class’s toString Method
Let’s now consider another important method that all classes inherit from the Object class. The Object class’s toString method returns a string that’s a concatenation of the calling object’s full class name, an @ sign, and a sequence of digits and letters. For example, consider this code fragment:
Object obj = new Object();
Car car = new Car();
System.out.println(obj.toString());
System.out.println(car.toString());
When executed, the code fragment produces this:
java.lang.Object@601BB1
Car@1BA34F2
full class name These digits and letters form a hashcode.
Note how obj.toString() generates java.lang.Object for the full class name. The full class name consists of the class name prefixed by the package that the class is part of. The Object class is in the java.lang package, so its full class name is java.lang.Object. Note how car.toString() generates Car for the full class name. Since the Car class is not part of a package, its full class name is simply Car.
                  Apago PDF Enhancer
Note how obj.toString() generates 601BB1 for its hashcode value. You can think of an object’s hashcode value as its location in memory, but it’s really a bit more complicated than that. The JVM trans- lates an object’s hashcode value to one or more other values and the last value in the translation chain specifies the object’s actual location in memory. In Java, hashcode values, like 601BB1, are written as hexa- decimal numbers. We described the hexadecimal number system in the optional Unicode section at the end of Chapter 11. What follows is a review.
Hexadecimal Numbers
Hexadecimal numbers use digits that can have one of sixteen values—0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F (lowercase letters a through f are also acceptable). The A through F values represent the num- bers 10 through 15. With 16 unique digits, hexadecimal numbers form what is known as a base-16 num- ber system. With the number system that you are used to, base-10 for decimal numbers, suppose you’re counting up and you get to the largest digit, 9. To form the next number, 10, you need two digits—a 1 at the left and a 0 at the right and the result is 10. Likewise, suppose you’re counting up with hexadeci- mal numbers and you get to the largest digit, F for 15. To form the next number, 16, you need two dig- its—a 1 at the left and a zero at the right and the result is 10. In other words, 10 is how you write 16 in hexadecimal. For additional help with hexadecimal counting, see Appendix 1. In it, you’ll see a sequence of hexadecimal numbers and their associated decimal numbers, in the context of the Unicode/ASCII character set.
You know that the hexadecimal number A is equivalent to the decimal number 10. What about the 601BB1 value generated by the previous code fragment—what is its equivalent decimal number? Convert- ing large hexadecimal numbers to their decimal equivalents can be done mathematically, but we’ll present a shortcut. If you’re on a Windows-based computer, select Start / Programs / Accessories / Calculator. In


















































































   546   547   548   549   550