intern() behaving differently in Java 6 and Java 7

后端 未结 9 1057
梦谈多话
梦谈多话 2020-11-29 21:36
class Test {
    public static void main(String...args) {
        String s1 = \"Good\";
        s1 = s1 + \"morning\";
        System.out.println(s1.intern());
              


        
9条回答
  •  一向
    一向 (楼主)
    2020-11-29 22:11

    there are mainly 4 ways to compare string:

    1. "== operator": it just compares the reference variable of the string object. So it might give you unexpected results depending upon how you have created the string i.e. using String class's constructor or simply by using double quote as both get memory differently(in heap and pool respectively).
    2. "equals(Object) method": this is method of object class and is OVERLOADED by string class. It compares whole string and IS CASE SENSITIVE.
    3. "equalsIgnoreCase(String) method": this is method of string class and compares whole string and IS NOT CASE SENSITIVE.
    4. "compares(String) method": compare both strings character by character and return their difference if the returned value is 0, this means strings are equal.

提交回复
热议问题