Running on Java 21-ea+24-2086 (Preview)
Home of The JavaSpecialists' Newsletter

3.19 Expression Lambdas

Juppies 2 by Dr Heinz M. Kabutz

Lambdas with a single statement do not need the curly braces and can be written as an "expression lambda". Note that our IDE has a thin line that indicates how wide the line should be. When I was a little boy, my father was studying at university and one of the classes he took was Fortran programming. We had all these printouts and punched-cards that we used to play with, pretending they were some secret access cards. Punched-cards were typically 80 characters wide, and this number has stuck through decades of coding. Even though I am using a 4k Dell monitor, the default line length is still set to 80 characters. This does seem to be a reasonable human limit. Make it much longer, and it becomes difficult to read. You might want to increase it 120 if your team agrees.

We replace the SORTED_SCRAMBLER statement lambda with an expression lambda.

In order to also change the RANDOM_SCRAMBLER to an expression lambda, we need to first inline the new ArrayShuffler() to where the shuffler variable is currently used. We can do that manually by selecting the new ArrayShuffler(), cutting it, and pasting it on top of the shuffler in the line underneath. We then delete the ArrayShuffler variable declaration.

When we change the layout of our code, we need to remember that this is very much a matter of preference. It is a good idea to be able to comfortably read a lot of different coding styles. When we work in someone else's code base, it is polite to use whatever coding style they used for their code.


When you load these comments, you'll be connected to Disqus. Privacy Statement.

Table of Contents

We hope you enjoyed this tutorial. If you did, you will also enjoy our courses. We suggest you start with Extreme Java - Advanced Java, followed by Extreme Java - Concurrency Performance for Java 8.

About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack '23

Superpack '23 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...

Java Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.