How does this code work? | Sololearn: Learn to code for FREE!


How does this code work?


6/20/2021 8:47:09 AM

alagammai uma

7 Answers

New Answer


alagammai uma String a and d stores reference of new string "abc" so if you compare value with reference then result will be false. b and c are values so comparison will return true but in other comparison result will be false because of comparison of value with reference. use equals method to compare string reference with value. b.equals(d) //will return true


There is still some lack of understanding. Why does b==c return true? The variable 'b' gets created in the String constant pool. The speciality of this pool is that it doesn't store duplicates. So when you create variable 'c' with the same value as 'b' then 'c' receives just the reference of 'b'. So both of them are now pointing to the same value in memory. This is why it returns true. So c="abc" is never created. Hope that clears the doubt.


When you use new operator it makes a new object in heap memory. But when you simply declare it . It's object is made in string pool area


Atul 🅰🅹 🅐🅝🅐🅝🅣 Thank you

+1 //Hope this helps you


Avinesh thank you


Avinesh yes avinesh I thought the same but still needed clarification on other comparisons