+ 1
What is going on here?
I Don't understand What is going on in this code. public class Program { public static void main(String[] args) { System.out.print((1 + "1") + 1 +1); } } The output is 1111. But why? What happens with the sum of 1 + 1? why is not 112?
4 Antworten
+ 10
All "+" operator are treated as concatenate operator After a string literal.
(1 + "1")+1+1
As you can see "1" this is a string literal here so all "+" use to append all the number.
//Output
1111
+ 4
The last two 1s will be appended to the string 11 one by one.
+ 3
Changing the string to a 2 to make this easier to read, I think the result step by step is:
(((1 + "2") + 1) + 1)
((1"2") + 1) + 1)
(1"2"1) + 1
1"2"11
1211
+ 1
I understand right now, thanks!!