Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
public class Main {
public static void main(String[] args) {
var n = 10;
n = 2.5;
System.out.println(n);
}
}It does not compile
- Expecting
2.5:varis not dynamic typing; the compiler fixesnas anintfrom10, and adoublecannot be assigned to it. - Expecting
2: Java never narrows adoubleto anintsilently, so assigning2.5to anintvariable does not compile.
- Where in Java is
varnot allowed? - What type does
var x = 10L;get, and why?