One line at a time

Another Java technology blog from a developer far away from home

Month: October 2014

Specialized collections for Enums

Yes, I will talk about enums again, I find these guys fascinating. And I’m one of these people who loves to know what’s happening behind the scene, to scratch the surface and (reasonably) go as deep as I can.

So you’ve heard about HashMap or HashSet right ? It turns out they have brothers (or sisters, up to you). They are called EnumMap and EnumSet, and they also extend AbstractMap and AbstractSet. When I discovered this, I obviously asked myself, why did they do this, how does it work ?

As you can imagine they are dedicated to be used with enums only, the enum being the key. So let’s go deep into the OpenJDK implementation. I’m not going to talk about the implementation of the HashMap (or HashSet), they are plenty of posts about it on the internet. I’ll start directly with the explanation of EnumMap and EnumSet because I never found any implementation’s explanation.

Continue reading

Java Enums, that’s the stuff

Since Java 1.5, the language includes the enumerated type, everybody knows that. But these guys are offering a very powerful tool here, too often used in the old way because we don’t always realize we’re dealing with a proper Java object.

Where languages like C are just offering an integer representation with enumeration, an Enum in Java is an object with all the characteristics we know about the Java objects.

Behind the scene

So what’s happening when we declare an enum in Java ? Let’s take an enum called AdjustmentType for example, which describes the operation we want to apply on a price:

public enum AdjustmentType {

Because of the enum keyword, AdjustmentType becomes implicitly a final class extending the java.lang.Enum abstract class,  the call AdjustmentType.class.getSuperClass() would return the java.lang.Enum class.

Continue reading

A blog about… ?

Welcome to my (first) blog ! I never had a blog, not that I never thought about it, it’s just I never really found the topic and the content. But recently, I was talking with @Jaiew, my co-worker at Skiddoo, about how fascinated it was, the way some Java classes have been implemented in OpenJDK by Joshua Bloch (I spend a lot of time reading the JDK source code), and he told me I should write articles about it. I realized he was right, it could potentially be an interesting subject to other people too, I finally got my topic.

Here we are today, a VPS, a domain, some installations, configuration of PHP5, WordPress, we’re up and running ! So yes, it will be another blog talking about Java, but more the Java “Behind the scene”. I can’t promise it will only be this, it will probably have some stuff about being a software engineer at Skiddoo or posts about iOS development, my second main development skill, and why not talking about Sydney this amazing city ? I guess it will be about me in the end, but I’ll do my best to get everybody to learn something at the end of a post. So let’s start immediately with this one:

Did you know you can find the magic number 0xCAFEBABE (Cafe Babe) at the beginning of every Java .class file ?

This magic number is used to mark the file as conforming to the class file format. I like the story behind this number.

© 2018 One line at a time

Theme by Anders NorenUp ↑