When should we use this annotation? How can we prevent heap pollution?Continue reading “@SafeVarargs and Heap Pollution”
Simple method that correctly rotates a BufferedImage.
Java code to easily rotate a buffered image.Continue reading “Rotate BufferedImage in 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:
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
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.
I think I’ve just found a mistake in the Java Tutorial.
Maybe I’m just being pedantic, but I think I’ve just found a mistake in the Java Tutorial. It claims:
All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.The Java™ Tutorials » Language Basics » Operators
What is this supposed to mean? Evaluation from right to left would mean that when you have a line like the following you get the rightmost expression evaluated first:
int x = 42; x += getNumber(); // The above would be equivalent to: x = getNumber() + x;
But that’s not how Java actually evaluates this expression. And the JLS 12 (2019-02-08) clearly states in 15.26.2 that “the value of the left-hand operand is saved and then the right-hand operand is evaluated.”Continue reading “Java Assignments are evaluated Left to Right”
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 programmingContinue reading “What is a Variable?”
Many misconceptions in Java and similar languages stem from the bad default behaviour. I’ll explain some of them.Continue reading “Bad Defaults in Java”
Java isn’t easy. It’s often more complex than needed. But being easy for beginners shouldn’t be a design goal with high priority.
Should a programming language be easy?
I don’t know why so many educational establishments use Java to teach programming. They often start with Java in the first semester. Even the Oracle tutorials expect the reader to already know some basics. Java was designed for programmers who already know C/C++ as seen in this example.
Continue reading “Is Java easy?”
mainmethod is similar to the
mainfunction in C and C++; it’s the entry point for your application and will subsequently invoke all the other methods required by your program. [Lesson: A Closer Look at the “Hello World!” Application]
A charset is not an encoding.
We also have
StandardCharsets. It’s great to have a class with these constants. But UTF-8, UTF-16BE and UTF-16LE are all the same set of charaters: Unicode
The keyword “static” is arguably a bad choice of naming.
If you look up “static” in a dictionary you find many meanings, none of which explains its use in programming languages. So why is it used in Java, and how is it misunderstood?Continue reading “Misnomer: static Keyword”