How does this code work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2
20th Jun 2021, 8:47 AM
alagammai uma
alagammai uma - avatar
7 Answers
+ 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
20th Jun 2021, 9:17 AM
A͢J
A͢J - avatar
+ 5
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.
20th Jun 2021, 12:04 PM
Avinesh
Avinesh - avatar
+ 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
20th Jun 2021, 9:41 AM
Atul [Inactive]
20th Jun 2021, 10:21 AM
alagammai uma
alagammai uma - avatar
+ 1
https://youtu.be/d1NPEb1MC5U //Hope this helps you
20th Jun 2021, 9:42 AM
Atul [Inactive]
+ 1
Avinesh thank you
20th Jun 2021, 12:11 PM
alagammai uma
alagammai uma - avatar
0
Avinesh yes avinesh I thought the same but still needed clarification on other comparisons
20th Jun 2021, 12:10 PM
alagammai uma
alagammai uma - avatar