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:
varis 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 membervar:class Book { var title = "Kotlin in Action" // Valid class property var pageCount = 400 } - Java:
varis 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:
varexplicitly denotes a mutable variable (you can reassign its value later). Kotlin has a built-in complementary keywordvalfor 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:
varon its own creates a mutable local variable. To make it read-only, you have to add thefinalmodifier manually (final var). There's no dedicated language-level keyword for immutable variables like Kotlin'sval.var mutableNumber = 5; mutableNumber = 10; // Allowed final var immutableNumber = 5; immutableNumber = 10; // Compile error — final variable can't be reassigned
3. Null Safety
- Kotlin:
vardefaults 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:
varhas 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@NonNullannotations.var nullableName = "Bob"; nullableName = null; // Allowed, no compile error
4. Type Binding Behavior
- Kotlin: Once a
varvariable 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,
varuses compile-time type inference, and the type is fixed after initialization. The key difference here is that Java'svaris 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
varvariables, you must either initialize them at declaration or set their value in the class constructor. Localvarvariables need to be initialized before they're used. - Java: All
varlocal variables must be initialized at the time of declaration — the compiler can't infer the type from an uninitialized variable.
内容的提问来源于stack exchange,提问作者gldanoob
相关产品推荐
相关产品推荐

