aspose file tools*
The moose likes Beginning Java and the fly likes looking at some code ('this', and a method) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "looking at some code ( Watch "looking at some code ( New topic
Author

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

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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);
}
}


giddee up
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
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:


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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

Joined: Jul 11, 2001
Posts: 14112
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:


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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

Joined: Jul 11, 2001
Posts: 14112
You're welcome!
Eric Fletcher
Ranch Hand

Joined: Oct 26, 2000
Posts: 188
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


My theory of evolution is that Darwin was adopted. - Steven Wright
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: looking at some code ('this', and a method)
 
Similar Threads
Inheritance Query
private inner class access
static variable and Serialization.
creating object using newInstance() method