What's the logic behind these two outputs? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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 Respostas
+ 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