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

Java 10与Kotlin中var关键字的使用差异是什么?

Differences Between var in Kotlin and Java 10+

Great question! Let's break down the key distinctions between how var works in Kotlin versus Java 10 and later versions:

1. Scope of Usage

  • Kotlin: var is incredibly flexible — you can use it for top-level variables, class member properties, local variables inside functions/code blocks, and even function parameters (though mutable parameters are generally discouraged in Kotlin).
    Example of a class member var:
    class Book {
        var title = "Kotlin in Action" // Valid class property
        var pageCount = 400
    }
    
  • Java: var is strictly limited to local variables (variables inside methods, code blocks, or for-loop declarations). You can't use it for class fields, method parameters, or return types — those still require explicit type declarations.
    This Java code will throw a compile error:
    class Book {
        var title = "Java in Action"; // Invalid! var can't be a class member
    }
    

2. Mutability & Readable Counterpart

  • Kotlin: var explicitly denotes a mutable variable (you can reassign its value later). Kotlin has a built-in complementary keyword val for read-only (immutable) variables, which is a core part of the language's design.
    var mutableNumber = 5
    mutableNumber = 10 // Allowed
    
    val immutableNumber = 5
    immutableNumber = 10 // Compile error — can't reassign val
    
  • Java: var on its own creates a mutable local variable. To make it read-only, you have to add the final modifier manually (final var). There's no dedicated language-level keyword for immutable variables like Kotlin's val.
    var mutableNumber = 5;
    mutableNumber = 10; // Allowed
    
    final var immutableNumber = 5;
    immutableNumber = 10; // Compile error — final variable can't be reassigned
    

3. Null Safety

  • Kotlin: var defaults to non-nullable types. If you want a variable to hold null values, you must explicitly add a ? to the type (even when using type inference).
    var nonNullableName = "Bob"
    nonNullableName = null // Compile error — non-nullable type can't hold null
    
    var nullableName: String? = "Bob"
    nullableName = null // Allowed
    
  • Java: var has no built-in null safety. The inferred type can be nullable, and the compiler won't enforce non-null constraints unless you use additional tools like @NonNull annotations.
    var nullableName = "Bob";
    nullableName = null; // Allowed, no compile error
    

4. Type Binding Behavior

  • Kotlin: Once a var variable is initialized, its type is fixed and unchangeable (even though the value can be reassigned). The compiler infers the type at compile time and locks it in.
    var count = 3 // Inferred type is Int
    count = "Three" // Compile error — can't assign String to Int
    
  • Java: Similarly, var uses compile-time type inference, and the type is fixed after initialization. The key difference here is that Java's var is purely a syntax sugar for local variables — it doesn't change how the JVM treats the variable under the hood.

5. Initialization Requirements

  • Kotlin: For class member var variables, you must either initialize them at declaration or set their value in the class constructor. Local var variables need to be initialized before they're used.
  • Java: All var local variables must be initialized at the time of declaration — the compiler can't infer the type from an uninitialized variable.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:47:01