23

I know that we can use the “var” keyword for defining variables in Kotlin:

var foo = 3

The latest java update (java 10) also introduces the “var” type:

var bar = new int[]{1, 2, 3}; // int[] bar = {1, 2, 3}

My question is, what is the difference of the use of “var” between these languages?

ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
gldanoob
  • 762
  • 1
  • 7
  • 18

3 Answers3

28
  1. Their meaning is very different, even if the syntax in the basic case var x = ... ends up being the same:

    1. var in Kotlin means "this is a mutable variable", and can be used both when type is inferred and when it's explicit: var x: String = ...;

    2. var in Java means "this is a variable with inferred type", and can be used both for mutable and final variables.

  2. var in Java can only be used for local variables; var in Kotlin is used for properties as well.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • 2
    Bonus question: is var in Java the same as in c#? [This old post](https://stackoverflow.com/questions/3443858/what-is-the-equivalent-of-the-c-sharp-var-keyword-in-java) suggests that there was no equivalent in Java, but perhaps the answers are outdated now. – user1306322 Mar 29 '18 at 09:46
  • The top answer is outdated, but there are a couple of relevant answers there. – Alexey Romanov Mar 29 '18 at 09:49
  • By the way, is there a standard way to mark the answers as outdated in cases like this? You've got the rep here and I guess you'd know. Or maybe you could do it if it's appropriate. – user1306322 Mar 29 '18 at 09:54
  • @user1306322 There is no way to do this, other then downvote and/or comment and then upvote or post an up-to-date answer. – BartoszKP Mar 29 '18 at 09:56
  • @BartoszKP normally I would put a bounty and select "current answers are outdated" but that's only for when none of the answers are contemporary, which is not the case there. I've seen people edit in "this answer was true until DATE" at the top. – user1306322 Mar 29 '18 at 09:57
  • @user1306322 Maybe I skimmed too fast, but I only see claims that `var` is *planned* for Java 10, not that it's present. So "current answers are outdated" seems fine to me. – BartoszKP Mar 29 '18 at 09:58
  • I decided to put a bounty on it :p – user1306322 Mar 29 '18 at 10:09
  • 4
    `var` in Java is much closer to what it means in C# than to Kotlin or Scala. – Brian Goetz Mar 29 '18 at 13:34
  • var in Java 10 is almost identical to auto in C++11. With so many concepts and keywords borrowed from C/C++, java could have got auto as well. var could easily mislead somebody with a Swift or Kotlin background. – Seshadri R Apr 02 '18 at 17:17
  • @SeshadriR 1. It was considered, see questions 4 and 5 at https://sv.surveymonkey.com/results/SM-FLWGS5PW/. 2. I actually can't think of a single feature Java borrowed from C++ after initial Java definition (which doesn't mean there isn't one, of course). I don't consider generics a counterexample, they work very differently from templates. – Alexey Romanov Apr 03 '18 at 06:43
13

According to the release notes of Java 10, it states:

We seek to improve the developer experience by reducing the ceremony associated with writing Java code, while maintaining Java's commitment to static type safety, by allowing developers to elide the often-unnecessary manifest declaration of local variable types. This feature would allow, for example, declarations such as:

var list = new ArrayList<String>(); // infers ArrayList<String>
var stream = list.stream(); // infers Stream<String>

So basically, it's to accommodate the laziness of programmers; instead of writing List<String> list = new ArrayList<>();, you can now omit the List<String>, and replace it with var, thus allowing Java to infer which type list is - it's able to do this because the variable is initialized when it's defined. Note, you'll have to infer the type of the list when initializing (in the case above, you'd add String to new ArrayList<String>();). Thus, Java still keeps its static type declaration.

What is the difference to Kotlin?

The type of a variable in Kotlin defined with var is also inferred (but can also be explicit in case of var foo: Int), and Kotlin is also a statically typed language.

Kristin
  • 1,336
  • 7
  • 23
  • "Supposedly none." Supposed by whom? The meaning is completely different. "The type of a variable in Kotlin defined with var is also inferred" No, it can be inferred or not. – Alexey Romanov Mar 29 '18 at 06:56
  • 1
    In context of the question, where `var foo = 3`, it is inferred. I did not say it couldn't be explicit. I've removed "supposedly", poor choice of word, I guess. – Kristin Mar 29 '18 at 07:58
  • 3
    +1 for the laziness! Some programmers tend to stop thinking and instead use new features everywhere, believing they will surely help. They use var instead of int, believing they are faster... – Droidum Mar 29 '18 at 08:17
  • 1
    @Droidum True, but it seems Brian Goetz, Java Language Architect at Oracle, disagrees, and thought it was appropriate to rephrase my answer to his liking. – Kristin Mar 29 '18 at 13:41
  • 2
    wow I think it is quite inappropriate to falsify the contents of someone else's post! Who is he to do that? Really, the whole world complains about influencing people or even elections on social media - and everyone in this forum can freely modify other people's opinion to his needs? Come on! (sorry for the lots of edits, I am still not familiar with not pressing enter in comments) – Droidum Mar 30 '18 at 10:40
5
  • var in Java 10 can simplify some declaration like ArrayList<String> and stream's type.

  • var can only be used in a method for Java 10. But you can write it anywhere in Kotlin

  • And there is no val keyword in Java 10 then we could use final var as a val in Java 10.

(I think we all know mutable and immutable by default so I won't describe them.)

var is regarded as both a type(JB Lexer) and a keyword(JB Grammar Psi) in Intellij IDEA 2018.1+.

I've developed a plugin for Java 10 var type hints to show its type (just looks like Kotlin)

Sin
  • 316
  • 2
  • 4
  • 1
    This is not an answer to my question; You should state the difference of “var” between Java and Kotlin – gldanoob Mar 29 '18 at 10:26
  • Yeah, maybe I state nothing about difference...I'll improve this answer later. – Sin Mar 29 '18 at 12:51