This discussion is archived
1 2 3 Previous Next 31 Replies Latest reply: Aug 8, 2010 1:30 AM by 3004 RSS

Which toString() method does System.out.println() call?

843789 Newbie
Currently Being Moderated
Which toString() method does System.out.println() call? I know that if I did something like System.out.println( new myClass() ) that the toString being called would be that of the myClass class.

My initial thought about this was that System.out.println() is the equivalent of System.out.println( new Object() ) but I remember trying that in a test program and it gave me a myClass@some_hash_code type of message when adding the new Object() part within the System.out.println().

Could someone please tell me which toString the System.out.println() statement calls? Also, it would be great if an elaboration follows :).

Any input woudl be greatly appreciated!
Thanks in advance!
  • 1. Re: Which toString() method does System.out.println() call?
    843789 Newbie
    Currently Being Moderated
    s3a wrote:
    Which toString() method does System.out.println() call? I know that if I did something like System.out.println( new myClass() ) that the toString being called would be that of the myClass class.
    Yes.


    >
    My initial thought about this was that System.out.println() is the equivalent of System.out.println( new Object() )
    I don't understand why you would think that.
  • 2. Re: Which toString() method does System.out.println() call?
    3004 Newbie
    Currently Being Moderated
    When you downloaded your JDK, it came with src.zip, where you can find the following.

    Given that System.out is a PrintStream, in PrintStream.java, we see:
        public void println(Object x) {
            String s = String.valueOf(x);
            synchronized (this) {
                print(s);
                newLine();
            }
        }
    which leads us to String.java
        public static String valueOf(Object obj) {
         return (obj == null) ? "null" : obj.toString();
        }
    And there we can see out class's toString() being called.
  • 3. Re: Which toString() method does System.out.println() call?
    843789 Newbie
    Currently Being Moderated
    cotton.m:* Because System.out.println( new myClass() ) would run the toString of the myClass class so why shouldn't System.out.println( new Object() ) run the toString of the Object class?

    jverd:* I don't get your explanation because there are somethings I have not covered such as "synchronized" and I'm pretty confused about System.out being a PrintStream.

    All I know, for example, is that, I can do:
    PrintWriter writer = new PrintWriter(new File("outputFile.txt");
    writer.println();
  • 4. Re: Which toString() method does System.out.println() call?
    843789 Newbie
    Currently Being Moderated
    s3a wrote:
    My initial thought about this was that System.out.println() is the equivalent of System.out.println( new Object() )
    Huh?

    In one method call you pass no parameters in the other method call you pass one parameter. How could you possibly think that they would do the same thing?
    Could someone please tell me which toString the System.out.println() statement calls? Also, it would be great if an elaboration follows :).
    Yes. The toString of the class of the object which you pass to the method. If that class does not have a toString method it uses the toString method it inherits from the parent class. If the parent class does not have a toString method it uses the one inherited and so and so on all the way up to the Object class.
  • 5. Re: Which toString() method does System.out.println() call?
    843789 Newbie
    Currently Being Moderated
    s3a wrote:
    jverd:* I don't get your explanation because there are somethings I have not covered such as "synchronized" and I'm pretty confused about System.out being a PrintStream.
    Who mentioned synchronized?

    If you go the the API and look at the System class you will see that out is declared as a static member of type PrintStream.
  • 6. Re: Which toString() method does System.out.println() call?
    843789 Newbie
    Currently Being Moderated
    flounder wrote:
    s3a wrote:
    jverd:* I don't get your explanation because there are somethings I have not covered such as "synchronized" and I'm pretty confused about System.out being a PrintStream.
    Who mentioned synchronized?
    Nobody but it's in the posted source.
  • 7. Re: Which toString() method does System.out.println() call?
    843789 Newbie
    Currently Being Moderated
    s3a wrote:
    cotton.m:* Because System.out.println( new myClass() ) would run the toString of the myClass class so why shouldn't System.out.println( new Object() ) run the toString of the Object class?
    It does but it wasn't clear (to me at least) that that was your question in the first post.
  • 8. Re: Which toString() method does System.out.println() call?
    EJP Guru
    Currently Being Moderated
    My initial thought about this was that System.out.println() is the equivalent of System.out.println( new Object() ) but I remember trying that in a test program and it gave me a myClass@some_hash_code type of message when adding the new Object() part within the System.out.println().
    No it didn't.
  • 9. Re: Which toString() method does System.out.println() call?
    843789 Newbie
    Currently Being Moderated
    cotton.m wrote:
    flounder wrote:
    s3a wrote:
    jverd:* I don't get your explanation because there are somethings I have not covered such as "synchronized" and I'm pretty confused about System.out being a PrintStream.
    Who mentioned synchronized?
    Nobody but it's in the posted source.
    Yeah, it was interesting that this is what OP concentrated on rather than jverd's explanation that println calls valueOf and valueOf calls toString.
  • 10. Re: Which toString() method does System.out.println() call?
    3004 Newbie
    Currently Being Moderated
    s3a wrote:
    jverd:* I don't get your explanation because there are somethings I have not covered such as "synchronized" and I'm pretty confused about System.out being a PrintStream.
    Where did you get synchronized from? I didn't say anything about that. It might have appeared in one of the code snippets I posted, but it's totally irrelevant to your original question. My point was, you can look at the source code and see what's getting called.
    All I know, for example, is that, I can do:
    PrintWriter writer = new PrintWriter(new File("outputFile.txt");
    writer.println();
    Okay, and? What confuses you about that?
  • 11. Re: Which toString() method does System.out.println() call?
    Tolls Journeyer
    Currently Being Moderated
    I think the OP thinks that a call to println() with no parameters has to result in some sort of call to a toString() somewhere, hence all the stuff about it being the equivalent to println(new Object()).

    I could well be wrong...

    Anyway, if that's the case, then all println() does is call newLine().
  • 12. Re: Which toString() method does System.out.println() call?
    843789 Newbie
    Currently Being Moderated
    The only way I can see this question arising at all is as a result of thinking that calling code can decide which implementation of a method to invoke on another object. It can't, obviously.
  • 13. Re: Which toString() method does System.out.println() call?
    843789 Newbie
    Currently Being Moderated
    The reason why I think the System.out.println() and System.out.println(new Object()) statements are the same is because in my mind I view the Object class' toString() method's scope as something like return "\n" for both cases. If someone could give me the code for the Object class' constructors, it would probably help more than reading English words.
  • 14. This Thread is now moved
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Note: This thread was originally posted in the [Java Programming|http://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment.
1 2 3 Previous Next