r/programming • • Mar 09 '16

var keyword proposed for Java

http://openjdk.java.net/jeps/286
250 Upvotes

395 comments sorted by

View all comments

Show parent comments

4

u/depressiown Mar 09 '16 edited Mar 09 '16

Ignore the fact that I used the interface instead of the implementation class on the left side. It's just good practice since you abstract the implementation class, but sometimes won't work if the implementation class has some implementation-specific method you need. Either way, not relevant when discussing the diamond.

The diamond (<>) can be used if Java can infer the generic type of the implementation class (the right side of the expression). This is "new" in Java 7. For example...

List<String> foo = new ArrayList<String>();

... is exactly the same as:

List<String> foo = new ArrayList<>();

Since Java 7, it can infer the generic type on the right side is String. However, if Java can't infer it, it won't compile. For example, this method will not compile:

public List<String> foo() {
    return new ArrayList<>();
}

There is no left side. Java can't infer the generic type of "new ArrayList<>()". In this case, you would need ArrayList<String>(). Hopefully that makes a bit more sense about the diamond. You can Google it if I'm not explaining it very well.

My larger point is that right now, the diamond allows Java to infer the generic type. If we use something like var, that inference is gone and now you have to define the generic type on the right side. While you gain not having to define any type on the left, you now lose the ability to shortcut the generic type on the right side. Overall, it's a positive gain for number of characters, but it's not as large as people make it out to be for objects with generics... and to some, there's a loss of readability by not having the explicit type on the left.

3

u/s888marks Mar 10 '16
public List<String> foo() {
    return new ArrayList<>();
}

This compiles and works fine in 7 and beyond. The left-hand side is the return type of the method.

1

u/four024490502 Mar 09 '16

Thanks for taking the time to explain the diamond operator.

I guess my pedantry about declaring foo as an interface vs. the implementing class was about my understanding of the var keyword in C#, and my assumption about it operating similarly in Java. In C#, var becomes an instance of the most specific type determined by the right of the equals sign, so

var foo = new ArrayList<String>();

doesn't compile to the same thing as

List<String> foo = new ArrayList<>();

assuming Java implements the var keyword in a similar manner to C#.

1

u/depressiown Mar 09 '16

Not being familiar with C# at all, what does a method signature look like of something you pass this var to?

public void foo(var bar) { ... }

... or...

public void foo(ArrayList<String> bar) { ... }

I'd hope the former. Otherwise, what a nightmare that seems...

1

u/four024490502 Mar 10 '16

The former won't compile. You need to explicitly specify the type of a parameter in a method signature.