+ 2

why?

String s=8+"hello" ? String s="8"+"hello"? what is the ans and why ?

30th May 2017, 5:24 PM
Somnath Ghosh
Somnath Ghosh - avatar
3 Antworten
+ 16
To be precise, it's a compilation error because s is already defined ;) But you mean what is stored in s for both definitions... Answer is: 8hello for both. They're equal because everything after the = is casted to string internally if you use a string type.
30th May 2017, 5:35 PM
Tashi N
Tashi N - avatar
+ 12
String s = 8 + "hello" casted in : "8" + "hello" = 8hello try this : s= 1 + 2 + 3 + 'h' +"ell" + 'o' + 1 + 2 + 3; can you explain why ?
30th May 2017, 6:11 PM
NimWing Yuan
NimWing Yuan - avatar
+ 2
@NimWing Yuan s=6hello123 Reason: '+' operator has same priority and associativity will be left to right. So it will be like 1+2=3, 3+3=6, 6+'h'='6h','6h'+'ell'='6hell' and so on....
31st May 2017, 11:55 AM
Dhaval