-2

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

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • 1
    "As both the String objects [...] are stored in constant Pool " There's your problem, this assumption is wrong; in the second case, the `rev` string is allocated in the heap and is not interned. – Erwin Bolwidt Feb 06 '16 at 06:13

3 Answers3

2

In program 1 java compiler saves "MADAM" string in one memory location and assigns both "s" and "rev" to that location hence "s==rev" returns true because they both refer to the same address. you should use "equals()" method to compare two strings. e.equals(rev);

have a look at this question: Java String.equals versus ==

Community
  • 1
  • 1
  • Ya... And then what about the program 2... Why am I getting false over there... As after the execution of the program 2 'rev' is again having the value 'MADAM'. – Dhruv Govila Feb 06 '16 at 06:15
  • @DhruvGovila Because at program 2, they are no longer in the same location. Take a look at my solution. – user3437460 Feb 06 '16 at 06:18
  • He is right in the second program you initialize the string at runtime. so the java compiler can not use the previous memory location for the new string. – Nexus Original Ltd Feb 06 '16 at 06:24
0

In your first class both strings are initialize to same object. So both are pointing to same memory location.

Next class, Rev is intilialized to "" value and see to madam so both have got different memory location. So false.

Chandrashekhar Swami
  • 1,742
  • 23
  • 39
  • I think may be you missed the code line 'for(int x=s.length()-1;x>=0;x--) { rev+=s.charAt(x); }' . Where i am initializing 'rev' again to 'MADAM'... – Dhruv Govila Feb 06 '16 at 06:31
  • Actually different memory locations are already assigned when you intilialized Rev ="", so doesn't matter what you are doing in for loop.. Rev and s will not be equal == – Chandrashekhar Swami Feb 06 '16 at 07:23
0

In essence,

  1. if you use == for comparison, you are comparing their identity.

  2. If you want to compare the object's value, use .equals()


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

The above code will return true, because both Strings will be stored in the same memory location.

However, you can try the following, it will return you false:

String s1 = "aaa";
String s2 = new String("aaa");

System.out.println(s1 == s2);       //false (comparing memory location)
System.out.println(s1.equals(s2));  //true  (comparing value)

Side note: It is generally a bad practice to create Strings using new String(""). It was only used for demonstration purposes only.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • In the second program i have not used the 'new' operator. So 'rev' and 's' should be in the same 'constant memory pool' and after the execution of the program the output should come as 'true'. And I am aware of the 'equals' function, and am looking the solution specific to '=='. Thanks – Dhruv Govila Feb 06 '16 at 06:35