• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why the ouput is like this ?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class C {
public static void main(String arg[]) {
System.out.println("A"+new C());
}
public String toString() {
System.out.print("B");
return "C";
}
}
The output is :BAC
can you tell me why?
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. First JVM has to compute "A"+new C(). At this point nothing is printed yet, computation is going in memory.
2. new C() calls toString() method of C class
3. Within toString() method "B" is printed (it is the first thing that is getting printed!!!)
4. toString() returns "C". "A"+"C" is computed
5. First println is executed, printing "AC".
Shura
 
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
even the main is not explicitly call toString(), the "B" can still but printed out?
Thanks!
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because of the concatenation of the new C object with the String "A", the C object will automatically have the toString() method called on it in order to convert it to a String format so that it can be output.
Corey
 
andy lau
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do you mean that no matter when and where i creat an object using the ''new'' keyword will cause the toString() method of that object to be executed automaticly?
thanks!
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by andy lau:
[QB]do you mean that no matter when and where i creat an object using the ''new'' keyword will cause the toString() method of that object to be executed automaticly?
[QB]


No! The toString() method is not called when the object is created, but any time the object needs to be displayed as a String. The purpose of the "toString" method is to display the object in a String.
Corey
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic