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

Geovani Estacio
PLUS
Geovani Estacio
Courses Plus Student 1,875 Points

Can anyone help me with equality's, I am stuck on this one. What am i missing here.

String firstExample = "hello"; if (firstExample.equals("secondExample") ) { console.printf("first is equal to second"); System.exit(0); } String secondExample = "hello"; String thirdExample = "HELLO";

Equality.java
// I have imported a java.io.Console for you, it is named console. 
String firstExample = "hello";
if (firstExample.equals("secondExample") ) {
  console.printf("first is equal to second");
  System.exit(0);
}
String secondExample = "hello";
String thirdExample = "HELLO";

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

There's a couple of things to try here.

First of all, you're passing in a String to the equals() method rather than a reference to secondExample.

Also, try storing all your variables above anything else in your code, so the method knows what the variables are, what they contain and what to do with them. :-)

Digvijay Katoch
Digvijay Katoch
1,637 Points

Make it

firstExample.equals(secondExample)

When you place something in double quotes, it becomes a string literal and that is what gets compared. You were trying to compare "hello" to "secondExample". You should be comparing "hello" to "hello". or firstExample to secondExample.

Also, create variables above the if statement.