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 trialmahesh gurbaxani
1,420 Pointsalternative way of expressing "equal"
Instead of writing "noun.equals("dork")", can we write noun=="dork"
3 Answers
Allan Clark
10,810 Points== should only be used to compare primitive types (i.e. int, char, float etc). The reason being if it is used on Objects like Strings or your defined classes, Java will only return True if the two Objects are identical. This includes where they are stored in memory. So it will only return true if you are comparing an Object to itself.
//example Strings
String noun = "Code Monkey";
String anotherNoun = "Code Monkey";
//this returns true
noun == noun;
//these return false
noun == anotherNoun;
noun == "Code Monkey";
*Side note the single = is the assignment operator and does not do comparison
mahesh gurbaxani
1,420 PointsThanks Allan
Craig Dennis
Treehouse TeacherYou should check out my workshop The Thing About Strings.
cecil merrell
6,873 Pointscecil merrell
6,873 Pointsjust one = sign with spaces, like this
jian.name = "Jian";