Starting with Java 10, you do not need to declare the types of local variables if they can be inferred from the initial value. The simple use the keyword varinstead of the type:
Var vacationDays=12;//vacationDays is an int
Var the greeting="Hello";//the greeting is a String
I tried, but no, I don't know what to pay attention to? I wrote
Public class test1 {
Public static void main (String [] args) {
Var n=100;
System. The out. Println (n);
}
}
Compile the prompt:
Warning: (3, 13) Java: since release 10 'var' local variable type is restricted, cannot be used in the type declaration, also cannot be used as elements of an array of type
Error: (3, 9) Java: can't find the symbols
Symbol: class var
Location: class test1
CodePudding user response:
You compiled version not 10CodePudding user response:
My latest Java16CodePudding user response:
Java with 8 kinds of basic types defined variables cannot be var the weak language definition typesCodePudding user response:
Java basic type contains 8 kinds of basic types, byte, short, int, double, float, long, char, BooleanCodePudding user response:
Var is Java10 version of new features, use it to define local variables,Use var to define variables of grammar I pot used so high I can with you check the JDK version of 8
CodePudding user response:
Anyway, I've tested var use is no problem, make sure you use the compiler JDK is 10 or moreCodePudding user response: