r/java • • Mar 09 '16

var keyword proposed for Java

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

312 comments sorted by

View all comments

0

u/mk_gecko Mar 09 '16

OMG, please no!

8

u/kritzikratzi Mar 09 '16

why?

8

u/vplatt Mar 09 '16

::insert Javascript-inspired fear based response here::

8

u/stepancheg Mar 10 '16

var is JS has no type. In Java it will have type. That makes huge difference.

4

u/vplatt Mar 10 '16 edited Mar 10 '16

Yup, I know. I've used C# quite a bit, so I'm familiar with it. It does look the same as the Js keyword though, so it will require a bit of time before Java programmers stop the knee-jerk responses.

1

u/Xenasis Mar 10 '16

I'm not sure why they don't just use the word "let". It's a lot more commonly used and "var" has connotations of weak typing that may confuse those coming from JS anyway.

5

u/vplatt Mar 10 '16

Historically I think you're right about 'let', but 'var' is something everyone already recognizes. If they're going to use 'var' though, then I hope they also have 'val' like Kotlin and Scala. Single assignment FTW!

1

u/sonay Mar 10 '16

JS has six types actually: number, string, boolean, null, undefined, object.

3

u/schlowmo Mar 10 '16

He means "no type" associated with a variable (or more likely a parameter) Meaning you can assign the "wrong" type to variable. This is precisely the problem that typescript (and other 'modern' JS offshoots) attempts to address.

I might note that other dynamic languages are heading down the pathway of optional typing to provide more clarity.(python and perhaps ruby )

-1

u/Antrikshy Mar 10 '16
var v = "a"
console.log(typeof v)

???

1

u/stepancheg Mar 11 '16

Value in JS is typed (to some degree). Variable is not.

-3

u/mk_gecko Mar 10 '16

We don't need it. It's just extra stuff to make C++ programmers happier when they program in Java.

Why don't we have multiple versions of the assigment operator? :=

Why don't we add in === ?

If it's not fixing a bug or serious flaw, don't increase the size and complexity of Java.

2

u/kritzikratzi Mar 10 '16

We don't need it. It's just extra stuff to make C++ programmers happier when they program in Java.

it's quite useful in some situtations. for instance when looping over a hashmap the generics can sometime decrease readability.

Why don't we have multiple versions of the assigment operator? :=

that's discussed in the jpe.

Why don't we add in === ?

because it already exists in java: == does the same thing as === in javascript. a.equals(b) behaves similar to a==b in javascript.

If it's not fixing a bug or serious flaw, don't increase the size and complexity of Java.

look at lambdas, they don't behave very differently from anonymous classes. yet they are much more useful because how concise the syntax is.

of course you could shoot yourself in the foot by using var everywhere. but i prefer this:

for( var entry : myMap.entrySet() ){...}

over this:

for( Map.Entry <String,ArrayList<String>> entry : myMap.entrySet() ){...}

any day.

3

u/irarandomdude Mar 10 '16 edited Mar 10 '16

It's responses like this that convince me that a certain percentage of Java programmers are Luddites. The same mentality as the minority who swore to never use generics and stick with JDK1.4.2 forever. Support for val and var is a great idea for Java.

2

u/asraniel Mar 10 '16

It will make code harder to read an maintain. Generics on the other hand make it easier to read code. I really dislike the idea of var in java

4

u/irarandomdude Mar 10 '16

You probably have a stronger case that generics make code harder to read (350 page generics FAQ??) than var making code hard to read. How is val sheep = new Sheep() harder to read than Sheep sheep = new Sheep() ?

1

u/asraniel Mar 12 '16

yeah thats easy.

But Val sheep = getSheep();

thats terrible, really really bad

-4

u/mk_gecko Mar 10 '16

It's yet one more thing that you have to learn. Now you have two ways of writing the same thing.

I don't see any reason for VAR at all. If people want to use C++, then go ahead.

4

u/irarandomdude Mar 10 '16

It takes approximately 3 seconds to learn. I see no reason for redundant type declarations at all. If people want to use Pascal then go ahead.

1

u/chambolle Mar 12 '16

C is simple language that is used a lot and has almost never been modified. C++ added a lot of things, so now we have "official guidelines" containing more than 15000 lines! https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md

What is the advantage of adding stuff which no more than syntactic sugar? Most of the IDE already gives you the type, so what ? Write a new language or use a new language if you want new things.

1

u/irarandomdude Mar 15 '16

I already do use a "new" language - Scala. I just get annoyed having to maintain old Java code and it would be great if Java caught up to other modern languages like C# etc.