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

Philip Schultz
Philip Schultz
11,437 Points

How would I check that my first vaiable doesn't equal the second variable using if statement?

I tried saying If (firstExample.equals(secondExample)){ console.printf("first is equal to secound"); } but it is saying error. (;) expected. what am I missing.

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

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

1 Answer

Colin Bell
Colin Bell
29,679 Points

The challenge is asking to check if it is equal, which is what you're checking for. Your code isn't passing because the if needs to be lowercase.

Another way to write it would be:

Nevermind. It wasn't correct

Yes I agree that the "if" needs to be lowercase, however I don't agree that "firstExample == secondExample" and "firstExample.equals(secondExample)" are equivalent when it comes to strings in Java. From what I've experienced .equals() is for object comparison where as == is for reference comparison and you can get into murky waters when using one over the other in the situations when it matters. However this even has its intricacies, as noted in the source below.

source

Colin Bell
Colin Bell
29,679 Points

You're right. While it does pass the challenge, it's not the proper way to compare string equivalency in Java.

Thanks for the correction.