Tag Archives: Java

Pluralization in Java

Any website that needs to deal with international customers will naturally have to support multiple languages. In this regard, Java has pretty good support for this and there are a plethora of online resources that show you how. The trouble is that there are almost no non-noddy examples for pluralizations. Pluralizations are simple in English but a nightmare in languages like Polish and Russian where the multiple suffix is almost random!

Thankfully one of my co-workers Stuart came across the excellent if little known ChoiceFormat class that provides much of the functionality you need. Being a good lad he wrote a blog post about it and in a nice show of karma, he was given an even better library to use. Supported by IBM, the ICU4J library is used by almost everyone including IBM themselves, Google and Rolls-Royce and offers all the features you need for an internationalization library.

What The Double Curly Brace!!!

It’s not everyday you get to wow your co-workers with a bit of programming esoterica. As developers we love to get one over our co-workers, especially when it comes to weird bits of the language. Yes, it’s childish and 99 times out of 1000 it has no useable benefit but that hundredth time tends to be a doozy and I regard this kind of intellectual one-upmanship as a positive thing. At the root of it, we are knowledge workers and the more we know the better we are so why shouldn’t we try to learn as much as possible.

But this was not one of those times. This was just something that I’d seen in passing a few months ago that amused me but since it stumped the 3 best developers on my floor I thought I’d put it down, so here it is:

package com.bloodredsun;

import java.util.*;

public class DoubleCurlyBrace {

    private static List myList = new ArrayList(){{
        add("Hello ");


So what we have here is an instance initializer block for an anonymous inner class. The class of myList is actually com.bloodredsun.DoubleCurlyBrace$1 and not a java.util.ArrayList. While this is admittedly cool, the syntax is so unusual that I threatened to throw heavy things at anyone in the team that actually uses this. I will report back on the fate of anyone that does… 😉

Why we don’t use Doubles for Financial Calculations

My current client is in the middle of hiring some Java developers and as I mentioned earlier in March (Interviewing – the importance of PASSION!) I’ve been doing some of the interviewing. One of the things we’ve done is to create a technical task to see how the candidates actually code. It’s a simple exercise that requires them to think through some of the basics of financial operations and one thing that has surprised me has been the common use of doubles to represent financial values. It’s been highlighted for some time that this is not a great thing to do but someone actually challenged me to show that it wasn’t.

So here we go…

package com.bloodredsun;

public class DoubleOperation {

    public static void main(String[] args) {

        double t1 = 10.266d;
        double t2 = 10.0d;

        //Outputs 0.266

        double h1 = 100.266d;
        double h2 = 100.0d;

        //Outputs 0.26600000000000534

Ouch! That is not what we want but it is the classic behaviour of doubles. The inability to represent some decimals in the IEEE-754 format (as binary fractions) causes this. If we want correct precision the answer is to use BigDecimals but we have to remember to use Strings in the constructors or you end up with the same issues that you were trying to avoid.

package com.bloodredsun;

import java.math.BigDecimal;

public class BigDecimalOperation {

        public static void main(String[] args) {

        BigDecimal t1 = new BigDecimal("10.266");
        BigDecimal t2 = new BigDecimal("10.0");

        //Outputs 0.266

        BigDecimal h1 = new BigDecimal("100.266");
        BigDecimal h2 = new BigDecimal("100.0");

        //Outputs 0.266

That’s great but wouldn’t it be nice to use the normal operators rather than the overly-verbose method calls for the mathematical operations.

Now there is no way that we can do this in Java but if we let ourselves use another language on the JVM…

package com.bloodredsun

object ScalaBigDecimalOperation {

  def main (args: Array[String]) {
    var t1 = BigDecimal("10.266")
    var t2 = BigDecimal("10.0")
    //Outputs 0.266
    println(t1 - t2)

    var h1 = BigDecimal("100.266")
    var h2 = BigDecimal("100.0")
    //Outputs 0.266
    println(h1 - h2)

Scala FTW!

PS if you want to know more about floating point operations have a read of What Every Computer Scientist Should Know About Floating-Point Arithmetic

Page Template Frameworks in Java

My new project is very different from the Poker project that I’ve spent the last year doing – it’s all about web sites. The interesting thing about this project is the sheer scale involved – billions of page views a month. Needless to say, this is far a pretty large company where there are big teams working on different aspects. Front end UI guys writing HTML, CSS and JavaScript, Java guys writing services and execution frameworks and DB guys working their magic on the backends.

Where I will be involved is the web tier itself, creating the web application that renders the content and beefs up the site with a whole host of sexy features like A/B testing, SEO, analytics and more. So one of the things I have to do is to come up with an easy way of integrating the HTML produced by the UI guys with the data required to populate it.

Page Template Engines seem the obvious choice with Velocity and Freemarker as stand out candidates. There are some more left field candidates like StringTemplate, Trimpath , Mustache and JPT. JPT is particularly interesting as it is a Java implementation of Zope Page Templates where the template code lives as attributes in the mark up rather than as their own tags.

Consider this example in freemarker where we want to iterate over a list of writers.

<#list writers as writer>

Here’s the same in JPT

<li tal:repeat=”writer writers”><span tal:content=”writer”>Test content goes here</span>/li>

The benefit is that the JPT fragment is still valid XHTML and you can have placeholder content. This makes the integration between front end devs and web tier guys really simple.

There are a few issues with JPT but it looks interesting enough to have a closer look.