Posts

Being as fast as possible is a bad idea

Image
Overview When you design a system which must perform, a common assumption is that you need to it to be as fast as possible. However this needs to be qualified and not having a clear idea of how much performance you need can mean you spend more money, waste time or impact your design more than needed. The first question is knowing whether it is latency, throughput or both which are required. Often only one really matters. A low latency usually gives you a high throughput. If only throughput is required using parallelism is often a cost effective solution. Should you only design a system to be just what you need? It is a brave move to only design a system to do exactly what you need and no more. This is because systems often behave as well as they should on paper. The other reason is that systems tend to vary in their performance due to the complexity of their systems. End users tend to remember the worse performance they ever got making the occasion slow performance more sign...

Java Secret: What is called before main.

Image
Overview The starting point for core java applications is the main(String[]) method. But is there any code which is called before this method and do we even need it? The class has to be initialised Before calling main, the static block for the class is called to initialise the class. public class Main { static { System.out.println("Called first."); } public static void main(String... args) { System.out.println("Hello world."); } } prints Called first. Hello world. Can we avoid having a main() Normally, if you don't have a main() method, you will get an error. However if your program exits before calling main() no error is produced. public class Main { static { System.out.println("Hello world."); System.exit(0); } } prints Hello world. The premain method If you have Java agents, those agents can have a premain method which is called first. Instrument package public static void premain(St...

Java Secret: More uses for varargs

Image
Overview Varargs have many uses from simplifying code. However, they are not always used as often as they could be. Use in reflections Calling a method via reflections is fairly ugly without varargs. ClassLoader cl = Thread.currentThread().getContextClassLoader(); Method defineClass = cl.getClass().getDeclaredMethod("defineClass", new Class[] { String.class, byte[].class, int.class, int.class}); defineClass.setAccessible(true); defineClass.invoke(cl, new Object[] { myNewClassName, myByteCode, 0, myByteCode.length }); With varargs, the code appears cleaner. ClassLoader cl = Thread.currentThread().getContextClassLoader(); Method defineClass = cl.getClass().getDeclaredMethod( "defineClass", String.class, byte[].class, int.class, int.class); defineClass.setAccessible(true); defineClass.invoke(cl, myNewClassName, myByteCode, 0, myByteCode.length); Use of varargs for optional arguments System.out.printf use varargs which might be optional. System.o...

Queues with Threads to be avoided.

Image
Warning: Contains some ranting. ;) It baffles me why so many developers use Queue and Thread explicitly when there is an ExecutorService built in which does both much more elegantly. This has been part of Java 5.0 since 2004 and was available as a third party library many years before that. There are specific cases where using a Queue and Thread is the best choice but that doesn't appear to be the reason it is done most of the time. Is it a lack of understanding of these core libraries? Is it because that is how it is done in other languages and people are bringing the patterns they know to Java? I wonder if developers get some satisfaction doing things the hard way. There is a greater sense of achievement when you have written some really complex code? Any other theories welcome. ;) Examples of developers doing things the hard way. Using a text editor for development instead of an IDE Using double checked locking lazy Singleton instead of an enum with one instance...

4000 hits today.

Image
I am pleased to say my blog got 4000 hits today, a record me. ;) Thank you everyone for taking an interest and reading my articles, it motivates me to post more.

Java Secret: Using an enum to build a State machine.

Image
Overview The enum is Java is more powerful than many other languages which can lead to surprising uses. In this article, I outline some the individual features of enum in Java, and put them together to form a state machine. Enum for Singleton and Utility class You can use an enum as a Singleton or Utility very simply. enum Singleton { INSTANCE; } enum Utility { ; // no instances } Enum to implement an interface You can also implement an interface in an enum. interface Named { public String name(); public int order(); } enum Planets implements Named { Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune; // name() is implemented automagically. public int order() { return ordinal()+1; } } Each Enum Instance a different sub-class You can override the behaviour of an instance. This effectively give the instance a different sub-class of the enum with its own implementation. // from http://download.oracle.com/javase/1,5.0/docs/guide/language/e...

Java Insanity: Two methods with the same super.method()

Overview Recently I wrote an article on how you could write multiple methods with the same name and parameters by using generics. I wondered if you can have multiple methods with the same super.method() Generics and signatures The way the Java compiler works, the generic is effectively part of the signature. However, due to type erasure, the JVM doesn't care so how can this work? The answer is that for the JVM the return type is part of the signature. This means if you have two method with different generic signatures and return types they can have the same name and parameter types. For the purposes of determining a parent super.method(), they share the same method. So if you have two methods overriding a method, which one is actually called. The answer is neither, polymorphism breaks and the parent still gets called. static class A { public Number method() { System.out.println("Inside: Number A.method()"); return 0; } } static class B ...