My travels with Modern Java in Action

As one of my New Year resolutions is to read more tech I’ve decided on an attempt to document my travels in Mannings Modern Java in Action

I have just started and currently reading the introductory sections which are generic. The book will take me through whats new in Java 8, 9, 10 & 11 with some of the main topics being lambdas, streams, functional & reactive programming.

Java release timeline relevant to the book.

  • Java 5 2004
  • Java 8 2014
  • Java 9 2017
  • Java 10 2018 (March)
  • Java 11 2018 (September)

As you can see, Java was slow to evolve between Java 5 and 8, a whole 10 years slow. So I guess I got lazy.
Java 8 is the first version of Java with functional programming capabilities.
As of today Java is almost at version 16. Oracle have been on a 6 month release cycle since Java 9. This means the Java community will have to keep up with the pace. This may also inject life back into the old language.

I have high expectations for this book and it has been given positive reviews. There is a lot of material.
With 2 years working experience already in Java 8 I should have a solid foundation to follow what this book will cover. I am hoping to fill in gaps and maybe deep dive in some areas.

So, let’s see how it goes.

6 Likes

Corresponding tweet for this thread:

Share link for this tweet.

Nice one Finner!

I am sure your journal is going to provide interesting for a lot of people and I look forward to seeing your updates :nerd_face:

2 Likes

Slap bang in the middle of Chapter 5 already which talks about the Stream API and lovin’ it …
… but I’m getting ahead of myself.

The big change for long time Java programmers is the jump to Java 8. Quite literally it is the ability switch from coding in OO to coding in FP. When written well, Java8 code looks quite different to its previous versions.
Here is a very simple example.

Pre Java 8

Using a for loop to iterate a list of String objects.
This is known as external iteration.

public String findWordInList(String wordToFind, List<String> words) {
   for (String word: words) {
       if (word.equals(wordToFind)) {
          return word; 
       }
   }
   return "";
}

Java 8

The same as above but using filter() & findFirst() from the StreamAPI and a lambda predicate. This is known as internal iteration.

public String findWordInList(String word, List<String>) {
   return words.stream()
      .filter(w -> w.equals(wordToFind))
      .findFirst()
      .orElse("");
}

The advantage of internal iteration is that the Stream library can choose a data representation and implementation of parallelism.

To use parallelism you just need to rewrite your code as follows:

public String findWordInList(String word, List<String>) {
   return words.parallelStream()
      .filter(w -> w.equals(wordToFind))
      .findFirst()
      .orElse("");
}

When I see a for statement in code at work these days I experience something akin to being asked to believe that Harry Potter is 14 years old in Harry Potter and the Deathly Hollows.

to be continued …

3 Likes

Be careful to not alienate your work buddies if you radically change the way you code. In big projects it is usually a good idea to respect the programming style with which the project was developed, otherwise you could end up with a mix of OOP and FP.

That being said, it is useful to try new programming paradigms for personal projects or for new work projects on which you are the leader of the team.

2 Likes

Totally agree @iPaul , there has to be general consensus between the developers in which style to adopt. What I am seeing at the moment where I work is the heavy use of the Stream API when working with collections. This gives the developer an API similar to FP patterns, such as map, flatMap, filter & reduce. This feels like a natural step towards a functional style without diving in at the deep end.
In my case, the project has 4 teams, 3 of which are external vendors and it is our team who are behind the curve and struggling with the new concepts. Adapting to the other teams code styles has been a struggle for us but already, for me at least, it has helped me a great deal. You have to swallow your pride when you do not know what you do not know.
So coming back to your point, we are actually the alienated team. We are still trying to catch up but at least now I know what I want my code to look like when I grow up :smiley:

But just to clarify, Java 8 is still fundamentally an OO language with some FP features. We are still writing and thinking in OO. But I am hoping with all these new features and practice I will be able to revisit FP languages like Clojure or Scala with more confidence and a deeper appreciation for the mindset.
One must be able to dream …

