Eclipse crash in CompilerThread on 64-bits Java
The title describes it all, there is a bug in the Sun JVM version 1.6 update 5 and later versions that makes the CompilerThread in Eclipse crash on 64-bits systems. Bugreports have been filed for Eclipse and the JVM, but the problem is still not resolved with the recent 1.6 update 10 release.
To save anyone with the same problem a lot of time, a workaround is to add the following line to eclipse.ini:
-XX:CompileCommand=exclude,org/eclipse/core/internal/dtree/DataTreeNode,forwardDeltaWith
That stops Java Hotspot’s JIT compiler from compiling the problem-method and avoids the crash.
Fun with initialization blocks
After Tomas posted the article Creating a Map in “Ruby-style” in Java, I wanted to tell you a little more about the { } block notation, which is used in that article to create a HashMap object. It looked like this:
new HashMap() {{
put("name", "Tomas Salfischberger");
put("phone", "123456789");
put("room", "11b");
}};
The { } notation is called an “initialization block”. We can also see this notation in action in the following class:
public class Person {
protected int age;
// Initialization block
{
age = 30;
}
public Person() {
age = 40;
}
}
And you can feel it coming, the bonus-question here is: After creating a Person object, what is the value of the age field? You can try for yourself, or I can just tell you it will be 40. From that we can conclude that the init-block is executed before the constructor. If you check the documentation from Sun, you’ll find that init-blocks are indeed prepended to every constructor.
A new question arises: How could Tomas have used the put method in an init-block before the constructor of the HashMap has actually run? A more detailed look at the HashMap example reveals that what’s actually happening there, is the creation of a subclass of HashMap. This explains why the init-block runs after the constructor of HashMap: The init-block of the subclass is prepended to the constructor of the subclass, and the constructor of a subclass always runs after the constructor of its superclass.
A practical test confirms the following execution order of init-blocks and constructors:
- superclass init block
- superclass constructor
- your init block
- your constructor
You can also define multiple init-blocks, the runtime system guarantees they execute in the order that they appear in the source code. So all in all the init-blocks are a nice (and widely unknown) language feature which can be used to add general initialization to all constructors or in special cases to replace a no-argument constructor.
Creating a Map in “Ruby-style” in Java
With Ruby on Rails gaining popularity more and more people post comments about how awkward coding in Java (or .NET for that matter) is. Someone tried to explain why Java is this bad by explaining how PHP and Ruby can instantiate a Map of string and value pairs (called array in PHP). The syntax to define a Map in Ruby would be:
{
"name" => "Tomas Salfischberger",
"phone" => "123456789",
"room" => "11b"
}
And for PHP:
array(
"name" => "Tomas Salfischberger",
"phone" => "123456789",
"room" => "11b"
)
Now the problem with a lot of these comments is that most Java-critics can’t actually write proper Java code. There is, for example, the following method to instantiate the same Map in Java:
new HashMap() {{
put("name", "Tomas Salfischberger");
put("phone", "123456789");
put("room", "11b");
}}
It is true that there is a bit more typing involved, but how many seconds does this actually cost you? And what if you look at the extra information we have provided? In Java we have actually expressed that we need a HashMap and not for example a TreeMap. That is not just more typing because Java programmers like typing, it’s actually valuable information.
Another one which might be useful (but more widely known I guess?) is this function to create a List using the Java 5 varargs feature:
Arrays.asList("value1", "value2", "something else")
These are both just simple tricks to make a programmer’s life easier, unfortunately a lot of people don’t know about them. Let’s hope the lost Ruby souls will some day find out about the simple tricks in other languages 