previous |
start |
next
Overriding the toString Method
- Returns a string representation of the object
- Useful for debugging
- Example: Rectangle.toString returns something like
java.awt.Rectangle[x=5,y=10,width=20,height=30]
- toString used by concatenation operator
- aString + anObject
means
aString + anObject.toString()
- Object.toString prints class name and object
address
BankAccount@d2460bf
- Override toString:
public class BankAccount
{
public String toString()
{
return "BankAccount[balance=" + balance +
"]";
}
. . .
}
previous |
start |
next