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 String Equality

Alexander Nowak
Alexander Nowak
498 Points

"if" statements

Could someone please tell me where I'm going wrong this the question?

Thank you!

Equality.java
String firstExample = ("firstExample");
String secoundExample = ("secoundExample');
                         if firstExample = secoundExample {
  console.printf("first is equal to second");
                         }

1 Answer

Hi Alexander,

Your starting position is this - that's what the challenge gives you:

// I have imported a java.io.Console for you, it is named console. 
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";

The question is Add an if statement that checks to see if firstExample is equal to secondExample. If it is, print out "first is equal to second"

We're asked to compare to strings to see if they are equal. If they are, print something out, else do nothing. Every String has an equals() method that allows you to test if one string is the same as another. We want to see if firstExample equals secondExample. That looks like:

if(firstExample.equals(secondExample)){
  console.printf("first is equal to second");
}

I hope that helps!

Steve.