Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,028 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.