r/programming • • Nov 17 '20

New Java language features since JDK 8 to 15

https://advancedweb.hu/new-language-features-since-java-8-to-15/
36 Upvotes

17 comments sorted by

4

u/ragnese Nov 18 '20

I love the switch expression. But, on the other hand, it makes it kind of weird that other statements don't have an expression equivalent (e.g., if). I love expression-based languages.

I also love that it's getting records and sealed classes.

Kotlin really gave Java a good kick in the ass. Not that it wasn't improving before Kotlin's popularity, but Kotlin definitely helped accelerate it, IMO.

1

u/AttackOfTheThumbs Nov 17 '20

The new switch statement has advantages, but the fall through can be useful if the cases are only slightly different, where 1 does something and two does 1 and something more.

1

u/StillNoNumb Nov 18 '20

You can keep using the old switch statement fortunately. That said, for the particular case you mentioned this might be more preferable:

if (x == 2) {
  // something more
}
// do something

2

u/AttackOfTheThumbs Nov 18 '20

When you only have two options, yes, but there are sometimes cascading options where that's needed and imo the switch leaves it a bit prettier formatting wise.

-18

u/aullik Nov 17 '20

Honestly the only reason for new Java versions is to increase JVM capabilities. If the new switch expression doesn't bring anything new to the jvm its not that important, even worse, its actually confusing for those who still use java and for beginners who google the switch expression. If you wanna add this capability, name it differently!

I firmly believe that 90% of the work done on Java is completely unnecessary and not appreciated in the wild. Those who wanna be up to date more often than not use Kotlin. Those who wanna teach with java still use mostly java8 (this one is an assumption based on personal experience)

25

u/jherico Nov 17 '20

...Back then we wore an onion on our belt, which was the style at the time...

16

u/DrunkensteinsMonster Nov 17 '20

Is your serious opinion that new code is not written in Java? That could not be more incorrect.

Kotlin is nice however as more of these improvements make their way into Java, Kotlin on the JVM is starting to get squeezed out. Project Loom for example will make coroutines redundant on the JVM. It is possible that Kotlin on the JVM is as popular as it will ever be right now, and even that you seem to over estimate.

This is a very uninformed opinion.

-8

u/aullik Nov 17 '20

It is my serious opinion that there aren't many new projects being started on Java >8 and even less on Java > 11 with people actually using the new features.

There will always be a few but in general those who care for the latest and greatest (on the jvm) are not using Java.

With more and more features crawling into java it just gets more confusing. See the overloaded switch statement that is a very nice way to introduce bugs with people messing up the different versions. Now the Local Type Interference is cool but its not that important and implementing that cost oracle a lot of worktime/money. Time they could have used that to bring features to the jvm (and java) which in my opinion would have been the better call.

15

u/DrunkensteinsMonster Nov 17 '20

there aren't many new projects being started on Java >8 and even less on Java > 11 with people actually using the new features.

There is no reason to think this except for your own anecdotal experience. Plenty of new projects are started on these versions, I would wager more than are started with Kotlin at the very least.

See the overloaded switch statement that is a very nice way to introduce bugs with people messing up the different versions.

There is no new switch statement, the switch expression was introduced. Switches used as a statement will continue to work the same. This basic misunderstanding does not indicate an informed opinion.

Seems like you don’t like Java and don’t want to use it which is fine you should understand that your perspective is limited.

9

u/[deleted] Nov 17 '20

Yeah, i work for a Fortune 50 company. We primarily use java on the back-end and all our services are running in Java 11 or higher, some in 14 already. Its hard to find a solid number due to the size of the org, but we definitely have over 20k apps. Many are also implemented with WebFlux utilizing Reactive Streams with functional style programming. We use almost every feature that has been released post Java 8 in one place or another except Local Type Inference. Many are used in almost every app.

Specifically you chose to criticize the new switch expression claiming :

|"the new switch expression doesn't bring anything new to the jvm its not that |important, even worse, its actually confusing..."

public enum Day {

MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY

}

Flux.fromIterable(List.of(MONDAY, WEDNESDAY, FRIDAY, SUNDAY))

