Background

Integer handling in Java vs. Ada

18. March, 2008 • Jesper Dahl Nyerup

For those of you not in the loop, I attend a Diploma Programme in Computer Science in a school located in southern Denmark. The programming curriculum in the last couple of semesters have consisted mainly of courses in Java and Ada (and Spark, which, basically, is Ada with handcuffs).

Lately, I’ve grown more and more fond of Java, primarily for the following three reasons:

  1. The object (or class?) oriented paradigm suits me well, and this is very well implemented in Java.
  2. Sun’s Java API is as consistent and simple to use as it is vast and well documented.
  3. The garbage collection is brilliant and, as far as I have discovered, very well functioning.

But of course my fondness has a fly in the ointment. Java’s integer handling resembles those of many other programming languages, with the exception that the class Integer is a wrapper class for the primitive type int. But this particular area is, to say the least, ingeniously thought out in Ada, and I can’t help but miss this functionality when I’m coding in Java.

In Ada, creating a new integer-type with a delimited range for a specific purpose, is as simple as:

type My_Integer is new Integer range 26 .. 42;

And declaring an array with this new type as range, for instance, is even more trivial:

My_Array : array (My_Integer) of Some_Class;

This also works well with enumerated types:

type Day is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);

And, I assure you, doing the following declarations and assignments will very quickly become quite addictive:

Work_Day : constant array (Day) of Boolean :=
    (Mon .. Fri => True,
     others => False);

Consumed_Cups_Of_Coffee : array (Day) of Natural :=
    (Mon => 10,
     Tue .. Fri => 7,
     Sun => 2,
     others => 3);

This is an immensely powerful feature, and I didn’t know I missed it in other languages before I tried it in Ada.

I don’t presume to know how this would be implemented in Java. I realize that this cannot be accomplished, simply by being able to inherit from the class Integer, as this merely wraps around the int-type, and thus would lack an integer type delimited to the suitable range.

But then what? Solving this pickle would surely make a lot of code a lot more elegant and readable. Any ideas are more than welcome.

EnglishKommentarer

5 Kommentarer

    18. March 2008 • Jacob Sparre Andersen

    I suppose I should have spent some time teaching my students how to write object oriented code in Ada.

    18. March 2008 • Peter Makholm

    Besides as a term of an array type, in which ways does the behaviour of My_Integer differ from the behaviour of Integer?

    As I see you example the real gain would only be that the compiler can decide My_Array can be best implemented with an C-like layout or with an associative array like perl’s hashes.

    What else do you get? Run time checking of bounds?

    18. March 2008 • Jesper Nyerup

    … in which ways does the behaviour of My_Integer differ from the behaviour of Integer?

    None, what so ever.

    What else do you get?

    The main advantage, as I see it, is readability (and “writeability”, for that matter).

    The only performance improvement I can think of, is that the compiler knows how much memory is needed to describe this value, and that this is less than Integer, but this is not my point at all.

    My point is that code instantly becomes easier to read, if you can count like you think, not necessarily from zero, name things as you like, and use these constructions in simple as well as complex data types.

    19. March 2008 • Jacob Sparre Andersen

    What else do you get?

    Run time checking of bounds. (Sometimes handled already at compile time.)

    Protection agains implicit conversions between apples and oranges.

    The possibility of using the type as an index range in a for loop:

    for D in Day loop
       if Work_Day (D) then
          Put (D);
          Put (" is a work day.");
          New_Line;
       end if;
    end loop;
    

    29. April 2008 • Jens Schødt

    I would look into “Programming By Contract” which would give you extended runtime checking features. Ada is superior in its static type checking. The philosophy is to catch bugs as early as possible. So indeed My_Integer is a different type and you can safely use My_Integer in a division without running into divide by zero problems. In this aspect it is the same as a contract checked at compile time.

    As a side the compiler can be instructed not to create the runtime checks (after heavy testing of cause), which will generate something only achievable by heavy bit fiddling in C.

    All in all a win win situation…

Skriv kommentar




Sprog

Tidligere indlæg

Tjenester

Facebook Twitter LinkedIn LastFM Skype

Jesper Dahl Nyerup • Engbakken 10, Skuldelev, 4050 Skibby • Epost: jesper@dahlnyerup.dkXMPP: jesper@dahlnyerup.dk