More about Objects and Classes
The relational operations on primitive data are ==, >=, <=, >, <, and !=. They compare two data values, when those values' type has an ordering. For example, integers are ordered by size or magnitude. The result of a relational operation is a boolean value: either True or False. The relational operators on objects like Strings are different, and they are expressed as methods. Pay special attention to the equality method, equals().
10. It expects a reference to a String as a parameter.
Just as the program is about to close, how many objects have been created? Four, counting the String object.
Has any garbage been created? No, because a reference variable points to each object.
Automatic Call of
It looks like the following will not work:
Point a = new Point(); // a is a Point reference System.out.println( a ); | +--- should be String reference
However, it does work, for the following reason:
When a parameter should be a
Stringreference, but is a reference to another type of object, Java calls the object's
toString()method to create a
Stringand then uses the resulting
All objects (of any type at all) have their own
toString() method, so this trick works with any object.
(Puzzle: ) Does a
Stringobject have a