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 - Looping Until the Value Passes

mahesh gurbaxani
mahesh gurbaxani
1,420 Points

alternative way of expressing "equal"

Instead of writing "noun.equals("dork")", can we write noun=="dork"

cecil merrell
cecil merrell
6,873 Points

just one = sign with spaces, like this

jian.name = "Jian";

3 Answers

Allan Clark
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