Object
class sits at the top of the class hierarchy tree in the Java
platform. Every class in the Java system is a descendent, direct or indirect, of
the Object
class. This class defines the basic state and behavior
that all objects must have, such as the ability to compare oneself to another
object, to convert to a string, to wait on a condition variable, to notify other
objects that a condition variable has changed, and to return the class of the
object.
Your classes may want to override the following Object
methods.
The equals
/hashCode
are listed together as they must
be overridden together.
clone
equals
/hashCode
finalize
toString
Object
methods (they are final):
getClass
notify
notifyAll
wait
Coord
that
represent a coordinate. Here is the class
public class Coord { public int x; public int y; Coord(int x, int y) { this.x = x; this.y = y; } }
toString
MethodObject
's
toString
method returns a String
representation of the
object. You can use toString
along with
System.out.println
to display a text representation of an object,
such as
or we can even useSystem.out.println(new Dimension(5,7).toString);
TheSystem.out.println(new Dimension(5,7));
String
representation for an object depends
entirely on the object. The String
representation of an
Integer
object is the integer value displayed as text. The
String
representation of a Dimension
object contains
various members such as width and height. For example, the previous lines of
code displays the following output:
Thejava.awt.Dimension[width=5, height=7]
toString
method is very useful for
debugging. It behooves you to override this method in all your classes. System.out.println(new Coord(1,2));
Coord
like this
public class Coord { public int x; public int y; Coord(int x, int y) { this.x = x; this.y = y; } public String toString() { return "("+ x + "," + y + ")"; } }
equals
and hashCode
Methodsequals
and hashCode
methods together. (We
won't talk about hashCode
, it is used in hash table). The equals
method compares two objects for equality and returns
true
if they are equal. The equals
method provided in
the Object
class uses the identity function to determine if objects
are equal (if the objects compared are the exact same object the method returns
true
).
However, for some classes, two distinct objects of that type might be considered equal if they contain the same information.
givesCoord c1 = new Coord(5,6); Coord c2 = new Coord(5,6); System.out.println(c1.equals(c2));
false
. A better program ispublic class Coord { public int x; public int y; public Coord(int x, int y) { this.x = x; this.y = y; } public boolean equals(Coord c) { return x == c.x && y == c.y; } public String toString() { return "("+ x + "," + y + ")"; } }
public class Coord { public int x; public int y; public Coord(int x, int y) { this.x = x; this.y = y; } public boolean equals(Object obj) { if(obj instanceof Coord) { // if obj is of type Coord Coord c = (Coord)obj; // type casting return x == c.x && y == c.y; } else { // obj is of different type return false; } } public String toString() { return "("+ x + "," + y + ")"; } }
finalize
MethodObject
class provides
a method, finalize
, that cleans up an object before it is garbage
collected. The finalize
method is called automatically by the
system and most classes you write do not need to override it. So you can
generally ignore this method.
getClass
MethodgetClass
method is a
final method that returns a runtime representation of the class of an object.
This method returns a Class
object.
Once you have a Class
object you can query it for various
information about the class, such as its name, its superclass, and the names of
the interfaces that it implements. The following method gets and displays the
class name of an object:
void PrintClassName(Object obj) { System.out.println("The Object's class is " + obj.getClass().getName()); }