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 trialAFIR MOURAD TAHAR
3,766 PointsString.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
joppegeluykens
4,393 PointsAFIR 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.