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 Answers
+ 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