Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java SE 9 for the Impatient: What is your favorite feature in Java 9?  RSS feed

 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Cay,

While writing this book, which topic you enjoyed most? Which one is your favorite feature and why?

Regards
Javin
 
author
Ranch Hand
Posts: 232
27
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JShell is my favorite feature. And InputStream.readAllBytes and List.of/Set.of/Map.of. My least favorite feature? Modules. The kale of programming :-)

Cheers,

Cay
 
Javin Paul
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but no surprise there :-) Though, I haven't figure out how to take full advantage of JShell ... Maybe your book can help there.
 
Sheriff
Posts: 57931
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try jshell oiut and find what it does and what it doesn't do. That will teach you what it is good for.
 
Saloon Keeper
Posts: 8494
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some new Java 9 features I think are really cool and useful, even if they might be small or specialized:

  • Factory methods for collections.
  • Private instance methods on interfaces.
  • Diamond operator works when defining anonymous classes.
  • Effectively final variables can be used in try-with-resources.
  • Stream.takeWhile(), Stream.dropWhile(), Optional.ifPresentOrElse(), Optional.or().
  • Javadoc now supports HTML5 tags.
  •  
    Cay Horstmann
    author
    Ranch Hand
    Posts: 232
    27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I agree with the collection factory methods. And Optional.ifPresentOrElse(). (What a terrible name, though.)

    With many of the other items on your list, I had a tough time coming up with exciting examples. Take private instance methods on interfaces. Sure, why not? But try writing a plausible example. You need multiple default or static methods that want to share something that's worth putting into a method (but that can't access fields, only call other methods).

    Or the diamond operator with anonymous classes. Nowadays, how often do you have an anonymous class? Lambda expressions are almost always simpler. So, you need to override a generic class or a generic interface that's not a functional interface. Of course I can come up with an example, but not with one that is so memorable that the reader says "gotta have that feature". Truth be told, I never knew you couldn't use diamond with anonymous classes, because it never came up in my work.

    Or take effectively final variables in try-with-resources. I tried



    It's ok, but it's a little weird to give a writer to a method that then closes it. Definitely something that one would want to document carefully.

    Or Stream.takeWhile/dropWhile. I dutifully present them, but I have never used them in practice, even though I use streams a lot. It just doesn't happen that much that I am interested in stream value until something terrible happens, and then I no longer care. I'd love a really compelling use case.

    I am sure Optional.or will at some point find good uses when we all use more optionals. And it has a nice name. But explain it to someone in an elevator. Ok, you have two Optional. The or method produces the second if the first one is empty. Except, not quite. The second Optional is computed lazily. Ok?

    I have a long list of features like these where I have no truly compelling use case. Every once in a while, I find one, put it into the next edition of the books, and cross it out.

    Now contrast that with InputStream.readAllBytes. Of course that's something you want to do. And I can go on to show you how to read a text file into a string:



    Cheers,

    Cay

     
    Stephan van Hulst
    Saloon Keeper
    Posts: 8494
    155
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Cay Horstmann wrote:You need multiple default or static methods that want to share something that's worth putting into a method (but that can't access fields, only call other methods).


    I don't agree with this premise. Let's say that a default method can perform a large chunk or complex logic. Instead of writing a 50 line method, I rather write a 5-10 line method that calls private instance methods that are responsible for a smaller self-contained piece of logic, even if they're not called by any other methods. That way the original method becomes self-documenting and more easy to understand.

    Nowadays, how often do you have an anonymous class? Lambda expressions are almost always simpler.


    I agree with you, but I remember I've often had to use them when implementing certain event handlers. Like the other points I've made, I don't think they merit (great) dedication in your book, I just enjoy them for situations I've personally come across.

    Or Stream.takeWhile/dropWhile. I dutifully present them, but I have never used them in practice, even though I use streams a lot. It just doesn't happen that much that I am interested in stream value until something terrible happens, and then I no longer care.


    Not necessarily until something terrible happens. In functional languages like Haskell I've used them often, and I'm glad they made the translation to Java. You would use them when you know your source is sorted according to some property, and you're only interested in elements where some condition about that property holds.

    I am sure Optional.or will at some point find good uses when we all use more optionals. And it has a nice name. But explain it to someone in an elevator.


    I didn't know about the existence of this one, until I browsed the API exactly because I wondering if it had something like this. It's quite specialized and I don't expect that it will be used a lot, but in one instance I used it, it cut the amount of code I had to write significantly, and made it more easy to read.

    I have a long list of features like these where I have no truly compelling use case. Every once in a while, I find one, put it into the next edition of the books, and cross it out.


    It's true that these are more specialized features that might not immediately be interesting to your readers. If I think of good examples, I will contact you.
     
    Campbell Ritchie
    Sheriff
    Posts: 57931
    178
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:. . . .

    Or Stream.takeWhile/dropWhile. . . .


    Not necessarily until something terrible happens. In functional languages like Haskell I've used them often, and I'm glad they made the translation to Java. You would use them when you know your source is sorted according to some property, and you're only interested in elements where some condition about that property holds. . . . .

    It isn't the sort of thing you use all the time. It is very useful if you want to start reading from the first negative number, or something like that. Very useful, but by no means a common use case. If you use keyboard input with something like Scanner#tokens, you can use a sentinel value to stop input.
     
    Bartender
    Posts: 19221
    85
    Android Eclipse IDE Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Odd that you're so fond of JShell. One of the arguments against it was that Java isn't really a one-shot language, it's a language that operates in the context of classes and multiple statements, which can be a bit awkard in a compile/process/go loop.

    Then again, I hardly ever use that feature even in Python. And everyone uses it in Python.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!