Advertising

The Problems With Java

Contents

Introduction

Java is designed to be simple and easy to learn. This was achieved by not including some useful features. Clearly a decision was made to hide some problems rather than to give people the means to solve them.

I learned C++ after I learned Java. C++ does not have these problems, but its very completeness make it more daunting. I still believe that Java is a good way to learn Object Orientated programming, but that people will quickly become frustrated by the limitations.

Not strongly-typed at compile time

Casts are checked at run time. Therefore a system design can only be checked by run-time testing See No generic programming.

This is problematic

This means that design problems may not be seen until too late, and they may not show the big picture.

This is not enough

If there are an almost infinite number of paths through the code - for instance, a converter, compiler, or virtual machine - then the design flaws may never be seen during testing.

No copy-by-value (or Everything is a reference)

All objects are passed by reference, therefore developers must do a great deal of explicit cloning in order to ensure that object references are not being shared.

Developers must manually clone an object if they want to get a copy of an argument that they can safely change.

Collections (e.g. lists) are collections of object references, so a method that returns a collection needs to manually clone each item in the collection (note that normal collection cloning just creates a copy of the same references). Developers need to trust that methods do this properly although there is no way to tell without looking at the implementation.

Java programmers often believe that there are no pointers in Java, but forget that everything apart from a base type (such as an int) is a reference. It is far too easy to change an object and inadvertently affect some other code that you didn't know was sharing the same object. Also, this type of problem is very difficult to debug - it just looks like random behavior.

No const method arguments

Developers do not know whether a method will change the values of its arguments unless they look at the implementation of that method. This type of problem is also difficult to debug because coders rarely expect a method to change its arguments.

No generic programming (or No templates)

There are no collections of particular types of objects - there are just collections of references to Objects. Collections will normally contain references to instances of a particular class or its subclasses, but nothing about a collection's declaration says what it will contain. Developers who take objects from these collections must cast to the expected type, but the compiler is unable to check the validity of the cast until run-time. See Not strongly typed At compile Time.

Again, a developer can only know what a method will do to a collection by looking at the implementation.

It is possible to create your own subclassed containers which are specialized for a particular type, but this leads to a lot of repetitive code, and it is not possible to change the return type of an overridden method.

Conclusion

Java has the following problems:

Java developers could document the behavior of every method but they rarely do, and there is no formal guarantee that the code will comply with the constraints described in its documentation.

Ideally, a design would be perfect from the start, developers would make no errors while implementing it, and all of the code would be completely documented. In the real world, designs are perfected only in the course of development, simple errors are made, and documentation is incomplete. This is the reality, but java does not help us deal with it.

However, java is simpler and more portable, so it is useful for these types of systems:

And Java certainly has advantages over plain C, which also suffers from the problems listed here.

Recommended Reading:

Thinking In Java, Bruce Eckel: Tells you how Java really works.

Copyright © Murray Cumming. Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.

Murray's Web Pages