• 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

looking at some code ('this', and a method)

 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, here is some code below... i am wondering two things - first, the private ints a, b, and c are initialised to zero? and then the Constructor has to use 'this' to assign received args into these class variables. But i wonder why the toString method executes without being called?
class Abc {
private int a;
private int b;
private int c;
Abc(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public String toString() {
return "a = " + a + ", b = " + b + ", c = " + c;
}
}
public class OwnString {
public static void main (String args[])
{
Abc theAbc = new Abc(11, 14, 19);
System.out.println ("Here it is, the result of my very own toString :-) : " + theAbc);
}
}
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jasper Vader:
first, the private ints a, b, and c are initialised to zero?


Yup.

Originally posted by Jasper Vader:
But i wonder why the toString method executes without being called?


If you concatenate a String with some other object, like you did in your code, the jvm automatically calls the toString() on the object. It's just one of those cool things built into the language.
So when you have this:

Java automatically does the equivalent of this:
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh wow, what a trip!
yes, it is a cool thing thanks for saying that, as being new to this i would be tempted to initially think, 'this is a confusing thing about java', but no, as you say, it is one of those cool things built into the language.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Blake Minghelli:

Java automatically does the equivalent of this:


Well, actually it's a little bit more complicated; typically (depending on the compiler) it gets compiled to


It's the append method of StringBuffer which will call String.valueOf(theAbc), which checks that the string isn't null and finally calls the toString method on the object.
Well, I am not sure wether this did help at all... :roll:
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, it is interesting, and informative to know that there is a bit more going on behind the scenes. In terms of getting certification, of course i want to attain that rather 'blunt' goal, but in terms of actually coding graceful incisive well formulated comprehensive code (one day), this information is very helpful to my understanding thankyou Ilja .
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ilja's post also brings up another point to remember about writing efficient String-handling code:

Use a StringBuffer and append instead of using the concatenation operator for Strings.
This is a classic performance enhancement tip in Java. Since a StringBuffer is being used "under the hood", just use it in your code instead and save the JVM a step.
Cheers,
E
 
reply
    Bookmark Topic Watch Topic
  • New Topic