Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • 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);
}
}
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic