0
Java programming trace the output?
What is the output of this code? String a=âaâ; String b=âbâ; String c=a+b; String d=âabâ; If(c==d) System.out.println(1); else System.out.println(0); Output is 0 why? Please solve my doubt!! because I lost this question in SoloLearn challenge.
3 Respostas
+ 2
'==' is a reference comparison.
c and d has same value but different reference. So
c.equals(d) will return true
c==d will return false
0
b
0
Because when you run the code the memory will make combination between a and b while the d will have the ab already in it's place in the memory
and for another reason ( == ) this type of equalization it compare the references not the value of it and the code here was compare between the letter c and d itself now the value that store inside them , so if you you to make this code output become 1 you should make this
if ( c equal(d)){
System.out.println("1");
the output here will be 1