This is the mail archive of the
frysk@sourceware.org
mailing list for the frysk project.
toPrint vs toString
- From: Andrew Cagney <cagney at redhat dot com>
- To: frysk at sourceware dot org
- Date: Tue, 14 Aug 2007 11:23:20 -0400
- Subject: toPrint vs toString
toString(): for dumping internal object state
---------------------------------------------
Just a reminder; Java's Object class includes a <<public String
toString()>> method that can be overridden (by default it prints the
object's address and type) to dump out an objects internal state. That
way, when printing or tracing this method is called vis:
System.out.println("my " + task); // uses Task.toString
logger.log(Level.FINE, "{0} task\n", task); // ditto
and debugging level information is provided. However, within frysk,
toString() or variations shouldn't be used when creating output intended
for the user.
toPrint(): for displaying an object user friendly
-------------------------------------------------
If the output is intended for the user, the add a toPrint() method and
use that. There are two variations. The first sends the output to a
Writer vis:
public void toPrint(PrintWriter writer) ... writer.print ...
and the second, if you find it necessary, just wraps the first vis:
public void toPrint()
stringWriter = new StringBuffer()
printWriter = new PrintWriter(stringWriter)
toPrint(printWriter)
return stringWriter.toString();
For instance, frysk.value.Value as methods such as:
public void toPrint(PrintWriter writer, ByteBuffer memory, Format
format)
public String toPrint() { ... see above ... }
for converting a value in to a user-readable form.