public inbox for frysk@sourceware.org
 help / color / mirror / Atom feed
* toPrint vs toString
@ 2007-08-14 15:23 Andrew Cagney
  2007-08-14 15:35 ` Sami Wagiaalla
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Cagney @ 2007-08-14 15:23 UTC (permalink / raw)
  To: frysk

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.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-08-14 17:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-14 15:23 toPrint vs toString Andrew Cagney
2007-08-14 15:35 ` Sami Wagiaalla
2007-08-14 16:44   ` Kris Van Hees
2007-08-14 17:12   ` Sami Wagiaalla

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).