Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

The Difference between var in Kotlin and Java 10

$
0
0

1. Introduction

In this short tutorial, we’ll cover differences between var keywords in Java and Kotlin.

We cover the identifier var in Java in more depth in our article Java 10 LocalVariable Type-Inference, and we talk about the keyword var in Kotlin in our article Kotlin const, var, and val Keywords.

2. Optional Type Declaration vs No Type Declaration

2.1. Kotlin

If we declare a mutable variable and initialize it at the same time, we don't need to specify the type:

var myVariable = 1

However, we can do so if we choose to:

var myVariable : Int = 1

If we want to declare a variable without initializing it directly, we need to specify its type, and we can assign a value later:

var myVariable : Int
myVariable = 1

2.2. Java

In Java, we can only use var if we initialize the variable on-the-spot, and we can't provide type information even if we want to:

var myVariable = 1;

The following is invalid:

var myVariable;
myVariable = 1;

And we get a compile-time error:

Cannot infer type: 'var' on variable without initializer

3. Keyword vs Identifier 

In Kotlin, var is a keyword, which means we cannot use it as the name of a variable, parameter, method or class. In contrast, Java 10 defines var as an identifier with special meaning.

That means in Java 10, we can still define a variable, parameter, or method with the name var:

public void var() {
    // do something
}

public void method(int var) {
    // do something
}

public void method() {
    int var = 1;
    // do something
}

Keep in mind that, as of Java 10, it’s not possible anymore to define a class with the name var:

public static class var {
}

The above code results in a compile-time error:

Error:(1, 1) java: 'var' not allowed here
  as of release 10, 'var' is a restricted type name and cannot be used for type declarations

This ensures backward compatibility with older Java versions.

4. Mutability

A variable declared with var in Kotlin is always mutable, while a variable declared with var in Java can be mutable or final:

var mutableInt = 1;
final var immutableInt = 1;

In Kotlin, there is no final keyword — final variables are declared with val instead:

val immutableInt = 1

5. Global vs Local Usage

5.1. Kotlin

In Kotlin, we can use var to declare global and local variables, as well as class members:

var myGlobalVar = 3

class MyClass {

    var myVar = 3    

    public fun myFunction() : Int {
        var myVariable = 3
        return myVariable
    }
}

5.2. Java

In Java, we can use var only to declare local variables:

public class MyClass {

    //not possible for member variables
    //private var myVar = 1;

    public int myFunction() {
        var myVariable = 3;
        return myVariable;
    }
}

6. Conclusion

In this article, we looked at the difference between var in Kotlin and Java. Even though both look very similar, there are fundamental differences between the two.

The most important difference is that var in Kotlin is about mutability, and var in Java is about type inference.


Viewing all articles
Browse latest Browse all 4535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>