How to Build Java Applications Today: March 8, 2021

I'm a start-up CTO, my latest LJC talk video & "Making of" are here, avoiding NPEs, Project Valhalla inline types, GraalVM native images, RedMonk rankings Q1/2021, and Scala 3.0.

README

This is issue #26 of my weekly newsletter “How To Build Java Applications Today” - for Java developers, by a Java developer.

Well, it’s official now: For the last year, I didn’t just work on a project here in the U.K. I’m the CTO of a start-up called “Your Home in Good Hands”! We build an app for businesses who visit their customer’s homes. The app isn’t ready yet, and the logo is still a placeholder. But you can always - wait for it - sign up for another newsletter from me!

Quote of the Week

In science, if you know what you’re doing, you should not be doing it. In engineering, if you don’t know what you’re doing, you should not be doing it.

Mathematician Richard Hamming, quoted in Tom Vanderbilt’s book “Beginners: The Joy and Transformative Power of Lifelong Learning” (heard in the “No Stupid Questions” podcast, episode 39, at 24:23)

Video & “Making of” for My LJC Talk “Front-Ends for Java Developers”

On Thursday, February 25, 2021, I presented “How Should Java Developers Build Front-Ends for Web, Mobile & Desktop Today?” to the London Java Community (LJC). In my 41-minute talk, I looked at various frameworks from a Java developer's perspective and suggested which toolkits to use in typical scenarios. I then answered questions for ten minutes.

The video for that talk is now available. As is a “Making Of”, which I documented for the very first time: I look at things that didn’t work in this talk.

Talk page

Bookmarks

Java

How to Avoid & Deal with NullPointerExceptions

We all know them in Java, and we all hate them: NullPointerExceptions. Java 15 makes it easier to find the cause. But we could still do without them! So how do we do that? This article tells us!

Some of the things I already knew. Like “Don’t call toString() on something that could be null”, or “Call equals() on the object that is not null for sure”. Some things I didn’t. Or did you know that Objects.toString(Object, String) calls toString() on the first parameter if it’s not null and returns the second parameter otherwise? Nifty!

Blog post

Faster Java with Project Valhalla and Inline Types

I thought the “flattened data types” of Project Valhalla are the same “immutable value classes” I use with Project Lombok today already: @Value class Point { Integer x; Integer y; } Here Lombok makes all fields both private final and creates the constructor, the getters, toString()equals() and hashCode().

But the purpose in Valhalla is something different: To turn multiple objects into one flat object. In my example above, I got three instances: Point and the two Integer instances. With Valhalla, we just get a single Point instance. Why? So that all the data of Point can fit into CPU registers. That’s important because it’s much faster: It takes 200 - 330 times as long to read Java instances from RAM than reading them from CPU registers. And if Java needs to load instances from an SSD, it’ll take 167,000 times as long!

InfoQ talk

Not for the Faint of Heart: GraalVM Native Images

GraalVM native images make Java programs use much less memory and start much faster. Brilliant! But the road there can be a thorny one. Let’s say you want to create GraalVM native images without support from a framework like Quarkus. How hard can that be? Very hard, as this live-coding podcast episode demonstrates.

The main obstacle is reflection. Our Java programs use it a lot, but GraalVM needs to (mostly?) eliminate it for native images. Two things surprised me there:

  • Logging frameworks like SL4J don’t work because they use reflection at runtime to find loggers. Now there’s just a handful of logging frameworks out there in Java land. And GraalVM could substitute some methods to have them log to standard out. But GraalVM does nothing to fix this, and logging simply fails.

  • Libraries can help GraalVM by putting configuration files inside the new META-INF/native-image/ directory in the JAR file. But even well-known ones like Apache HttpComponents don’t, leaving the fight with GraalVM up to us developers. Just wonderful! 😒

So my advice to you: Don’t build GraalVM native images unless your framework supports you! And call me when Spring Boot 3.0 gives us GraalVM native images out of the box.

Podcast episode

Over the Fence

RedMonk Programming Language Rankings Q1/2021

In the TIOBE index, Java is still ahead of Python by a hair. In the RedMonk ranking, Python is already second. Who’s number one here? JavaScript - which only places 7th in the TIOBE index (C leads there). Where’s that difference coming from?

RedMonk only looks at programming language usage on Stack Overflow and GitHub. It believes this to be “predictive of future use”. At the very least, it’s “predictive of future discussions”, like this one. Well, more like “future gossip”, really.

Anyhow. Is there any consolation for us Java guys? Yes: Kotlin climbed one place to #18, while Go (Google’s “better C++/Java” drops one place to #16. RedMonk calls Go “at best static and arguably on a decline path“. Welcome to the club, Go! Ruby even drops two spots to #9. Looks like Ruby on Rails isn’t enough to sustain Ruby in an age of mobile apps and Single Page Applications.

Blog post

The Road to Scala 3.0

Talking about programming languages with downward trends: Scala 3.0 is soon upon us. Ouch, that was harsh. In my defense, there was a time when I thought that Scala could be the heir to the Java throne. But we now know that Scala's functional programming isn’t every Java developer’s cup of tea. Or cup of coffee, for that matter. And then came Kotlin, with the power of Google and Android behind it, and swept up most of those looking for a Java alternative on the JVM.

Now Scala still tries and gives us better enums and faster programs through improved inlining in version 3.0. But I don’t know about leaving out parentheses in control expressions. To me, the new if i < 3 is worse than the good old if (i < 3): It’s harder to read. And we all know that code is mostly read by other developers and occasionally executed by computers! That’s a smart thing to say, so you know it isn’t from me. I think I heard it on the most recent episode of the Stack Overflow podcast.

InfoQ article

About

Karsten Silz is the author of this newsletter. He has been a Java developer for 22 years.

Karsten grew up in East Germany, behind the Iron Curtain. He wrote his first BASIC program on a Sinclair Spectrum ZX81 in 1984 and never looked back. He got a Master's degree in Computer Science from the Dresden University of Technology in 1996.

Karsten has developed software in five European countries and the U.S. since 1996. In 2004, he co-founded a software product start-up in the U.S. The main product went from 0% to 80% market share in the North American cable industry. Karsten led product development for 13 years and left the company after it was sold successfully.

Since 2017, Karsten has been a contract developer in Germany and the U.K. In 2020, Karsten co-founded another start-up in the U.K. as CTO. There he builds an app for businesses who visit their customer’s homes.

Karsten has this newsletter, a developer website, and a contractor site. He's on LinkedInTwitter, and GitHub.