[course07] 01 Object class

[course07] 01 Object class

The universal superclass

Object is a direct or indirect superclass of every other class.

toString() method

examples:

Note

  1. The + sign is a concatenation operator for strings

  2. Array objects are unusual in that they do not have a toString method. To print the elements of an array, the array must be traversed and each element must explicitly be printed.

equal() method

Do not use == to test objects for equality. Use the equals method.

Note

  1. The default implementation of equals is equivalent to the == relation for objects: In the Date example above, the test if (d1 == d2) returns trXe; the test if (d1 == d3) returns false.

  2. The operators <, >, and so on are not used for objects (reference types) in Java. To compare objects, you must use either the equals method or define a compareTo method for the class.

Last updated

Was this helpful?