.take(3)

.map(day -> switch (day) {

case MONDAY -> ":(";

case TUESDAY, WEDNESDAY, THURSDAY -> ":|";

case FRIDAY -> ":)";

case SATURDAY, SUNDAY -> ":D";

})

.collect(Collectors.joining(" ** "));

you can't use a switch statement in this way, you'd have to add a bunch of less readable boilerplate that you can do away with when switch is an expression. Can it still be done either way, sure, but one is less code, less typing, less reading. Don't forget this is just a toy example. Imagine that enum actually being something like say the list of all the attributes on a product the company sells (we have over 1k attributes) or maybe its a list of taxonomy concepts associated with a product and you want to do something different based on matching search terms for those. I can tell you, having a huge block of if statements doesn't make it easier to read, write, or understand. Saying nobody uses these or writes code past Java 8 shows how small your world is. I haven't used Java 8 or under at work for like 2 years now. The only reason i still have Java 8 installed on my machine is so when we stumble on an app that hasn't been upgraded to 11+ i can just check it out, spin it up and upgrade it to get it off that legacy crap and modernize it with some non-blocking IO hotness.

1

u/aullik Nov 18 '20

I'm criticizing that the switch expression has the same keywords as the switch statement which makes it hard for beginners. Why not call it match as in scala or when like in kotlin? Why overload the keyword?

3

u/DrunkensteinsMonster Nov 18 '20

Because adding new keywords is not always backwards compatible and switch expressions have are a well established fixture of a lot of mainstream languages. C# has it, for instance.

0

u/nutrecht Nov 18 '20

It is my serious opinion

Based on what? You can't just have an "opinion" on statistics you know.

With more and more features crawling into java it just gets more confusing.

If the new features are 'confusing' for a Java dev I'd rather not work with them.

Time they could have used that to bring features to the jvm (and java) which in my opinion would have been the better call.

Such as?

0

u/[deleted] Nov 17 '20

[deleted]

9

u/DrunkensteinsMonster Nov 17 '20

I disagree with everything you just said.

Their point is that quite a bit of new code will either be in old Java versions or written by people who only know old Java.

You could say this about any language. There will always be people that don’t adjust to the new changes, or do not upgrade until forced. Java is not special here

I am inclined to agree that those people tend to pick other languages than Java because of it taking 6 months to get a new version, and then 1 or 2 more versions for the feature to leave previews.

6 months is a pretty rapid release cadence for a language. Do you really expect a language to be adding features any more rapidly than that?

It is just more convenient to not use Java in a lot of cases, and in cases where it's not, you'll probably not be updating Java.

Maybe to you, however to a great many companies and individuals Java is, in fact, the most convenient choice for applications that are expected to last years. Maybe in your personal experience people don’t upgrade, but again, that happens with every language that people actually use for real work.

None of the points brought up here support the notion that working on Java language features is a waste of effort somehow, which was the whole point of the OPs post. It’s a pretty insulting insinuation for the people that work on it and it’s wildly misinformed.

0

u/[deleted] Nov 17 '20

[deleted]

3

u/BoyRobot777 Nov 18 '20

I think what the OP was talking about is that a lot of these new features are conveniences via language design

Virtual Threads (Project Loom) will change how Java community treats threads. No more pooling. No more Reactive stacks. They all become obsolete the next day.

Records + withers will change how we represent our data. No more mutable getters/setters and cancerous Lombok. Immutable data will allow to write concurrent programs more easily.

Pattern Matching is a more niche feature, but with records they will create algebraic data type which have their place and uses.

Thus I think it's not only conveniences, they are shaping how we'll write tomorrows Java.

-3

u/aullik Nov 17 '20

This is basically what i said. Now I might have worded it a bit to strongly but you got the gist of it.

2

u/nutrecht Nov 18 '20

its actually confusing for those who still use java and for beginners who google the switch expression.

Yeah, let's not let these people be the deciding factor on what does and doesn't get added to a language.

this one is an assumption based on personal experience

Well on personal experience as an independent Java contractor you're wrong on pretty much everything you said outside of Kotlin being popular.