This one actually comes from their own youtube channel. In this short video from their series “Cracking the Java Coding Interview“, the host explains that “the ConcurrentModificationException that you get has in fact nothing to do with concurrency.”
Continue reading “Misnomer: ConcurrentModificationException”Author: Claude
TypeScript’s “readonly” is weird
Most of my posts on this blog are about Java but in recent years I worked a lot with TypeScript. I think the reason I never wrote about it is that most of the the weirdness comes from EcmaScript, which is so often weird that enough other people have written about it and TypeScript itself is actually quite well done in my opinion.
Continue reading “TypeScript’s “readonly” is weird”Do programmers count from zero?
How programmers count isn’t really that much different from how anyone else does it. But there are some misconceptions about counting and numbering.
Continue reading “Do programmers count from zero?”Don’t use Set.of().contains(). Use switch instead.
Set.of(a, b, c).contains(value) might seem clever and while it is easy to read it’s way slower than switch(x) {case a,b,c -> true}.
We often have to check if a value is one of a given set of values. But we don’t want to write code like this:
if (value == SomeEnum.FOO || value == SomeEnum.BAR || value == SomeEnum.QUX) { ...
Some use a Set and write it like this:
if (Set.of(SomeEnum.FOO, SomeEnum.BAR, SomeEnum.QUX).contains(value)) { ...
But now the runtime has to create a Set each time this line is run and it also has to call the method contains. And it can’t even handle null. In Java we have a much better alternative: The switch expression. You can use it like so:
switch (value) { case FOO, BAR, QUX -> { ... }; case null, default -> {}; }
@SafeVarargs and Heap Pollution
When should we use this annotation? How can we prevent heap pollution?
Continue reading “@SafeVarargs and Heap Pollution”No, ChatGPT can’t replace human programmers
ChatGPT and other bots aren’t even general artificial intelligence. Those who claim they could replace human programmers are wrong or lying.
ChatGPT and other bots aren’t even general artificial intelligence. They don’t understand anything. Some people are now pushing the misconception that the large language models we have now are already AI and can replace human workers.
Continue reading “No, ChatGPT can’t replace human programmers”Rotate BufferedImage in Java
Simple method that correctly rotates a BufferedImage.
Java code to easily rotate a buffered image.
Continue reading “Rotate BufferedImage in Java”It’s about RTFM, not BOMDAS!
Different results can both be correct. This depends on the grammar used to parse the expression. Some handle implied multiplication with higher precedence, others don’t.
There’s this meme on the internet about two calculators (or phone calculator apps) with different results. This is a common misconception and therefore a perfect topic for my blog.
Continue reading “It’s about RTFM, not BOMDAS!”My first inline class for Java
I’ve once made a simple helper class for a set of bytes stored as an integer bitfield. So the idea is that each of the 32bits is used to represent if the ordinal number is in the set or not. This only works for the bytes 0 to 31.
Now this is an “inline class” – something we will get with Project Valhalla.
Here’s the branch of my project:
https://github.com/claudemartin/smallset/tree/valhalla
For this to work you need a JDK with a preview of Project Valhalla. Check java.net for that: http://jdk.java.net/valhalla/
Just download that JDK, clone my branch, build using Ant, and see what you can do with it. It’s actually a good example of an inline class, because it is just an integer (primitive) value but has methods (like a referenced type would). It doesn’t need object identity, but should still behave like an object. For example the sets are comparable. The type actually implements the Comparable interface.
Note that this might not work with newer JDKs. I used JDK 14 + valhalla from java.net and the feature might look quite different in newer releases of Java.
Streamed SQL
I just saw that I never posted this here on my blog. JDBC is still a good way to just connect to some SQL database where JPA would be overkill. So I wrote a library that allows you to use the Java Stream API to process that data.
The Code is on GitHub:
https://github.com/claudemartin/streamed-sql
Example Code:
Connection conn = dbpool.getConnection();
var strsql = StreamedSQL.create(conn, true);
try (Stream<Foo> stream = strsql.stream("SELECT * FROM FOO", Foo::new)) {
stream.filter(f -> f.getName().startsWith("L")).sorted().forEachOrdered(System.out::println);
}