+ 1

(Solved) Can anyone please explain the output of this code?

I am confused as to why the three tests are giving the output that they are doing. Please explain a little bit in depth like how the object is being created and which is being assigned to which, etc. https://code.sololearn.com/cxXFAduO6lY3/?ref=app

7th Dec 2020, 6:06 AM
Gammaburst
Gammaburst - avatar
18 Answers
+ 2
in test 1, a is created in heap, b in string pool(inside heap) both are different object , so false test2, a and b point to string pool, since "SoloLearn" already exist, b will use the same object as a.both are same object so true test3, both are stored in heap.both are different obj, so false thats all i know..probably you should read more about string pool/literal
7th Dec 2020, 8:06 AM
durian
durian - avatar
+ 3
Hii Gamma Guy Which QUESTIOn are you talking about? Can you attach the question in description so that there can be more chances of helping you!
7th Dec 2020, 6:20 AM
Piyush
Piyush - avatar
+ 2
This type of code is available in challenge in java
7th Dec 2020, 2:36 PM
Yash Chaudhari
Yash Chaudhari - avatar
+ 1
do you mean how string is created?
7th Dec 2020, 6:20 AM
durian
durian - avatar
+ 1
I think he meant to say, how the output is: false true false in the code ,he has given.
7th Dec 2020, 7:00 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 1
Piyush[21 Dec❤️] In the question, I was referring to the attached code bit.
7th Dec 2020, 7:49 AM
Gammaburst
Gammaburst - avatar
+ 1
did you mean why the middle one is true, while other are false?
7th Dec 2020, 7:52 AM
durian
durian - avatar
+ 1
idk really much about java..but it think using == to compare string will compare the object(same object or not)
7th Dec 2020, 7:57 AM
durian
durian - avatar
+ 1
the middle is true,cause string object "Sololearn" is already created, so when you try to create a string literal with same string,it will return u that existing string
7th Dec 2020, 7:59 AM
durian
durian - avatar
+ 1
meanwhile the new operator will create a new string object in the heap..so it is another object even thoug have same string(characters)..thats why it false
7th Dec 2020, 8:00 AM
durian
durian - avatar
+ 1
if u want to compare the string,use .equals() method instead
7th Dec 2020, 8:01 AM
durian
durian - avatar
+ 1
Lily Mea Thanks a lot Lily. My doubt is cleared now. I will definitely read up on string pool.
7th Dec 2020, 8:10 AM
Gammaburst
Gammaburst - avatar
0
Lily Mea Yes, that's what I meant. Here's a little more info if you want...I was confused as why one time the output was true and the other time it was false. I know that == checks for whether the objects are the same or not. I want to know when two strings created, are the same or different
7th Dec 2020, 7:53 AM
Gammaburst
Gammaburst - avatar
0
Zatch bell yes, that's what I meant...
7th Dec 2020, 7:53 AM
Gammaburst
Gammaburst - avatar
0
Yash Chaudhari that's where I found it
8th Dec 2020, 12:44 PM
Gammaburst
Gammaburst - avatar
0
hugo rada The question has already been answered...Look at the answers by lily mea
8th Dec 2020, 12:45 PM
Gammaburst
Gammaburst - avatar
0
x was initiallized in the main. When try to overload x variable it will generate an error on x=8
8th Dec 2020, 11:28 PM
Lukman Ja'afar Aliyu
Lukman Ja'afar Aliyu - avatar