mounaim
mounaim

Reputation: 1180

Why the String returned by String.toLowerCase() is not interned?

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

Upvotes: 2

Views: 1133

Answers (4)

Animesh Jaiswal
Animesh Jaiswal

Reputation: 371

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

Upvotes: 1

Andrew
Andrew

Reputation: 49646

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.

Upvotes: 5

DhaRmvEEr siNgh
DhaRmvEEr siNgh

Reputation: 2098

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);
}

Upvotes: 2

harsha reddy
harsha reddy

Reputation: 82

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

Upvotes: 0

Related Questions