• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java 8 - the good, the bad and the ugly  RSS feed

 
Rob Spoor
Sheriff
Posts: 21050
85
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I've finally persuaded myself to upgrade to Java 8, and there are great things about it and not so great things. I thought I'd share my experience.

The good:

- Lambda's, definitely. You can do the same with a lot less code.

- java.time. Although I never had many problems with Date/Calendar, Joda-Time was always a better implementation, and java.time has taken Joda-Time and made it even better. There are still some things I need to figure out (like how to convert a LocalDate into an Instant), but I'll definitely use it more and more in the future.

- default and static methods in interfaces. No longer do we need abstract skeleton classes, nor utility classes (I'm looking at you, java.util.Collections and java.util.Objects!). Instead, most of it can go into the interface. (Actually, we may need utility classes like (package private) java.util.Comparables because interfaces still cannot contain non-public members, but those utility classes can remain package private instead of being part of the public API.)


The meh (a.k.a. I don't really care about it yet):

- Streams. I understand that it can make larger calculations a bit easier to write and read, but I haven't needed them yet.


The bad / ugly:

- Lambda serialization. Sure, it's very easy to create a serializable lambda, but that it requires some verbose coding again - you need to explicitly cast to both the functional interface and Serializable. Also, this doesn't propagate properly. For instance:

- Support from tools. Two examples:
-- The latest version of CheckStyle fails horribly when a lambda or default method is encountered (not sure about static methods in interfaces). I get an "EOF expected" error and CheckStyle stops checking the remainder of the source file. See https://github.com/checkstyle/checkstyle/issues/10 for more information. For proper support we will have to wait a "few month"!

-- Javadoc is messed up badly. First of all, doclint... This is turned on by default, which will cause Javadoc errors if your Javadoc comments contain invalid HTML (like &). You have to explicitly turn this off if your code contains anything that doclint doesn't like. It even complained about background colours belonging in CSS. (It's right, but come on, this is Javadoc. I just want table rows to alternate in colours.)
Now I can live with that by adding <additionalparam>-Xdoclint:none</additionalparam> to my pom.xml. What's worse, a lot worse, is that automatic linking does not work at all. I have a few links in my Javadoc generation (like to JEE, <link>http://docs.oracle.com/javaee/7/api/</link>), but all of them give the same error: Error fetching link. They aren't obscure libraries - JEE, Jackson, JDOM, stuff like that. They all fail, but I can open the exact same URLs in my browser just fine (no proxy setup). Javadoc is able to find the links if I add package-list to the URL (e.g. http://docs.oracle.com/javaee/7/api/package-list) but then every created hyperlink includes the package-list (e.g. http://docs.oracle.com/javaee/7/api/package-list/javax/servlet/http/HttpServletRequest.html instead of http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletRequest.html). This has been acknowledged but we of course have to wait for at least one or two releases later. If the release schedule is adhered to that means 3 months of either broken or missing Javadoc links. Great...


If anyone has other good/bad/ugly items, please share!
 
Rob Spoor
Sheriff
Posts: 21050
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing about the lambda serialization - this can be overcome by extending IntPredicate (and others), and providing new default implementations:
Now it works as expected:
It can still be cumbersome though:
If you miss either cast, then the end-result will not be serializable. Missing the first cast will use the old and method which creates a regular non-serializable IntPredicate, and missing the second cast will create a SerializableIntPredicate that still can't be serialized because it requires a non-serializable IntPredicate.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:- java.time. Although I never had many problems with Date/Calendar, Joda-Time was always a better implementation, and java.time has taken Joda-Time and made it even better.

I would be very interested in the "even better" part. Do you have any examples?

Furthermore, how do you feel about converting existing projects from JodaTime to java.time? Is it worth it? Is it a lot of work?
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you want to convert projects which are already working at all, Martin?
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a project we continually develop. The project is fairly intensive in date-time operations. If the conversion was reasonably easy (such as just replacing import declarations in 99% of cases), I might be tempted to do it to avoid the terrible design flaw of JodaTime of interpreting null values as "now". We're far from adopting Java 8 yet, though.
 
Rob Spoor
Sheriff
Posts: 21050
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're now working with Joda-Time, it's probably not worth the effort to change all your existing code to java.time. But to answer your question, what I like most:
- DayOfWeek is an enum, not some int (like with java.util.Calendar).
- The Month enum can be used in addition to a (proper 1-based) int value.
- Parsing throws a proper DateTimeParseException instead of a generic IllegalArgumentException.
- The API looks cleaner to me. There are a lot less classes / interfaces while you can still do the same things.
 
Rob Spoor
Sheriff
Posts: 21050
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unrelated, another good thing - the Javadoc has "tabs" for the methods. Just take a look at http://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html. Go to the method overview, click on Static Methods, and you'll see only those.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like the conversion would not be a small task.

May I ask what is the java.time counterpart of JodaTime's Interval? Is there any at all?
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Unrelated, another good thing - the Javadoc has "tabs" for the methods.

Great way to organize the documentation. Love this.

 
Rob Spoor
Sheriff
Posts: 21050
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:Looks like the conversion would not be a small task.

May I ask what is the java.time counterpart of JodaTime's Interval? Is there any at all?

Doesn't look like it.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16007
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to have a serializable lambda?

A lambda expression is a stateless, anonymous method (stateless since it's based on a single-method interface, which cannot have member variables = no state). Since it's stateless, why would you want to make it Serializable? There's no state to serialize.

Tool support: IntelliJ has very good support for Java 8, in fact it has had support for Java 8 already since IntelliJ 12, more than a year ago.

I like streams a lot too. They work very nicely together with lambda expressions to make your code shorter and easier to understand, and help you to get rid of a lot of external iteration plumbing code.
 
Rob Spoor
Sheriff
Posts: 21050
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Why would you want to have a serializable lambda?

A lambda expression is a stateless, anonymous method (stateless since it's based on a single-method interface, which cannot have member variables = no state). Since it's stateless, why would you want to make it Serializable? There's no state to serialize.

I have a few serializable classes that use Predicates, Functions and Suppliers as part of their state. I still like using lambda's to provide those. That means that instances of those classes are no longer serializable though.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Martin Vajsar wrote:May I ask what is the java.time counterpart of JodaTime's Interval? Is there any at all?

Doesn't look like it.

That's most unfortunate in my opinion. The Interval is a very useful class, and I've seen some terrible implementations of this concept (such as specifying the last day/hour/minute/second that belongs to the interval as the "to" date, thus making the end date of the interval dependent of the desired resolution) that I'm wary of having the average Java 8 developer implement it on his own.
 
Stephen Bloch
Ranch Hand
Posts: 48
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I knew about the lambdas, and have been looking forward to them with bated breath for months. They won't make Java code as short and clean as Haskell code, or even Scheme code, but much shorter and cleaner than it has been. Yay!

I hadn't heard that you could now put static methods into an interface; I've been kvetching about that for YEARS. From a programmer's perspective, an "interface" should be a collection of method specifications that all implementing classes support, and there's no obvious reason static methods (e.g. pseudo-constructors) should be excluded from that. The implementation explanation, that interfaces are about run-time polymorphism and a static method isn't subject to run-time polymorphism, always struck me as missing the point. Also yay!
 
Rob Spoor
Sheriff
Posts: 21050
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javadoc is more messed up than I had already discovered. Besides not handling links to external APIs well, it seems to use Java 7 for the JSE API (although that could be a setting in my Maven project).
What's worse is the links to methods it creates. For example, instead of creating a link to http://docs.oracle.com/javase/7/docs/api/javax/swing/DefaultCellEditor.html#getTableCellEditorComponent(javax.swing.JTable, java.lang.Object, boolean, int, int), it replaces the (, , and ) with -, leading to http://docs.oracle.com/javase/7/docs/api/javax/swing/tree/TreeCellEditor.html?is-external=true#getTreeCellEditorComponent-javax.swing.JTree-java.lang.Object-boolean-boolean-boolean-int-, and that causes links to methods to fail (only the class itself is opened).
 
Paul Clapham
Sheriff
Posts: 22375
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just installed Java 8 on my computer, and now when I try to open a web page with an applet, I get a message which says my Java installation is out of date. When I accept the offer to install the latest version, it installs the latest version of Java 7. Which doesn't make the message go away.

Probably that situation will go away when Oracle deems Java 8 to be the "latest version".
 
Rob Spoor
Sheriff
Posts: 21050
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maven Javadoc plugin version 2.10 has been released last week, and it seems to have fixed all errors
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!