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 Organizing Data Interfaces

Treet comparison code

Can someone tell me what they believe this code means? public int compareTo(Object obj) { Treet other = (Treet) obj; // Casting to a treet so that we can access all of those values if(equals(other)) { return 0;

1 Answer

Todd Farr
Todd Farr
9,547 Points

Sandy, I've added comments to each line below

//we are overriding the compareTo() method here for use in our Class
public int compareTo(Object obj) { 

    // Since we are passing in an object we have to Typecast it to a Treet
    // We need to do this so that we can compare it to another Treet
    Treet other = (Treet) obj;  

    //Then we do a check for if this Treet equals other which has been typecasted to a Treet
    if(equals(other)) { 

        // if they are equal we return 0
        return 0;

I hope this helps and makes sense!