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().

7. The toString() Method


Yes, even though the phrase is a little bit deceptive. One should really say
"the object currently referenced by a," or "the object referred to by a," or "the object a points to,"
but usually people say "the object a" and expect you to know what they mean.

The toString() Method

The example program does very little that is visible on the monitor screen. It would be nice to print something out. The bytes that make up an object cannot be sent directly to the monitor because they are not ASCII character data. For example, the two ints of a Point use a data type that can't be sent directly to a monitor. Of course, the methods of an object are in Java bytecode, which makes no sense to a monitor, either.

Luckily, there is a method defined in class Point that can be used to create a printable String for a Point object. Point, class

The toString() method of a Point object creates a String object, which can then be printed. Look at the documentation:

  public String toString();   // returns character data that can be printed
      |      |        |
      |      |        +--- this is the method name.  It takes no parameters.
      |      |
      |      +--- this says that the method returns a String object
      +--- anywhere you have a Point object, you can use this method

All objects have their own toString() method, so it is possible to print out something for any object your program has created. (Often, though, the String is not very useful.)

Question 7:

(Review: ) What is a parameter?