What does "+ i" mean in Java?

I looked at the code of a colleague. She left it by accident (it was a string concatenation), and I assumed that it would not compile. It turns out I was wrong, so I tried to see what this operator did:

public static void main(String[] args) {
    int i = -1;
    System.out.println(String.format("%s", +i));
    System.out.println(String.format("%s", +i));
}

As far as I can tell, he is not doing anything, but I am curious if there is a reason this is allowed to compile. Is there any hidden functionality for this operator? It is similar to ++i, but you might think that the compiler will be tagged +i.

+4
source share
1 answer

+. , " byte, short char, int".

++, 1. ( ) (postfix operator) . , (++i) , (i++) .

int i = -1;
System.out.println(+i);         // prints -1

System.out.println(i++);        // prints -1, then i is incremented to 0

System.out.println(++i);        // i is incremented to 1, prints 1
+2

All Articles