Some Mammals can fly. Dog extends Mammal, but it can’t fly. The term “extends” leads to some misconceptions. Continue reading “Misconceptions about the “extends” Keyword”
Category: Programming
Simple Animation Loop in Java
If you want to show some animation, maybe for a simple java game, then you need to render a certain amount of frames per second. For a smooth animation you want 30 to 60 frames per second. But sometimes the needed calculations need a bit longer so the delay until the drawing of the next frame needs to be shorter. For this an animation loop is needed. I have implemented a very simple animation loop for Java 8+ that can be paused and started.
Tuples in Java
Java doesn’t provide tuples. Here are some ideas what you can do about it. Continue reading “Tuples in Java”
Types of Methods in Java
So in Java there are many kinds of methods. I probably won’t even list half of them but I try to explain how different methods can be.
Continue reading “Types of Methods in Java”Functional Programming in Java 8
Functional Programming made easy!
Continue reading “Functional Programming in Java 8”
Misconceptions about the new Operator
New is used to create new objects. But it’s used carelessly.
Continue reading “Misconceptions about the new Operator”Recursive Closures in Java
In Java you can only reference final fields inside a closure. Here’s my solution*.
Continue reading “Recursive Closures in Java”Generics
Generics have one thing in common with regular expressions: Students think they are an easy to use tool but they just end up with code they do not understand and can’t maintain. Regular expressions can often be replaced by well written and documented code. But you can’t just replace generics. A student using collections with generic type parameters but not fully understanding the concept is still better than one using “raw” collections. And it’s often ok to use something just to get familiar with the idea before learning the theory. The downside is that many misconceptions emerge from this approach of teaching. In this post I try to explain some of the common misconceptions on generics in Java.
Continue reading “Generics”Multiset in Java
Here’s an implementation of a multiset in Java 8. It uses a Map<T, Integer>
to count how many times an object was inserted (multiplicity). It is not thread-safe, but there’s synchronizedMultiset(...)
and it always knows the current size in O(1)
. With the method asMap()
you can get a view of the multiset as a Map
, that will allow modification of the underlying multiset.
The code is on GitHub: github.com/claudemartin/multiset
This started as a simple implementation and is not rather complex. The iterator allows to remove elements during iteration. I tried to write my own Spliterator
, but it wasn’t faster than the default implementaion. Many methods are optimized and I added some set operations for multisets (union, intersect, minus). With merge(...)
you can merge two multisets with any operation.
Fork/Join with non-recursive Task
Most tutorials use some recursive Task to calculate a Fibonacci number or something similar. This leads to the false impression that Fork/Join can only handle recursive Tasks (e.g. Divide-And-Conquer). Here’s a very simple example of a non-recursive Task.