What's the logic behind these two outputs? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

What's the logic behind these two outputs?

System.out.println(1+1+(1+"1")); // output 211 System.out.println(1+1+(1+"1").length()); // output 4 Why isn't length() returning 3 instead? I know it does if you place another set of parentheses before the first "1" and before the dot operator.

8th Jul 2020, 8:15 PM
Mind To Machine 💻🕆
Mind To Machine 💻🕆 - avatar
2 Respuestas
+ 5
(1 + "1") evaluates to "11". "11" is a string containing 2 characters and thus "11".length() evaluates to 2. 1 + 1 + 2 evaluates to 4.
8th Jul 2020, 8:22 PM
Seb TheS
Seb TheS - avatar
0
Seb TheS Thank you 🤦🏿‍♂️
8th Jul 2020, 8:26 PM
Mind To Machine 💻🕆
Mind To Machine 💻🕆 - avatar