* 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
* Re: toPrint vs toString
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
0 siblings, 2 replies; 4+ messages in thread
From: Sami Wagiaalla @ 2007-08-14 15:35 UTC (permalink / raw)
To: Andrew Cagney; +Cc: frysk
> public void toPrint()
> stringWriter = new StringBuffer()
> printWriter = new PrintWriter(stringWriter)
> toPrint(printWriter)
> return stringWriter.toString();
this should be stringWriter.getBuffer().toString();
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: toPrint vs toString
2007-08-14 15:35 ` Sami Wagiaalla
@ 2007-08-14 16:44 ` Kris Van Hees
2007-08-14 17:12 ` Sami Wagiaalla
1 sibling, 0 replies; 4+ messages in thread
From: Kris Van Hees @ 2007-08-14 16:44 UTC (permalink / raw)
To: Sami Wagiaalla; +Cc: Andrew Cagney, frysk
On Tue, Aug 14, 2007 at 11:35:31AM -0400, Sami Wagiaalla wrote:
>
>> public void toPrint()
>> stringWriter = new StringBuffer()
>> printWriter = new PrintWriter(stringWriter)
>> toPrint(printWriter)
>> return stringWriter.toString();
> this should be stringWriter.getBuffer().toString();
Hm, more like:
public String toPrint() {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
toPrint(printWriter);
return stringWriter.toString();
}
because:
1) You cannot pass a StringBuffer to PrinterWriter's contructor
2) StringBuffer doesn't have a getBuffer() method
3) StringWriter has a toString() method
4) You can't return a String from a method that declares its return
type as 'void' obviously
Either way perhaps it is safer in cases like this to point to existing code
(that is known to compile, etc) as an example on how to use specific
constructs?
Cheers,
Kris
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: toPrint vs toString
2007-08-14 15:35 ` Sami Wagiaalla
2007-08-14 16:44 ` Kris Van Hees
@ 2007-08-14 17:12 ` Sami Wagiaalla
1 sibling, 0 replies; 4+ messages in thread
From: Sami Wagiaalla @ 2007-08-14 17:12 UTC (permalink / raw)
To: Andrew Cagney; +Cc: frysk
Sami Wagiaalla wrote:
>
>> public void toPrint()
>> stringWriter = new StringBuffer()
>> printWriter = new PrintWriter(stringWriter)
>> toPrint(printWriter)
>> return stringWriter.toString();
> this should be stringWriter.getBuffer().toString();
>
nm... I thought I was saving people from a trap I fell into but it looks
like it works.
my bad.
Sami
^ 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).