0

In Program 1 I have declared two String and initialized them as "MADAM". When running I am checking the equality of their reference variable (by '==' operator') then I am getting a "true" response.

But in Program 2 I am declaring a String 'S' and initialize it as "MADAM". After that i am running a reverse loop and storing the characters of 'S' in reverse order in other String variable. Now i have again tried to check the equality of reference variable (by '==' operator') and am getting the response as 'false'. As both the String objects are of same value and are stored in constant Pool Area so both the variable should equate and the output in both the scenario should be 'true'. But WHY it is not same?

Program 1: class Reverse { public static void main(String[] args) {

    String s="MADAM";
    String rev="MADAM"; 
    System.out.println(s==rev);
  }
}

Output - true

Program 2: class Reverse { public static void main(String[] args) {

    String s="MADAM";
    String rev="";  

    for(int x=s.length()-1;x>=0;x--)
    {
        rev+=s.charAt(x);
    }

    System.out.println(s==rev);
}

}

Output- false

  • 2
    Why do you think the result of all those `+=` operations will be in the constant pool? – user2357112 Feb 05 '16 at 21:36
  • Because the String on which '+=' operation is being performed has not been created with the 'new' keyword so it will reside in Constant Pool Area... – Dhruv Govila Feb 05 '16 at 21:41
  • "*Because the String on which '+=' operation is being performed has not been created with the 'new' keyword*" and why do you think so? – Pshemo Feb 05 '16 at 21:52
  • Because I have declared it above without using 'new' keyword... If you have a solution then help me out... – Dhruv Govila Feb 05 '16 at 21:56
  • Best way to correct your thinking is to see where you are making mistake (otherwise we would need to write entire article about string pool and string concatenation). This is why I asked that question in previous comment. Now your answer is quite confusing. What do you mean by "Because I have declared it above without using 'new' keyword"? If you mean that new strings are created only when programmer explicitly use `new` keyword then you are wrong. Take a look at for instance at result of `"bar"=="foobar".substring(3)`. What do you think is result of it and why do you think so? – Pshemo Feb 05 '16 at 22:01
  • 1
    Or what do you think should be result for `String s1 = "foo"; String s2 = "bar"; System.out.println("foobar"==s1+s2);`? – Pshemo Feb 05 '16 at 22:05
  • @DhruvGovila If you decompile you will find that s1 + s2 statment is converted as: (new StringBuilder(String.valueOf(s1)).append(s2).toString();. Hence, its creating a new object altogether and that why it is false. – Java Guru Feb 05 '16 at 22:42
  • @JavaGuru what is also important here is fact that returned by `toString()` result is not part of String Pool. Generally results of all methods like `readLine`, `replace`, `toString` are internally creating new strings and these strings are not supposed to be part of pool since since its purpose is not to hold *all* strings, but only those which have chance to be reused later so they shouldn't be collected by GC because we could reuse them. That strings are literals like in: `print("name:");` as they are often part of code which will be reused many times (methods/constructors). – Pshemo Feb 05 '16 at 22:50

0 Answers0