Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By jim829

Thread: When is toString() called

  1. #1
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default When is toString() called

    In this code below there is a toString method but there is no call to the toString() method, but still the control goes to the toString() method. How?

    Java Code:
    class ExceptionDemo {
    	static void compute(int a) throws MyException
    	{
    		System.out.println("Called compute["+a+"]");
    		if(a>10)
    			throw new MyException(a);
    		System.out.println("Normal Exit");
    	}
    
    	public static void main(String args[])
    	{
    		try
    		{
    			compute(1);
    			compute(20);
    			compute(30);
    		}
    		catch(MyException e)
    		{
    			System.out.println("Caught "+e);
    		}
    		System.out.println("This shhould be printed");
    	}
    }
    class MyException extends Exception
    {
    	private int detail;
    	MyException(int a)
    	{
    //		System.out.println("Inside constructor");
    		detail=a;
    	}
    	public String toString()
    	{
    		System.out.println("Inside toString");
    		return "MyException["+detail+"]";
    	}
    }
    Output:
    Called compute[1]
    Normal Exit
    Called compute[20]
    Inside toString
    Caught MyException[20]
    This shhould be printed

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: When is toString() called

    ??? The toString is called ? --> System.out.println("Caught "+e) it`s the same like System.out.println("Caught "+e.toString()); -> Caught MyException[20]

  3. #3
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: When is toString() called

    The toString() method is called before Caught MyException[20]. Please see the output.

  4. #4
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: When is toString() called

    toString method is always called when you need string representation of that object e.g in
    System.out.println("Caught "+e);
    oviosly yon cannot add object to string, so toString of "e" is called and concatenated with "Caught"

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: When is toString() called

    Quote Originally Posted by allaudin View Post
    toString method is always called when you need string representation of that object
    Wrong. The compiler can't read your mind to know what you need.

    oviosly yon cannot add object to string, so toString of "e" is called and concatenated with "Caught"
    Read the correct decription here: Chapter*15.*Expressions

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: When is toString() called

    Quote Originally Posted by DarrylBurke View Post
    Wrong. The compiler can't read your mind to know what you need.


    Read the correct decription here: Chapter*15.*Expressions

    db
    both the statements are right ok .. compiler know when to call toString ..

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: When is toString() called

    Quote Originally Posted by allaudin View Post
    both the statements are right ok .. compiler know when to call toString ..
    The compiler knows nothing about it nor do you. It's better to keep your mouth shut instead of ruining so many threads. You should study more instead of spewing your nonsense all over these forums.

    Jo
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: When is toString() called

    Quote Originally Posted by JosAH View Post
    The compiler knows nothing about it nor do you. It's better to keep your mouth shut instead of ruining so many threads. You should study more instead of spewing your nonsense all over these forums.

    Jo
    Same advice for you ...

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,590
    Rep Power
    5

    Default Re: When is toString() called

    Quote Originally Posted by allaudin View Post
    Same advice for you ...
    Your advice has incredibly little value, so don't expect a result.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default Re: When is toString() called

    Quote Originally Posted by allaudin View Post
    Same advice for you ...
    You are not in any position to give me any advice; at least I know the Java language from the inside out, as well as many other languages. You, on the other hand, are just a 'sheer beginner' (something you accuse other people of), you can hardly program yourself out of a wet paper bag and you should stop spreading nonsense all over these forums; your 'advice' is misleading, vague or just dead wrong. I have warned you many times now and the next time I'll ban you immediately without any warning; you're nothing but a nuisance and you've ruined enough threads.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,329
    Rep Power
    5

    Default Re: When is toString() called

    The compiler can't know what your intentions are or what you need.

    Java Code:
    Foo foo = new Foo();
    String x = foo; // compiler error!    Do I want foo.toString() or should x have been declared type Foo?
    
    // What about here?
    Integer val = 1;
    String x = val + 1; // Compiler error!  Do I want Integer or int x or do I want val.toString() + 1?
    Regards,
    Jim
    gimbal2 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. How to display toString() inside another toString()
    By SVTermiCobra in forum New To Java
    Replies: 1
    Last Post: 08-08-2012, 01:49 AM
  2. toString
    By Waarten in forum New To Java
    Replies: 11
    Last Post: 01-25-2012, 03:02 PM
  3. toString
    By justin23 in forum New To Java
    Replies: 13
    Last Post: 05-02-2010, 02:44 PM
  4. toString
    By luckyleaf95 in forum New To Java
    Replies: 9
    Last Post: 02-11-2010, 08:52 AM
  5. Can i just use toString?
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •