When should we use this annotation? How can we prevent heap pollution?
Continue reading “@SafeVarargs and Heap Pollution”Category: Programming
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”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.
WeakAssQueue
LIFO queue that will drop old elements when needed.
The Idea
Have a Last-In-First-Out queue (implements java.util.Queue<E>) that will release old elements so that garbage collection (GC) can remove them. Like a cache that is a stack.
Continue reading “WeakAssQueue”Using WSL from Java
With Windows Subsystem for Linux (WSL) we can now use Linux tools, such as grep
, rsync
, ssh
, and network commands, such as dig
and netstat
, on Windows directly. No need for cygwin or other 3rd party software.
Here’s an example on how uname
is unknown to Windows, but WSL (Ubuntu in my case) knows it:

What is a Variable?
For beginners it is difficult to understand what a variable is. They are used in nearly all high-level languages and therefore vital for a good understanding of programming
Continue reading “What is a Variable?”Misconceptions about Dates and Time
Was 1900 a leap year and does every minute have 60 seconds?
In Java we have a new API for Date and Time since Java SE 8. Before that, there was already an API with mutable dates and fore some reason there was a date just for SQL.
Many were not happy with the old API. There was Joda-Time, which later became the basis for JSR-310 and is now found in the package “java.time”.
Stalin-Sort
Stalin-Sort using divide and conquer.
I just love this idea:
But when you have an input such as [42,1,2,3,4,5,6]
you just get
[42]
instead of [1,2,3,4,5,6]
. You can get a better result if you use divide and conquer. And that is something Stalin would do, don’t you think? So I implemented a recursive solution in Java. It tries all possible results, which is not a single pass and therefore defeats the purpose of having a fast O(n)
algorithm. And my implementation isn’t even in-place. A new data structure has to be created. That’s why I also added an implementation using a linked list, which is what Mathew describes.
The code is on pastebin: https://pastebin.com/rEmvZSiA
Durgasoft sucks
I already have a long rant on how JavaTpoint sucks. This one is about Durgasoft.
Continue reading “Durgasoft sucks”Recursive FizzBuzz
FizzBuzz in two lines of code.
The only import you need is one for System.out
:
import static java.lang.System.out;
Simple two-liner:
static void f(int a, int z) {
out.println(a % 15 < 1 ? "FizzBuzz" : a % 3 < 1 ? "Fizz" : a % 5 < 1 ? "Buzz" : a);
if (a < z) f(1 + a, z);
}
Without using the literal “FizzBuzz”:
static void g(int a, int z) {
int x = 0;
if (a % 3 < 1) { out.print("Fizz"); ++x; }
if (a % 5 < 1) { out.print("Buzz"); ++x; }
if (x < 1) out.print(a);
out.println();
if (a < z) g(1 + a, z);
}
Note that I use x<1 instead of x==0 to save a single character.