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 Objects (Retired) Meet Objects Refresher

How would you answer this? would you use .equals?

trying to figure out what to fill in the blank

The one I think you are struggling with would be the .contains() method

1 Answer

You need to fill contains() in the blank.

.equals() checks whether a string is exactly equal to another whereas .contains() checks whether a word or even part of it is present in the string.

Eg. for .contains()

String someWords = "I like grapes, mangoes and cherries";

Now to check whether above string contains a particular word say, 'mangoes' You will use .contains() like this:

someWords.contains("mangoes");

Eg. for .equals()

String favFruit = "mango";

Now if you want to check if the string contains exactly the word(s) you want then you use .equals()

favFruit.equals("mango");

Both .contains() and .equals() return a boolean value.