Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
变量可以在Java中具有动态类型绑定吗?我知道方法可以,但我不认为变量可以,因为每个变量在创建时都必须有一个类型声明,例如 int、double 或 float。如果他们可以,那将如何做?谢谢。
从 Java 10 开始,var引入了关键字以允许局部变量 类型推断,这意味着局部变量的类型将由编译器推断,因此您无需声明。例如,
var
var myNum=0;// At this moment, compiler interprets as an integer
因此,如果稍后您尝试为其分配String值,它将引发错误。请注意,我强调这仅适用于局部变量。
String