Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.
See all our reports

Java 8 Puzzlers: The Strange, the Bizarre, and the Wonderful with Baruch Sadogursky and Viktor Gamov

Join the worlds first 24 hour Virtual Java Conference for FREE! That’s right, 24 back-to-back virtual sessions from some of the greatest minds in Java. Every session is beamed directly to your couch, so sit back, relax and enjoy…in your underwear (just an option).

REGISTER FOR vJUG24!

vJUG24 starts at 7AM UTC on September 27th. Don’t miss it!


RebelLabs is the media partner for the Virtual JUG, the online Java User Group which brings you the best sessions by world class speakers. The best part? You don’t have to leave your house or office to enjoy them! You can educate yourself and become a better developer even from the comfort of your home!

This time I want to recap a magnificent session we had just recently: “Java 8 Puzzlers: The Strange, the Bizarre, and the Wonderful”. The speakers who brought it to our screens are two established experts in the Java community: Baruch Sadogursky and Viktor Gamov.

A couple of words about the speakers.

Baruch Sadogursky (a.k.a JBaruch everywhere on the internet) is the Developer Advocate at JFrog where he has made a profession of speaking about the company’s products: Artifactory, the Universal Artifact Repository; Bintray – the Universal Distribution Platform; Xray, Universal Component Analysis and Mission Control, Centralized Control and Management. For a living he hangs out with JFrog’s tech leaders, writes code around the JFrog Platform and its ecosystem, and then speaks and blogs about it all. He has been doing this for the last dozen years or so, and enjoys every minute of it. Baruch is @jbaruch on twitter and mostly blogs on http://www.jfrog.com/blog/ and http://blog.bintray.com. He is a professional conference speaker on DevOps, Java and Groovy topics, and is a regular at the industry’s most prestigious events including JavaOne, DockerCon, Devoxx, DevOps Days, OSCON, Qcon and many others.

Viktor Gamov is a Senior Solution Architect at Hazelcast, the leading open-source in-memory data grid. Viktor has comprehensive knowledge and expertise in enterprise application architecture leveraging open source technologies. He has helped leading organizations build low latency, scalable and highly available distributed systems. He holds an MS in Computer Science, co-organizes the Princeton JUG and New York Hazelcast User Group. He is a co-author of O’Reilly’s “Enterprise Web Development”. Viktor presents at various international conferences, blogging and producing a podcast. Follow Viktor on Twitter, he goes by @gamussa.

The session is available in full on the Virtual JUG’s Youtube channel. Oh, and do yourself a favor, watch it in full! You think you know everything there is about Java 8, lambdas, streams and so on, but you’d be pleasantly surprised/amazed at how many corner cases exist and how mind boggling a piece of Java 8 code can be, when experts put their minds to it.

Java 8 puzzlers

The session is obviously inspired by the amazing book by Joshua Bloch and Neal Gafter “Java Puzzlers“, which is already many years old and contains snippets of Java code that has unexpected behavior that often puzzles people, especially when they haven’t seen it before.

lg-puzzlers-cropped

The book is amazing. Progress hasn’t stopped there though, as we’ve recently witnessed the Java 8 release which brought developers a lot of sweet additions: lambdas, streams, more type inference and so on. New functionality means new corner cases and new puzzles.

Many of the puzzles in this session were created by Tagir Valeev, who has incredible knowledge of the mucky interior of the JVM, the peculiarities of the Java language. He is also the author of the StreamEx library that expands the capabilities of the default Java 8 streams. You’d be doing yourself a favor if you follow him.

Oh, incidentally, he’s also the author of HuntBugs, the static code analyzer, kinda like FindBugs, but specifically created to have the best support for Java 8. (One of the questions in the session was about finding a good analyzer and since we’re talking about Tagir, it seems reasonable to mention.)

The rules of the session were simple. Viktor and Baruch would present a puzzle definition and a choice of answers, discussing what the code might do. The audience would vote and then someone would explain the answer. The Virtual JUG was joined by the LJC (London JUG), so we had some audience for the speakers to interact with. Additionally, we had some lively voting in our IRC channel: #virtualjug at freenode, where anyone can follow the discussion during the live vJUG sessions.

Well, without further ado, let’s look at a couple of the puzzles. Please don’t spoil any solutions in the comments, it’s not the place. Don’t spoil the fun for other readers and other people who would come to listen to Baruch and Viktor next time.

Puzzle 0

puzzle0

What would the code on the image above print? Let me elaborate a bit, we have an ArrayList, we take a sublist, create a Stream from it, then add an element to the sublist we created the Stream from, and finally apply a System.out::println to each element of the Stream using the forEach method.

At the first sight, everything is shiny, and should work like as expected and print all the ingredients, right? But do you hear that thin voice of doubt in your head saying: “you’ve seen plenty of those ConcurrentModificationExceptions and we’re adding an element here. It’ll blow!”

Well, following the no-spoilers policy I will not provide the answer here and just encourage you to watch the session! It’s amazing amazing fun!

Puzzle 1

Let’s take a look at something easier, shall we? Here’s an one-liner:

puzzle1

What do you think will be the output? We take a stream consisting of some numbers, compute the maximum, then run the get().

How can you even think it’ll be anything else besides “3”, can’t Java get the basic arithmetic right? Let me just tell you that the voters in the IRC session were not so sure and the other answers were represented enough. But you’d have to watch the session, or at least this particular bit to learn the answer. (Yeah, yeah, I know that you can just take the code and run it, but where’s fun in that?)

Puzzle 2

This one takes quite a bit of the setup to reach the punchline, but it is worth it.

puzzle2

Imagine we have a local class that implements two interfaces with the distinct methods. Then we pass it into another method that takes an Object. So far everything is happy and good. The question is, however, how can we assign this object to a variable that will support calling both methods from the both interfaces directly.

There are 3 options meaningful options: use the intersection types, use the intersection type in generics, and use the intersection type inside an optional and map the code over it.

If you have a very vague idea of what will happen in every one of these cases and think you’d pick option D, you’re not alone. IRC was also confused:

im-confused-irc

However, have no fear! Viktor and Baruch explained all the puzzles and told all the secrets and solutions.

And don’t worry, chances are you’ll never have to look at this kind of monstrosity. But if you’re a curious developer, you should know your craft and your platform, its abilities and limitations. And you will definitely enjoy watching this session!

There are way more puzzles, more mind boggling code, more confusion, jokes and Java 8 knowledge in it!

Interview with the Virtual JUG speakers

After the session, Baruch and Viktor sat down with me to have a short chat which we call the RebelLabs interview with Virtual JUG speakers. During these interviews, we typically discuss the news in the Java community, the best resources to advance yourself as developers and other useful stuff.

This time, we have twice the insight and knowledge shared in the interview since the session had two speakers.

Click on the video below and enjoy listening to Viktor and Baruch a bit more. And if you have any questions you can find them on Twitter. Even if you don’t, find them and follow them, they know things! Baruch Sadogursky. Viktor Gamov.




Read next: