Home > Back-end >  A very simple question, how to use the var in Java
A very simple question, how to use the var in Java

Time:04-03

Watching the core Java said since Java10 can use var to define a local variable, the original text is as follows:

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 10

CodePudding user response:

My latest Java16

CodePudding user response:

Java with 8 kinds of basic types defined variables cannot be var the weak language definition types

CodePudding user response:

Java basic type contains 8 kinds of basic types, byte, short, int, double, float, long, char, Boolean

CodePudding 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 more

CodePudding user response:

refer to the second floor kittens love learning response:
my latest Java16

I guess you installed Java16 local, but the compiler specify JDK is not you install this, you'll just take a look
  • Related