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.

7th Jul 2019, 11:25 AM
Anish Abhyankar
Anish Abhyankar - avatar
3 Respuestas
+ 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
7th Jul 2019, 12:04 PM
Odiesta Shandikarona
Odiesta Shandikarona - avatar
0
b
5th Apr 2024, 2:43 PM
Balu Rahane
Balu Rahane - avatar
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
7th Apr 2025, 6:38 AM
Youssef Atef