Hello World: Why Java never was my language of choice

It's customary in any programming language for one to provide a simple example of displaying "Hello World" in that language. Generally these examples are quite contrived, as they tell you nothing about the actual language, and have been construed in several notable cases to show why one language is more verbose than another. I'd like to use a Hello World example in a similar way to the verbosity argument, but in a slightly more pragmatic way: part of the boilerplate of Java makes it my last choice as a language for quick hacks.

In a lot of the so-called dynamic languages, a Hello World program is pretty straightforward. I'll omit the shell call that generally shows up at the top of the program and just show the meat.

  • Python

    • print ("Hello World")
  • Ruby

    • puts ("Hello World")
  • Perl

    • print "Hello World\n";

Even LISP / Scheme and BASIC makes this pretty straightforward:

  • Scheme

    • (display "Hello World")
  • BASIC

    • 10 PRINT "Hello World"

Once you get into languages like C, it gets a little trickier. Here is the example:

#include <stdio.h> int main() { printf("Hello World\n"); return 0; }

C is special because it requires a minimum of boilerplate code to make it work. There's the library pre-processor directive (#include <stdio.h>), and the function creation for main. We then have the print statement (called "printf" for formatted printing), and a return statement (which could be optional, but reporting our error status to UNIX is quite helpful, even when no error is possible).

And then there's Java's rendition:

class HelloWorld { public static void main (String args[]) { System.out.println ("Hello World"); } }

So, in order to do one of the simplest functions in Java, I need to:

  • create a class called "Hello World"
  • Endow it with a main function, since Java has to have a main
  • Remember that main takes arguments (String args[])
  • Remember the breadcrumb trail for the print statement (System.out.println)
  • Hope I didn't forget anything (Because missing the parameter "String args[]" causes Java to throw this "helpful" exception: Exception in thread "main" java.lang.NoSuchMethodError: main

Now, to be fair, the C example should also take arguments, but at least I can get output if I chose not to take on the arguments. Java forces me to accept those parameters, otherwise it won't retrieve the function.

So, as a coder, I have a choice. If I want to test a language function, Python, Perl, and Ruby make it pretty painless to check something out. C has slightly more boilerplate required, but it's pretty non-intrusive. Java, on the other hand, expects me to:

  • create a class, even though I might not have everything finalized in my head
  • remember the format, which could be a problem if I haven't coded it in a while. (I know this is likely a red herring for people who program Java on a daily basis, but think about how effective you'd be if you decided to take a three month cruise away from your computer. Would you still be able to remember all this stuff?)
  • remember, and be familiar with the class library for I/O operations (again, if you're experienced, this is likely not much of an issue).
  • know enough to bail yourself out of a parameter mismatch.

So, whenever I rustled through my computer toolbox to do something slightly programmatic, Java never ever made the cut. Even on the occasions when I thought "Oh, I should use Java because I want to learn it", I couldn't commit myself to all of that up-front design and typing just to get something ready.

Some people might argue that today's hacks become tomorrow's problem programs, and that a more refined approach is necessary whenever beginning a programming project. To those people I submit that indeed, the right tool with the right experience is always preferable to a hacked solution. Unfortunately life is too short for the perfect solution every time. If a slightly smaller screwdriver makes the screw turn without stripping it, it's a far preferable tool for me to use than a screwdriver that is a perfect fit, but requires 10 minutes to put on special gloves to use it. Remember, the example presented is just a "Hello World". If you need more convincing of the problems of Java, I suggest trying your hand at coding a program that pops the last character off of a string of characters, or some other non-trivial example.

Here's Perl:

$char = 'This is a simple example'; @char_array = split //, $char; while (@char_array) { $a = pop @char_array; print "$a\n"; }


Comments !

links

social