5 Replies Latest reply: Apr 21, 2013 10:42 AM by baftos RSS

    toString method why its not called implicitly for objects

    997192
      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?