Can anyone explain why this outputs "true"? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone explain why this outputs "true"?

String name = "h"; name += 1; System.out.println(name.equals("h1"));

26th Mar 2017, 2:12 AM
Michael Russell
Michael Russell - avatar
4 Answers
+ 8
"h" + 1 results in "h1". So, the output is true.
26th Mar 2017, 2:28 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 2
Because, name is string type. And using + operator with string. It will append new value at end of string. when you add 1 to name it become h1. So, it is true
26th Mar 2017, 2:29 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 1
java converts String + int/double/char/long/float to newString
26th Mar 2017, 8:59 PM
Logan New
Logan New - avatar
0
oh, I thought I had gotten a different output when I checked the string before. Thanks guys.
26th Mar 2017, 3:13 AM
Michael Russell
Michael Russell - avatar