+ 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
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
+ 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!
+ 2
This type of code is available in challenge in java
+ 1
do you mean how string is created?
+ 1
I think he meant to say, how the output is:
false
true
false
in the code ,he has given.
+ 1
Piyush[21 Dec❤️] In the question, I was referring to the attached code bit.
+ 1
did you mean why the middle one is true, while other are false?
+ 1
idk really much about java..but it think using == to compare string will compare the object(same object or not)
+ 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
+ 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
+ 1
if u want to compare the string,use .equals() method instead
+ 1
Lily Mea Thanks a lot Lily. My doubt is cleared now. I will definitely read up on string pool.
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
0
Zatch bell yes, that's what I meant...
0
Yash Chaudhari that's where I found it
0
hugo rada The question has already been answered...Look at the answers by lily mea
0
x was initiallized in the main. When try to overload x variable it will generate an error on x=8