3 Likes

I have to say Manning’s Modern Java in Action is very good. It’s a perfect all-in-one for anyone looking to learn the new versions (at least up to Java 11). There are quizzes throughout chapters to help learn concepts, but the solutions are directly below the questions which makes it hard not to sneak peek :shushing_face: . But so far I’m very impressed with this book.
Moving on to Collections in the next chapter …

2 Likes

Finished reading Modern Java in Action !! Yeeeaaaahhhh for me :clap:

This is a very well written book. It’s obvious the authors have a lot of experience, not only with Java, but also with Design Patterns, JVM, Concurrency, Functional Programming as well as other JVM languages like Scala & Kotlin. Modern Java in Action will be open on my desk for quite a while to come.
Congratulations to the authors !!

There are a lot of topics covered in this book and it is heavy leaning towards Functional Programming.
However, for this entry I’ll just mention a few that come to mind right now.

  • Behaviour Parameterisation
    I’d never heard of this before but in a nutshell it’s the ability to let a method take multiple behaviours through parameters.
    Java 8 introduces lambdas (basically a type of higher order function) and by passing lambdas as parameters you get multiple behaviour.

  • Design Patterns
    There are a couple of sections dedicated to writing some of the classic Design Patterns using lambdas.
    I will be revisiting those sections sometime in the near future.

  • Java Module System (aka. Jigsaw Project)
    Java 9 is a “re-birth” I would say. The designers took the language and the JVM apart and put it back together in modules which means when writing applications in Java 9+ you can pick the modules you need to use from the language instead of being given everything in one JAR.
    Modules can expose APIs and hide implementations. This is definitely a feature I want to explore more. This is another big jump we Java developers will have to get our heads around. It’s all about design and architecture when using modules.

  • Functional Programming
    As I’ve mentioned before, Java 8 is the move towards FP programming and this book is heavily focused on all the features that give this functionality.
    With the Stream API, lambdas, Functional Interfaces and FP theory covering immutability, currying, recursion, pattern matching and the limitations Java currently has around some of these topics.

  • Reactive Programming
    Java 9 introduces the Flow API which is based on the Reactive Streams project. The Flow API is not an implementation but the main reactive libraries have been written against the Flow API. The discussion on reactive programming dives into the world of Threads, Concurrency and Parallelism.
    It’s another topic I will have to go back over again (and again, and again …).

There’s a lot more in this book. And I will try and add to this thread as I learn more.
. . .

2 Likes

execute-around pattern

This pattern is introduced early in the book while discussing behaviour parameterisation. It’s a pattern I had never heard of before.
You use this pattern when you want to implement common code but with slightly different behaviour… eh … duh! Right, well it’s basically the Template Pattern with functions (in Java a FunctionalInterface).

Let’s have a look in pseudocode :

String doThis(fn, o)  {
   alwaysDoThis(o)
   andThisAsWell(o)
   fn(o)      // this is the parameterised behaviour part
}

In Java it would look something like this:

public String doThis(Function<String,String> fn, SomeObject o) {
   alwaysDoThis(o);
   andThisAsWell(o);
   return fn.apply(o);   // apply() is the method on a Function interface
}

Unfortunately Java has to call a method on a FunctionalInterface so depending on the interface you pass in the parameter the code to execute the function would be different:

fn.apply(o);   // a Function interface declares apply()
fn.test(o);     // a Predicate interface declares test()
fn.accept(o);   // a Consumer interface declares accept()

So you would need to overload your method for the different types of functions you want to pass.

As far as I know in Scala, for example (and I’m sure in other languages), you can pass a function of a declared type and execute it by just adding its parenthesis.

def doThis(fn: (String) => String, SomeObject o): String {
      alwaysDoThis(o)
      andThisAsWell(o)
      fn(o)   // execute the function using parenthesis, nice!
}

I like this pattern though.

1 Like