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

8. Program that uses the toString() Method


parameter is an item of data supplied to a method or a constructor.

Program that uses the toString() Method

import java.awt.*;
class PointEg2
  public static void main ( String arg[] )
    Point a, b, c;              // reference variables

    a = new Point();            // create a Point at ( 0,  0); 
                                // save the reference in "a"
    b = new Point( 12, 45 );    // create a Point at (12, 45); 
                                // save the reference in "b"
    c = new Point( b );         // create a Point

    String strA = a.toString(); // create a String object based on the data
                                // found in the object referenced by "a".
    System.out.println( strA );

toString() needs no parameters. However, use empty parentheses when the method is called. The example program shows this.

When this program runs, the statement:

String strA = a.toString(); 

creates a String object based on the data in the object referred to by a. The strA refers to this new String object. Then the characters from the String are sent to the monitor with println.

The program prints out:


The Point object has not been altered: it still exists and is referred to by a.

Question 8:

Just as the program is about to end, how many objects have been created? Has any garbage been created?