On CBS.com: Six show girls attacked

Print useful object information by overriding toString()

Tags: Guest Contributor

  • Save
  • Print
  • 14

Takeaway: This week in Java Q&A, the Builder.com technical team fields a member question about customizing the output generated by passing an object to the System.out.println() method.


All developers need help from time to time. And when it comes Java, who better to offer that help than the experienced developers at Builder? This column features your Java technical questions and offers recommended solutions from the Builder technical staff.

Q: How can I change what information gets printed out when passing an instance of my class to the System.out.println(Object) method?

A: All objects that you define are descendants of the base class Object. The toString() method is one of the methods that’s part of the base class Object. This method can be overridden to allow for meaningful information to printed when passing an object to the System.out.println() method. The Sun Java 1.2.2 API documentation offers this information:

“Returns a string representation of the object. In general, the toString method returns a string that ’textually represents’ this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.”

By default, the toString() method will return the following value:

 
getClass().getName() + '@' + Integer.toHexString(hashCode())

 

For instance, let’s say we had the following simple class Foo defined:

 
import java.io.*;
public class Foo {
  private int size = 90;
  private String id = "foobar";
 
public static void main(String[] args) throws IOException {
     Foo tmpFoo = new Foo();
     System.out.println("Foo-> " + tmpFoo);
  }
}

 

If we compiled and ran this code, it would produce something similar to the following:

 
Foo-> Foo@66fee51b

 

Now, add the toString(), which returns a String type. This method (Object.toString()) is invoked (indirectly through the String.valueOf(Object)) when passed to the System.out.println() method.

 
public String toString() {
     return(id+”{“+size+”}”);
  }

 

The output would look like the following:

 
Foo-> foobar{90}

 

As you can see, by adding the method toString() to your class, you can customize (any way you like) what gets output when passing the object to the System.out.println() method.
  • Save
  • Print
  • 14

What do you think?

Article Categories

Security
Security Solutions, IT Locksmith
Networking and Communications
E-mail Administration NetNote, Cisco Routers and Switches
CIO and IT Management
Project Management, CIO Issues, Strategies that Scale
Desktops, Laptops & OS
Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
Data Management
Oracle, SQL Server
Servers
Windows NT, Linux NetNote, Windows Server 2003
Career Development
Geek Trivia
Software/Web Development
Web Development Zone, Visual Basic, .NET

The PC Cracked Open

advertisement
Click Here