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 trial

Android

Jordan Ernst
Jordan Ernst
5,121 Points

+ "" empty string

using this empty string is the same as calling the toString() method correct?

1 Answer

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Jordan,

If you want to represent any object as a string, toString() method comes into existence. The toString() method returns the string representation of an object.

I am not sure whether concatenation of strings using the + operator is the same as using the toString() method but I found this example:

If you create a "name" String variable and concatenate your first and last name the compiler makes crazy things for you:

The Java compiler transforms this line of code:

String name = "Jordan" + " Ernst";

into this:

String s=(new StringBuilder()).append("Jordan").append(" Ernst).toString();  

So String concatenation is implemented through the StringBuilder (or StringBuffer) class and its append method. In contrast to this you can override the toString method and it will do whatever you want.

In my opinion concatenation and toString are not the same.

Grigorij

Jordan Ernst
Jordan Ernst
5,121 Points

very in depth as always friend thanks

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey BRO,

so glad I could help :)))))

I hope you are doing great !

See you