I need to vent about Java. Please ignore or read with amusement.
I've been working for the past few days on an IDL parser in Java. The parser includes an arithmetic evaluator for constant value declarations in IDL, like this :
const unsigned long field_value = 88; const float pi_field_value = field_value * 3.14; const string name = "foo";
Since constants can be string types or numeric types, the parser has to pass around the constant values as strings, convert the strings to numeric types when it expects to have to evaluate some arithmetic operation (of course throwing an exception when the string doesn't represent a number), then convert the result back to a string. In a language like C or C++ this would be a pain. But, let the angels rejoice, it's also a pain in Java !
Consider this short example program to convert two strings to longs, add them, and convert the result to a string :
public class Dumb { public static void main(String args[]) { long a = Long.parseLong(args[0], 10); long b = Long.parseLong(args[1], 10); String result = a + b; System.out.println(a+" + "+b+" = "+result); } }
Originally, I wanted to just make two Long objects and add them together. But in Java you can't add two Long objects together* (specifically, the java compiler informs me, "operator + cannot be applied to java.lang.Long,java.lang.Long"). Grr.
Just for comparison I wrote a functionally equivalent Python script :
from sys import argv a = long(argv[1]) b = long(argv[2]) result = `a + b` print "%d + %d = %s" % (a, b, result)
The ratio of bytes of source code in the two examples is 112/213 ~ 0.523. So it looks like there are about two characters in my Java code for every useful character in the equivalent Python code. Is the compile-time security of strong typing worth having to input twice the code ? I'm not convinced. Particularly because Java containers like Hashtable can only store Objects (which is an extra pain when you want to store a number in a hash table, see *), Java's type safety is often reduced to the whim of the programmer. I am aware that Java will be coming out with C++-like templates sometime soon, but it just seems like so much extra grunt work.