3

I remember reading somewhere that local variables with inferred types can be reassigned with values of the same type, which would make sense.

var x = 5;
x = 1; // Should compile, no?

However, I'm curious what would happen if you were to reassign x to an object of a different type. Would something like this still compile?

var x = 5;
x = new Scanner(System.in); // What happens?

I'm currently not able to install an early release of JDK 10, and did not want to wait until tomorrow to find out.

Jacob G.
  • 28,856
  • 5
  • 62
  • 116
  • if they are both the same types then it should work otherwise no. speaking from experience in C# and I believe the use of `var` in java functions the same way. – Ousmane D. Mar 19 '18 at 21:56
  • 6
    Type inference is not dynamic typing. It's simply the compiler figuring out the type of a variable from the type of its initializer. Once the compiler infers `int` for `x` in your declaration, you have declared a variable of type `int`. That's the whole story; inference plays no additional part. – Brian Goetz Mar 19 '18 at 22:50
  • Btw *not able to install an early release of JDK 10* (for what reasons? what failures or errors) could've been a better question asked. ;) – Naman Mar 20 '18 at 01:49
  • @nullpointer Entirely unrelated :P it's a school-issued computer and they don't want us installing things, unfortunately! – Jacob G. Mar 20 '18 at 01:50
  • :) Maybe you're right. But then look at this perspective, your question might simply sound like *"I am not able to install Java, can someone let me know if `int x = 1;` compiles while working with it?"* Isn't it? And then the answers are like, *I've tried it on so and so machine. Yes, it compiles* or *No, it doesn't.* – Naman Mar 20 '18 at 01:53
  • Of course not, why would you think that? `Scanner` and `int` are two completely different unrelated types. – Jörg W Mittag Mar 22 '18 at 13:16

2 Answers2

8

Would not compile, throws "incompatible types: Scanner cannot be converted to int". Local variable type inference does not change the static-typed nature of Java. In other words:

var x = 5;
x = new Scanner(System.in);

is just syntactic sugar for:

int x = 5;
x = new Scanner(System.in);
M A
  • 71,713
  • 13
  • 134
  • 174
  • That's what I thought. Just curious, but what error does the compiler throw? – Jacob G. Mar 19 '18 at 21:57
  • @JacobG. Tried it on the EA OpenJDK --> "incompatible types: Scanner cannot be converted to int" – M A Mar 19 '18 at 22:11
  • Makes sense, so it *is* just syntactic sugar. Thank you! – Jacob G. Mar 19 '18 at 22:13
  • 2
    @manouti there is a follow sort of question here https://stackoverflow.com/questions/49410939/useful-applications-of-intersection-types-using-local-type-inference while this is indeed syntactic sugar, the *type* of var can be inferred by the compiler, and there are types the compiler can infer that we can not declare – Eugene Mar 21 '18 at 19:35
  • 1
    @Eugene Interesting post. You're right in this particular case it's syntactic sugar. With non-denotable types, it's more of an implicit typing of something that we couldn't express before. – M A Mar 21 '18 at 20:30
4

Once a var variable has been initialized, you cannot reassign it to a different type as the type has already been inferred.

so, for example this:

var x = 5;
x = 1; 

would compile as x is inferred to be int and reassigning the value 1 to it is also fine as they're the same type.

on the other hand, something like:

var x = 5;
x = "1"; 

will not compile as x is inferred to be int hence assigning a string to x would cause a compilation error.

the same applies to the Scanner example you've shown, it will fail to compile.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126