For Programmers: Free Programming Magazines  


Home > Archive > Java Help > May 2004 > Re: toString should _return_ a string, why does it print it?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: toString should _return_ a string, why does it print it?
Woebegone

2004-05-04, 4:04 pm


"javac" <javac@mail.com> wrote in message
news:64d923a9.0405031345.207f47bb@posting.google.com...
> class VectorOfLifeForms toString method:
> public String toString() {
> System.out.println("..toString..");
> for(int i=0; i<MAX_LIFE; i++) {
> lifeForm = (LifeForm)VECTOR_OF_LIFE_FORMS.get(i);
> String lifeFormString = lifeForm.toString();
> }//for
> return "";
> }//toString
>
> class Lifeform toString method:
> public String toString() {
> System.out.println("..LifeForm toString..");
> return "x\t\t" + location.getX() + "\ny\t\t" + location.getY()
> + "\nz\t\t" + location.getZ();
> }//toString
>
> here's my question: why does the VectorOfLifeForms even print
> anything out?
>
> in the main method:
> VECTOR_OF_LIFE_FORMS.toString();
>
>
> thanks,
>
> javac@mail.com
> source code: http://www.geocities.com/cjavacjava/
> (now with actual links!)


What does it print? I'd guess from looking that it prints the line
"..toString.."; this is what you might call a "side effect" of the function
call: it returns the empty String after all that work, but the first
statement in the method is a console print statement. What do you expect?


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com