+ 4

How the result of the code is true explain

The following was the challenge of sololearn quiz factory. The output of the code was true. Can someone explain me why the output is true as generally while comparing strings we use dot equals method. https://code.sololearn.com/cC3i0HIpoSOo/?ref=app

14th May 2019, 1:33 PM
Ayush Pandey
Ayush Pandey - avatar
7 odpowiedzi
+ 9
although you have created 2 strings with different variables in string pool there is only one string created called "a" both variables a and b refer to the same object "a" so a==b results in being true
14th May 2019, 1:38 PM
D_Stark
D_Stark - avatar
+ 9
== ⬅compares refreneces of both objects if you create a string using the new keyword and one without but both strings being the same then this would result in false becuase they have diffrent memory addresses.
14th May 2019, 1:48 PM
D_Stark
D_Stark - avatar
+ 5
D_Stark Thanks for u help.really it helped
14th May 2019, 3:05 PM
Ayush Pandey
Ayush Pandey - avatar
+ 3
Then y do we use equals method
14th May 2019, 1:39 PM
Ayush Pandey
Ayush Pandey - avatar
+ 3
14th May 2019, 1:39 PM
Ayush Pandey
Ayush Pandey - avatar
0
Equals method verify if it's true or not it does not give you Boolean value
23rd May 2019, 9:16 AM
md akil
md akil - avatar
0
Variable a and variable b both store the string value "a". When comparing, I find "a" (stored in a) and "a" (stored in b) to be equivalent. Hence checking for equivalence would return true.
27th May 2019, 10:46 AM
Robert Boettcher
Robert Boettcher - avatar