You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java 10中var究竟属于哪种类型的标记?

Great question! Let's dive into this key detail from Heinz Kabutz's 255th newsletter focused on Java 10's local variable type inference:

Java 10: Local Variable Type Inference – var vs. Reserved Words
  • var isn’t a reserved keyword in Java 10
    Here’s the critical distinction: you can use var as an identifier (like a variable name), but true reserved words such as assert don’t allow this. For example:

    • Valid code (no compilation issues):
      int var = 10; // Works perfectly in Java 10+
      
    • Invalid code (throws a compile error):
      var assert = 2; // Fails because `assert` is a reserved word
      
  • This design choice is a win for backward compatibility
    The newsletter emphasizes that this was an intentional, positive decision. Any pre-Java 10 code that used var as an identifier (variable name, method parameter, etc.) will still compile smoothly in Java 10 and later versions. You won’t need to refactor those old identifiers just to upgrade your JDK!

内容的提问来源于stack exchange,提问作者fps

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 06:58:27