Saturday, 3 May 2014

Why Java?

I have been a programmer most of my life.  I fell in love with programming as a young teenager.  I learned BASIC, because, then, everyone learned BASIC.  I progressed through Fortran, Algol, COBOL, and various assemblers... but that was decades ago.  Now I program mainly in Java, with occasional use of Scala. 

Java has a reputation of being clumsy, boring and out-of-date.  That's wrong.  Java has changed the way programming is done, and for the better.  Java has been around a long time, and it's worth looking back to how most programming was done at the time Java appeared.

The most widely used development languages were C and C++, and they were used for all kinds of development projects, from the writing of hardware drivers to office software and databases.  It's possible to program clearly and robustly in C and C++, but it's also very easy to end up making disastrous errors.  The reason why errors are so easy is because C/C++ provides a recipe for pretty much direct use of hardware.  What seems to the programmer to be abstractions are in really nothing more than conveniences of syntax - making raw use of resources look structured.  An example is C arrays:

int store [100];

This looks like an array of 100 integers, but it's not really.  It's simply an area of memory that your program now knows about.  It C it's perfectly acceptable to refer to the 200th element of 'store', and even to write data into it.  C compilers can be set to check for this kind of thing, but that's not part of the language.  Compare this with Pascal

var store: Array 1..100 of Integer;

The compiler and program know what this is: an array of integers.  Try and access store[101] and you will get an error. 

Pascal is C with seatbelts.  It can be much safer because restrictions on what can be done can be designed into the language, and many dangerous operations can be checked at compile time, even though they will also result in safe reporting of a mistake when the program is running (although, to be fair, such checks can be turned off to improve program speed).

C/C++ has been a disaster for software safety and security.  It's use is the primary reason why we suffer from computer viruses and security holes. 

Another real problem with C/C++ is portability.  Compiled programs are almost always limited to one type of computer, and one operating system.  Write a C program for Windows and you aren't going to be able to run it by default on MacOS/X or Linux.   This is a real problem, as there are countless legacy systems written in C/C++ that cannot be moved to modern platforms as it would be too expensive to re-write them.

There were many portable and safe languages around at the time Java was invented - LISP and Smalltalk, for example - but these didn't make an impact on C/C++ development because they were so different in style.  Java copied C/C++ syntax so was relatively easy for developers to pick up. 

Java provided safety, in that it had a carefully managed and garbage-collected memory management, and there were many layers of security built-in, so that use of resources on a machine had to be carefully approved for code that was delivered over the network.  This hasn't always worked, but it's always been far safer than the C/C++ approach. 

But, best of all, Java was extremely portable.  Today, I routinely develop and test Java applications on Windows for deployment on Linux and Solaris, and there are no platform-specific problems.  This has made transition to modern 64-bit machines effortless - I haven't had to do anything but download 64-bit versions of the Java runtime.

When I have worked in a programming team, Java has been extremely useful because of the clarity of the syntax: code is not subject to the obfuscation that can be achieved in C/C++ through the use of macros and operator overloading.  Java syntax may look simplistic, but its explicitness is valuable.

Finally, Java has managed to combine safety with very high performance.  The 'hotspot' automated run-time compiling and tuning technology means that none of the traditional ways of manually optimising code are necessary, and instead developers can concentrate on clarity and functionality. 

Java's compatibility includes the ability to continue to run ancient (in IT terms) code on modern Java runtimes.  Java is one of the safest languages to use if you expect to be supporting an application for a decade or two, without the need to recompile all your applications.

 Java 8 allows for functional programming, increasing the power of Java to make use of parallel processing, with all the automation and safety that is typical of the Java language.

Java may not be the most elegant language, but it's status one of the most widely used (if not the most widely used) language is richly deserved.

No comments: