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 Basics Perfecting the Prototype Censoring Words - Using String Equality

AFIR MOURAD TAHAR
AFIR MOURAD TAHAR
3,766 Points

String.equals method

I just wanted to ask what is the difference between

<string> = "whatever string" and <string>.equals("whatever string");

Why in Java the equal operator is constructed as a method.?

1 Answer

AFIR MOURAD TAHAR : In Java, when using the == operator, both sides of the operator are checked for being a reference to the same object. Only for primitive types, the value is compared directly. In any class, however, the .equals() method from the Object root class can be overwritten by the developer do provide a way to compare 2 objects. You can then decide when 2 objects are considered equal in your application. Look at the Java docs for Object.equals() for more on how to do this, following the convention.