<< February 2012 >>
SuMoTuWeThFrSa
   1234
5678
9
1011
12131415161718
19202122232425
26272829   

Further Information
If you are interested in any product or service from Bloor:

Home > Recent Analysis > Analysis

Java - not my cup of tea

Tel Hudson

Written By: Tel Hudson
Published: 15 April, 2005
Content Copyright © 2005 Bloor

Java was developed for a most laudable purpose. It was to make a fully portable language that can be used for web applications. As it became a standard, the Java environment appeared on most computers that access the Internet. This means that downloaded Java programs can be small because half the program is already on the target computer. It is unfortunate that the language was hijacked by people with an axe to grind.

Java has to be compared to C++. Often comparisons are made to C – generally to show how good Java is – but this will not stick. C is an old language with a great history but its day is done. Similarly, comparisons with C# are not helpful. C# is unashamedly nicked from Java and contains many of the same bonuses and faults.

Many of the differences between C++ and Java, quite frankly, make no difference. Such things as removing the dreaded 'goto' are unimportant. 'Goto' was very useful in the old days when you had to cram a massive program into a 16K EPROM. Now we can accept the odd couple of hundred bytes or the odd microsecond of execution time and write code that can be understood. Competent programmers do not casually use 'goto' instructions. Just because the language supports a construct does not mean that you must use it. However, if you want to write spaghetti code you can do it using all the structured constructs.

Whilst C++ allows global free-standing variables and routines, Java insists that all items must be either classes or members of classes. It is said that this makes Java a totally object oriented language. This is just nonsense. Java classes are not objects. They are program constructs that can be used to represent objects. Equally well, they can be used as a container for a number of disconnected items. I have seen any number of programs that have a class called CMiscellaneous simply to hold a rag-bag of globals.

It is interesting that the authors of Java missed the one feature of C that has caused more errors that any other. This is the use of '=' for assignment which is often accidentally used in place of the equality operator '==' (double equals). When creating C++, Bjarne Stroustrup considered replacing the C assignment operator with ':='. In the end, he reluctantly accepted backward compatibility so that legacy C code would compile under C++. The authors of Java claimed to be under no such constraint. Nevertheless, they perpetuated this problem. [Note: "if (a = 3)" is legal in Java and does not mean the same as "if (a==3)".]

Let us look at the new ideas that Java has that are beneficial. Automatic garbage collection is a great bonus. Memory leaks are a plague that bedevils C++ programmers. It is no secret that many standard utilities have memory leaks, including some popular word processors. Some of the problems that PC users suffer, such as computers that hang or refuse to shut down, are often the result of the odd 20K that has disappeared. Of course, whereas most memory leaks are simply programmers forgetting a delete statement, sometimes they can be evidence of something more important. That the memory leaks are obviated by automatic garbage collection can hide the fact that a programmer has completely forgotten to release or close a set of resources.

The other thing going for Java is the standard library. The C++ standard library has grown like Topsy. It contains many elements that have been inherited from C that are now obsolete whilst the modern elements which did not exist in the 1960s are under-represented.

Now we come to the features that make Java a poor choice for a serious program. The prime item is the lack of pointers. Pointers can be misused and that can cause errors. Java attempts to avoid these errors by pretending to get rid of pointers. Of course, it cannot. Instead, it sometimes treats declared variables as pointers and at other times as values. Anyone who understands pointers can see what is going on. It is a nuisance to have to mentally translate your language into real objects. Languages such as C++ are simpler because you state exactly what you want. I do wonder about new programmers who learn on Java. How will they ever understand what is really going on?

The other missing feature that can cause problems is multiple inheritance. This is one of those nasty features that none of us like. Like many other things, it has its aficionados who will use it to death and this is to be deprecated. But there are times – admittedly rare – when the real world objects that you are modelling inherit features from two parents. When that happens you can model the real world or you can go off into a confusing fantasy. Unfortunately, it always occurs in a complicated part of the model where you just want the simplest solution.

Java lacks many other items that can lead to simpler programming, such as templates and operator overloading. Individually they are not important but the totality shows an intention to dumb down the language. Writing anything but a tiny program requires care and thought. You make things easier by using good methodologies. It does not help when the language designers try to make a political statement as to what constitutes good programming.

Java is an excellent language for tiny applications. The trouble is that if you have a tiny application, any old language will do. Using Java for serious jobs is like trying to take the skin off a rice pudding wearing boxing gloves. You can do it, but the best tools for the job are wrapped in padding lest you misuse them.

The real problem with Java is the "might have been." Imagine taking a proven programming language such as C++ and just adding those features that were needed for use as a universal web programming language. Define the intrinsic datatypes so they have guaranteed ranges. Produce the universal environment and upgrade the standard library. Possibly then tidy up the obsolete features such as 'goto' and the sillies such as the '=' assignment operator. If they had stopped there, then Java would have superseded C++. As it is, we are left with two languages: one for web applets and another for heavy processing.

Reader Comments

There have been no comments on this page. Why not be the first?

To prevent spam, we ask that you register and log in to post a comment.