+ 2
Why a different output in these two loops?
// why without () around n+"" in this loop "hi" is printed 100 times?: int n = 100; for (int i = 1; i <= n+"".length(); i++) { System.out.println("hi"); } // and with () around n+"" in this loop "hi" is printed 3 times: int n = 100; for (int i = 1; i <= (n+"").length(); i++) { System.out.println("hi"); } // why isn't n evaluated as a string in the first loop?
4 ответов
+ 2
The first one is evaluated like this
n + ("".length()), the length of empty string is 0 so it's like 100 + 0. Well, you know how it's evaluated in the second one
+ 2
It seems that method invocation has a higher precedence than addition. Would that be right Agent_I ?
+ 2
Sonic
Certainly, the dot operator is evaluated before the addition
0
oh 😅, thank you Agent_I 😊