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

Lee B
Lee B
1,141 Points

Java string equals string in if statement

Hey gang, I'm getting a very strange error, or lack of error rather, in a bit of code that seemingly looks fine.

The goal is to print "first is equal to second" if firstExample is the same thing as secondExample.

String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";

// this is what the lesson uses and probably is wanting us to use for this, and fails the check, without printing anything into the "output" tab
if (firstExample.equals(secondExample)) {
  console.printf("first is equal to second");
  System.exit(0);
}

// also does not work, === or ==
if (firstExample == secondExample) {
}

Help would be greatly appreciated. Thank y'all!

2 Answers

Hi How are you doing?

lets solve this with 2 steps shall we :]? :

  1. first u are asked to check if firstExample is equals to secondExample, u did it fine, but u wasnt asked to break out of the prorgram usuing system.exit(0).

2.ur asked to use the method equalsIgnoreCase which checks for equality and ignoring the case, you couldn't do it with ==.

here is the complete code:

// 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 second");
}
if(firstExample.equalsIgnoreCase(thirdExample)) {
    console.printf("first and third are the same ignoring case");
}
Lee B
Lee B
1,141 Points

Ah, I was getting ahead of myself with System.exit(0); Thank you!

Remy Benza
Remy Benza
6,259 Points

To elaborate a little further on String equality. This goes beyond your question but is important to realise when working with Strings in Java (and Strings are everywhere). If you dont fully understand this topic right now, it's fine. Come back to this answer later and review again. This will probably make more sense than. Getting to know the String class in java took me some time.

In general both the equals() method and “==” operator in Java are used to compare objects to check equality but the ‘’==’ operator’ checks if both objects point to the same memory location whereas the equals() method evaluates the values inside the objects. You can override the standard equals() method from java.lang.Object to implement a custom equals() method. Strings are special in Java - they're immutable, and string constants (=variables that do not change) are automatically turned into String objects. Nor can you extend String, because it's declared as final, meaning no sub-classing is allowed (if you're not into inheritance that's ok for now).

So now a concrete example if you write:

String s = "Hello";

String t = "Hello";

Then s and t actually refer to the same String object, and ‘s == t’ will return true, because for objects the double equal sign ("==") reads as "is the same object".

If you write:

String s = new String("Hello");

String t = new String("Hello");

Then s!=t (because you've explicitly created a new string) although s.equals(t) will return true (because the equals method checks for value equality instead of reference equality).

There is a "String constant pool" in the Java language. In Java String objects are immutable (i.e their values cannot be changed once they are initialized), so when editing a string object you end up creating a new edited string object wherease the old object just floats around in a special memory area called the "string constant pool". creating a new string object by

String s = "Hello";

Will only create a String object in the pool and the reference s will refer to it, but by using

String s = new String("Hello");

You create two string objects: one in the pool and the other in the heap. the reference will refer to the object in the heap. This is pretty technical but will make more sense when you start to work with Strings more and more.