Showing posts from February, 2011

With generics, the return type is part of the method signature.

I recently came across this odd behaviour in Java. The JVM has always treated the return type as part of the signature but before Generics Java did not. Here is an example of where it does. This was tested using Oracle JDK 6 update 23, OpenJDK 6 update 20, and IBM's JDK R9 2.4. Apparently Java 5.0 update 22 doesn't not compile this code, but given Java 5.0 is EOL, I would have hoped Java 6 would have the correct behaviour. public class Main { public static void main(String... args) { Main.<Integer>print(); Main.<Short>print(); Main.<Byte>print(); Main.<Void>print(); } public static <T extends Integer> int print() { System.out.println("here - Integer"); return 0; } public static <T extends Short> short print() { System.out.println("here - Short"); return 0; } public static <T extends Byte> byte print() {

float has a use after all.

After many years believing there was no good use for float , I discover its never too late to eat my words. float has significant round errors and doesn't save much memory compared with double . However I have situation where I want small whole numbers (no rounding error) and I want to handle not-a-number transparently. I also want to minimise data passed between threads to cut latency. So float turns out to be the best solution. :-P

Don't type 2.2250738585072012e-308 in java.

There is a long standing bug in Oracle's Java (even the latest version) where using 2.2250738585072012e-308 as an input causes the JVM to go into an infinitie loop. Even the compiler and your IDE can go into an infinite loop if you use it in code. :P