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

+2

How does this code work?

Strings https://code.sololearn.com/cpxcW1LL4V60/?ref=app

6/20/2021 8:47:09 AM

alagammai uma

7 Answers

New Answer

+5

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 https://code.sololearn.com/cZms6c3vg2w3/?ref=app

+4

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.

+2

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

+2

Atul 🅰🅹 🅐🅝🅐🅝🅣 Thank you

+1

https://youtu.be/d1NPEb1MC5U //Hope this helps you

+1

Avinesh thank you

0

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