Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

toString method why its not called implicitly for objects

997192Apr 20 2013 — edited Apr 21 2013
class Building { }
public class Example
{
public static void main()
{
Building b = new Building();
String s = (String)b;
}
}

in the 5th Line it shows error that cannot cast building type to string..
but all classes have the toString method which is called whenever we give the object arguement in System.out.println();

the following code compiles

class Building { }
class Example
{
public static void main()
{
Building b = new Building();
System.out.println(b);
}
}
can anyone explain this?
This post has been answered by EJP on Apr 21 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 19 2013
Added on Apr 20 2013
5 comments
728 views