I'm a newbie to Java. Now I'm studying "==" operator with String.
I know that in java, string literals are stored in String Pool in Heap area to achieve immutability and memory saving. And I know that if I use the "new" keyword, it is stored in the Heap area, not the String pool.
In the code below case_1, case_2, case_3 all return the same string literal. So I expected everything to print true. But for case_1 it printed false.
My question is:
- Since case_1, 2, and 3 all return the same string literal, they all point to the same address in the string pool, so it should output true, but why does it output false for case_1?
If possible, please answer with reference to the String pool in the Heap.
Thanks!
public class Main {
    public static void main(String[] args) {
        MyString ms = new MyString();
        String str1 = ms.getString1(10);
        String str11 = ms.getString1(10);
        String str2 = ms.getString2();
        String str22 = ms.getString2();
        String str3 = 10 + "";
        String str33 = 10 + "";
        System.out.println(str1 == str11); // case_1
        System.out.println(str2 == str22); // case_2
        System.out.println(str3 == str33); // case_3
    }
}
class MyString {
    String getString1(int n) {
        return n + "";
    }
    String getString2() {
        return 10 + "";
    }
}
I expected the following
true
true
true
but the actual output like this
false
true
true
 
    