2

I was asked to predict the output of this code :

String party1 = "party";
String party2= "PARTY".toLowerCase();
if(party1==party2){
   System.out.println("EQUAL");
}else{
   System.out.println("NOT EQUAL");
}

I thought the result of toLowerCase() would be interned automatically and hence the same reference to party1 would be returned, but I was wrong since the code ouptuts "NOT EQUAL".
Does toLowerCase() (toUpperCase() as well) return a new String ?

Edit : I am not using == to compare Strings, I just want to know about the behaviour of toLowerCase() method

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
mounaim
  • 1,132
  • 7
  • 29
  • 56
  • 3
    Why did you expect it to not return a new string? – Sweeper Aug 09 '19 at 09:08
  • 2
    Never, ever rely on `==` for string comparison. –  Aug 09 '19 at 09:09
  • 3
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Ben R. Aug 09 '19 at 09:10
  • 5
    "I thought the result of toLowerCase() would be interned automatically" — What made you think that? – khelwood Aug 09 '19 at 09:11
  • @a_horse_with_no_name I use equals() for that end, my question was about the specific implementation of toLowerCase() – mounaim Aug 09 '19 at 09:19
  • 1
    "I thought the result of toLowerCase() would be interned automatically" did you mean that you thought an instance of the string would be returned from the string pool if one exists? If so, no: there is no (public) mechanism to query whether a string exists in the string pool, other than by calling `intern()` and seeing if the same string comes back. – Andy Turner Aug 09 '19 at 09:47

4 Answers4

5

If the result of any String operation were internalised, the pool would be growing extremely and unnecessarily rapidly. Any intermediate action on a Spring would result in a new element in the pool. It would be beneficial for a VERY small number of cases within a VERY specific domain. Therefore, it's not the default policy and that's why intern() exists.

String party1 = "party";
String party2 = "PARTY".toLowerCase().intern();
System.out.println(party1 == party2 ? "EQUAL" : "NOT EQUAL");

There is nothing special about toLowerCase(): any String method would return a new String since Strings are immutable.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
2

Answer is in String.toLowerCase() method itself.. as you can check by your self toLowerCase() method returns a new lowercase string (a new String object) and == checks for the object reference and returns true if they are same which are different in your case.. that's why its returning false.. hope you get it.

public String toLowerCase(Locale locale) {
    if (locale == null) {
        throw new NullPointerException();
    }
    ........

    ............

    return new String(result, 0, len + resultOffset);
}
DhaRmvEEr siNgh
  • 1,918
  • 2
  • 13
  • 17
1

Strings are immutable.Generally whenever we try to modify a string a new string literal is created in the Constant literal pool and a new reference to it is returned.Although "party" and "PARTY".toLowerCase() have same values i.e. party but in constant pool they refer to two different literals and have two different references. By using == operator you are comparing references and not the values.Since they have different references it prints NOT Equal.Try using party1.equals(party2) and it will print EQUAL as it compares content and not reference.

Constant literal pool and its references

Animesh Jaiswal
  • 331
  • 3
  • 7
0

As of my knowledge on any operations on string it will duplicate the string so never use '==' instead use .equals()

String party1 = "party";
        String party2= "PARTY".toLowerCase();
        System.out.println(party1);
        System.out.println(party2);
        if(party1.equals(party2)){
           System.out.println("EQUAL");
        }else{
           System.out.println("NOT EQUAL");
        }

This gave the result equal

harsha reddy
  • 72
  • 1
  • 13