Binary types and conversions


The Java environment supports, and indeed requires, the notion of fixed-precision 'primitive' binary types, which correspond closely to the binary operations usually available at the hardware level in computers. In brief, these types are:

Objects of these types are handled specially by the environment 'under the covers' in order to achieve maximum efficiency; in particular, they cannot be constructed like other objects -- their value is held directly. This distinction rarely matters to the NetRexx programmer: in the case of string literals an object is constructed automatically; in the case of an int literal, an object is not constructed.

Further, NetRexx automatically allows the conversion between the various forms of character strings in Java (String, char, char[], and Rexx) and the primitive types listed above. The 'golden rule' that is followed by NetRexx is that any automatic conversion which is applied must not lose information: either it can be determined at compile time that the conversion is safe (as in int -> String) or it will be detected at run time if the conversion fails (as in String -> int).

The automatic conversions greatly simplify the writing of programs for the Java environment: the exact type of numeric and string-like method arguments rarely needs to be a concern of the programmer.

For certain applications where early checking or performance override other considerations, NetRexx provides options for different treatment of the primitive types:

  1. options strictassign -- ensures exact type matching for all assignments. No conversions (including those from shorter integers to longer ones) are applied. This option provides stricter type-checking than Java, and ensures that all types are an exact match.
  2. options binary -- uses Java fixed precision arithmetic on binary types (also, literal numbers, for example, will be treated as binary, and local variables will be given 'native' Java types such as int or String, where possible).

    Binary arithmetic currently gives better performance than Rexx decimal arithmetic, but places the burden of avoiding overflows and loss of information on the programmer.

The options statement (which may list more than one option) is placed before the first class statement in a file.

You may also explicitly assign a type to an expression or variable:

  i=int 3000000    -- 'i' is an 'int' with initial value 3000000
  j=int 4000000    -- 'j' is an 'int' with initial value 4000000
  k=int            -- 'k' is assigned type 'int', with no initial value
  say i*j          -- carry out multiplication and display the result
  k=i*j            -- carry out multiplication and assign result to 'k'

This example also illustrates one difference between 'options nobinary' and 'options binary'. With the former (the NetRexx default) the SAY would display '1.20000000E+13' and a Conversion overflow would be reported when the same expression is assigned to the variable 'k'.

With 'options binary', binary arithmetic would be used for the multiplications, and so no error would be detected; the SAY would display '-138625024' and the variable 'k' takes the incorrect result.

Binary types in practice

In practice, explicit type assignment is only occasionally needed in NetRexx. Those conversions that are necessary for using existing classes (or those that use 'options binary') are generally automatic. For example, here is an "Applet" for use by Java-enabled browsers:

  /* A simple graphics Applet */
  class Rainbow extends Applet
    method paint(g=Graphics)                 -- called to repaint the window
      maxx=size.width-1
      maxy=size.height-1
      loop y=0 to maxy
        col=Color.getHSBColor(y/maxy, 1, 1)  -- select a colour
        g.setColor(col)                      -- set it
        g.drawRect(0, y, maxx, y)            -- and fill a slice
      end y

In this example, the variable 'col' will have type 'Color', and the three arguments to the method 'getHSBColor' will all automatically be converted to type 'float'. As no overflows are possible in this particular example, 'options binary' may be added to the top of the program with no other changes being necessary.


[ previous section | contents | next section ]

From 'nrover.doc', version 1.113.
Copyright(c) IBM Corporation, 1996, 1997. All rights reserved. ©