I've just realised that Java 8 is already 3 years old! For a general-purpose language version, this happens to be a lot.
What's even more striking is that there are still a lot of projects that haven't been migrated to the newest (but already mature) version. There are in fact many projects that still use first "real" JRE, that is version 5 which introduced generics, enums, autoboxing, improved concurrency and all of the stuff nowadays recognised as a minimum standard.
Why is that? Well, sometimes is because cost of migration, especially for big, monolithic systems, is deemed to large. Sometimes that may be true. However, often the cost is overestimated or the benefits are underestimated. Therefore in this short post I'd like to present some Java 8 features that in my opinion can really bring substantial benefits and at the same time provide good arguments to justify the migration:
Java 8 brings a lot. Really, in my opinion it's the first "REAL" update since version 5.0. There is much more than mentioned in this short post, I encourage everyone to go and try (and then try to persuade the management to migrate projects as well...).
What's even more striking is that there are still a lot of projects that haven't been migrated to the newest (but already mature) version. There are in fact many projects that still use first "real" JRE, that is version 5 which introduced generics, enums, autoboxing, improved concurrency and all of the stuff nowadays recognised as a minimum standard.
Why is that? Well, sometimes is because cost of migration, especially for big, monolithic systems, is deemed to large. Sometimes that may be true. However, often the cost is overestimated or the benefits are underestimated. Therefore in this short post I'd like to present some Java 8 features that in my opinion can really bring substantial benefits and at the same time provide good arguments to justify the migration:
1. Lambdas aka functional programming for the masses.
Yes, we have Guava's Function. Yes, it's a bit of functional programming. But let's just compare this:
and this:
How cool is that ? And it's only a beginning! Java 8 added a real functional programming API, as advanced as it can be taking into account object-oriented nature of the language and dreaded "backwards compatibility".
In order to fit some functional programming into Java, following concepts had to be introduced:
- functional interfaces - basically a "type" for a "function"
- new "->" notation, with following syntax rules:
- types of the parameters are optional
- parentheses around the parameter are optional if you have only one parameter
- curly braces are optional (unless multiple statements are present)
- “return” keyword is optional in case of a single expression that returns a value
- method references (with "::" operator)
It's really a lot of fun and a lot less of a boilerplate code. You simply need to try it for yourself :-)
2. Streams and parallel collections
Basically streams (implementing Stream interface, who would guess?) are Iterators on steroids. Why on steroids? Because streams support:
- out of the box parallel execution!
- map / filter / reduce pattern
- functional "iteration" as old friend - forEach method
- primitive (Int / Double / Long) counterparts for even greater performance
Map / filter / reduce together with lambdas effectively eradicates the need for Guava's FluentIterable and consortes. Sorry! The standard library always wins!
Built-in parallelization and lazy evaluation brings the need to collect, join, group or partition. This is also provided. Nice! Just take a look at this example - joining User names:
3. Joda (well, almost) date / time
The only thing that can be said is at last! LocalTime, LocalDate, LocalDateTime and ZonedDateTime along with a reasonable API (copied from Joda :-) ). Plus fast-and-easy creation (now(), toInstant()). Especially the last one is nice, allowing for a fast migration:
I wish this had been added years ago!
4. Misc (Optional, Base64 and others)
- Everyone knows (or at least should know) the Optional class. Now available in the standard library!
- Everyone had at least once a need to use Base64. Now available in the standard library as well!
- Tuples! At last!
- No more Permanent Generation (one JVM parameter less, heh)
Java 8 brings a lot. Really, in my opinion it's the first "REAL" update since version 5.0. There is much more than mentioned in this short post, I encourage everyone to go and try (and then try to persuade the management to migrate projects as well...).
2 komentarze:
Hej, listing z kodem są zupełnie nieczytelne na Chrome/Win: tekst jest szary - pewnie coś nie tak z CSS..
Faktycznie, dzięki za zwrócenie uwagi.
Prześlij komentarz