Search code examples
javajava-10

Type inference in loop that uses var


Generally I know how works type inference in Java. But I'm curious how it works in loop when you use JDK 10 new feature var.

I mean that I can write loop in this way

for(var i=0; i<100; i++) {
//something to do
}

How does the compiler infer that there should be an int and not a long?


Solution

  • Because 0 is an int literal. 0L is a long literal.

    true is a boolean literal, "true" is a String literal. The compiler has no problems handling simple cases like these.