-3

Is there a difference between declaring var x and int x?

//Java 8
int x = 10;

//Java 10
var x = 10;
  • Java 8 example requires you to explicitly declare the type of the variable. Java 10 example allows you to use type inference; the compiler will figure out the variable's type for you – byxor Oct 02 '19 at 15:43
  • the only difference is that first one *forces* the correct type on right side (e.g. `var x = 10.0;`or `var x = "10";` will compile, probably breaking some lines later) – user85421 Oct 02 '19 at 16:33

1 Answers1

3

There is no run-time difference. var is compile-time syntactical sugar. If you replace var with the inferred type (int) you get identical results.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578