Java Reference
In-Depth Information
Figure 8
The Object Class Is the Superclass of Every Java Class
465
466
10.8.1 Overriding the toString Method
The toString method returns a string representation for each object. It is used
for debugging. For example,
Rectangle box = new Rectangle(5, 10, 20, 30);
String s = box.toString();
// Sets s to
Ðjava.awt.Rectangle[x=5,y=10,width=20,height=30]Ñ
Define the toString method to yield a string that describes the object state.
In fact, this toString method is called whenever you concatenate a string with an
object. Consider the concatenation
Ðbox=Ñ + box;
On one side of the + concatenation operator is a string, but on the other side is an
object reference. The Java compiler automatically invokes the toString method
Search WWH ::




Custom Search