-
1
-
-
-
-
Cheers,
Cay
Author of Core Java, Core Java for the Impatient and Scala for the Impatient
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Ranch Hand
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
Author of Core Java, Core Java for the Impatient and Scala for the Impatient
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.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
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.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. . . . .
Then again, I hardly ever use that feature even in Python. And everyone uses it in Python.
An IDE is no substitute for an Intelligent Developer.

Consider Paul's rocket mass heater. |