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

Gareth Gray
Gareth Gray
3,336 Points

Overriding equals() question.

Wondering if anyone can explain these lines.

if (mArtist != null ? !mArtist.equals(song.mArtist) : song.mArtist != null) return false;
if (mTitle != null ? !mTitle.equals(song.mTitle) : song.mTitle != null) return false;

I understand how ?: works as if-then-else but I'm a bit muddled with what's going on here and why it's needed. It's the not-equals operand that is throwing me I think...

Gareth Gray
Gareth Gray
3,336 Points

Okay I think I've got it! It's the ternary within an 'if statement' that got me.

Basically it's checking if mArtist is null first. If it is null and song.mArtist isn't, return false (they aren't equal) If mArtist isn't null, it checks !mArtist.equals(song.mArtist) and returns false if true...

That was mind bending....!