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

Java Java Data Structures - Retired Organizing Data Interfaces

Here when we wrote methods like toString() or compareTo() in our Treet.java class but never called

Every time when we create a method we are calling them like t.toString() or t.compareTo() but here we are not doing that but, the result is generated, can you please explain.

1 Answer

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Hi Sai,

the compareTo() method gets called internally when the Arrays.sort(treets) method gets called. The toString() method get's called internally when the System.out.println(exampleTreet) method get's called.

Because the Treet class implements the comparable interface the Arrays.sort() method knows that the Treet has a compareTo() method which it can call. Because every object in Java has a toString() method, the System.out.println() method is able to call it.

Kind Regards, Florian