Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialChristion Gino
220 PointsHow does Treet.java automatically calls toString() method?
Okay. For example if we want to get the author of a tweet we would call treet.getAuthor(); in the main method, but why do we don't need to call treet.toString(); in the main method? It just automatically makes it to cast it?
I understand that the toString(); method is written somewhere in the Object class.
What confuses me the most is that the objects have the same structure, but behave totally different. Sorry for my stupid question, it confuses me.
2 Answers
Christion Gino
220 PointsThanks for your answer! Though I didn't understood it completely.
https://www.youtube.com/watch?v=l0N6WvIVoUI&list=PLFE2CE09D83EE3E28&index=4
This tutorial explains this one perfectly though.
Fahad Mutair
10,359 Pointshi Christion Gino , i found this answer on Stack Overflow
You're not explicitly calling toString(), but impliclty you are:
See:
System.out.println(foo); // foo is a non primitive variable
System is a class, with a static field out, of type PrintStream. So you're calling the println(Object) method of a PrintStream.
It is implemented like this:
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
As wee see, it's calling the String.valueOf(Object) method. This is implemented as follows:
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
And here you see, that toString() is called.
michaelcodes
5,604 PointsThank you for this answer! this perfectly explains it. I was wondering the same thing after watching this video, since with every other method its necessary to call the method in order to use it. I was confused as to why the toString method is automatically implemented without calling it...
This explanation makes perfect sense though that the .toString method IS being called, but it is behind the scenes happening through a series of other methods that we don't see.
muhammad faizullah
Courses Plus Student 8,122 PointsI'm sorry if I ask a stupid question, but what does the question mark (?) mean in the above code?
Gabbie Metheny
33,778 PointsMuhammad, the question mark (?
) and the colon (:
) are both part of the ternary operator. It's basically shorthand of writing an if/else statement: the part before the ?
is the condition to test, the part after the ?
but before the :
is the code to run if the condition evaluates to true
, and the part after the :
is the code to run if the condition evaluates to false
. So, in the above case, if obj
is null
, "null"
will be returned; if it is not null
, obj
will be converted to a string, then returned.