+ 2
How does this code work?
7 Antworten
+ 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
+ 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.
+ 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
+ 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