Home » Java » Core Java

Core Java

Five Golden Backticks

Five Kinds of Strings Ever since Java 1.0, we’ve had string literals "like this". What other strings might we want? Other programming languages give us: Expression interpolation: s"I am ${age - 10} years old." Interpolation with formatting: f"Price: $price%8.2f" Strings with internal syntax that is checked at compile time: r"[0-9]+([.,][0-9]*)? or xml"<a href='http://java.sun.com'>The Java home page</a>" Raw strings in which ...

Read More »

Builder As A (Fail-Fast) State Machine

This is an idea that came to me a few weeks ago while designing a “Generator” class that had to send the input to an encapsulated Writer. It was, in fact, the Builder pattern. However, the rules were a bit more complex, the user had to call the add...() methods in a certain way, for the output to be generated ...

Read More »

Java 11: JOIN Tables, Get Java Streams

Ever wondered how you could turn joined database tables into a Java Stream? Read this short article and find out how it is done using the  Speedment Stream ORM. We will start with a Java 8 example and then look into the improvements with Java 11. Java 8 and JOINs Speedment allows dynamically JOIN:ed database tables to be consumed as standard ...

Read More »

Give Me a break, or: How to Make Awesome Puzzlers with Java 12

Java 12 provides, in experimental form, a switch expression and new forms of the switch and break statements. There is a profusion of new syntax and semantics for constructs that may find little use—except, of course, for authors of puzzlers and certification exam questions for whom this is a wonderful gift. If you enjoy Java puzzlers and would perhaps like ...

Read More »

Functional Java by Example | Part 6 – Functions as Parameters

This is part 6 of the series called “Functional Java by Example”. The example I’m evolving in each part of the series is some kind of “feed handler” which processes documents. In previous part we tried to make our functions as pure possible by moving as much of the side-effects, such as IO, to the outside of the system. Now ...

Read More »

In JDK 9 (and well 8) and above everything can be a stream

In JDK 8 we finally got to use streams and everything was good apart from the times when the API you used couldn’t produce a stream. Then you ended up writing a wrapper class method that allowed you to convert an iterator into a Stream, because you missed streams. public static <T> Stream<T> asStream(Iterator<T> it) { return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.IMMUTABLE | ...

Read More »

Java: Simulating various connection problems with Toxiproxy

Simulate various connection problems with Toxiproxy and Java’s HttpURLConnection to see what kind of errors get produced: connect timed out vs. read timed out vs. connection refused … . Results: System: openjdk 11.0.1 2018-10-16 (.setConnectTimeout 1) => java.net.SocketTimeoutException: connect timed out (.setReadTimeout 1) => javax.net.ssl.SSLProtocolException: Read timed out on HTTPS, java.net.SocketTimeoutException: Read timed out on HTTP (or Toxiproxy with 5s latency or timeout ) Nothing listening at the port => ...

Read More »

Migrating from Commons CLI to picocli

Apache Commons CLI, initially released in 2002, is perhaps the most widely used java command line parser, but its API shows its age. Applications looking for a modern approach with a minimum of boilerplate code may be interested in picocli. Why is it worth the trouble to migrate, and how do you migrate your Commons CLI-based application to picocli? Picocli ...

Read More »