Operator Overloading in JVM COBOL

You can overload operators to provide alternative behavior, or behavior for different operand types. For example, where a Timer type is expressed as hours and minutes, you could define one plus operator to add two Timers and another plus operator to add a number of minutes to a Timer type, as follows:

       operator-id + . 
       01 sumMins binary-long.
       01 sumHour binary-long.
       01 m binary-long value 0.
       01 h binary-long value 0.
       procedure division using value a as type Timer 
                                      b as type Timer 
                            returning c as type Timer.
           set sumHour to a::pHour + b::pHour
           set sumMins to a::pMins + b::pMins
           if sumMins >= 60 
               divide sumMins by 60 giving h remainder m
               set c to new Timer(sumHour + h, m)
           else
               set c to new Timer(sumHour sumMins)
           end-if
       end operator.
       
       operator-id + . 
       01 sumMins binary-long.
       01 m binary-long value 0.
       01 h binary-long value 0.
       procedure division using value a as type Timer 
                                      b as binary-long 
                            returning c as type Timer.
           set sumMins to a::pMins + b
           divide sumMins by 60 giving h remainder m
           set c to new Timer(a::pHour + h , m)
       end operator.

To use the overloaded operators, you use statements such as the following:

       program-id OpConversionExtension.
       01 timer1 type Timer value new Timer(1 45).
       01 timer2 type Timer value new Timer(2 45).
       01 timer3 type Timer value new Timer.
       01 myMins binary-long value 30.
              
       procedure division.
      *> Overloaded operators:       
           set timer3 to timer1 + myMins   *> Plus operator adds binary-long to a Timer
           display timer3::pHour & ":" & timer3::pMins   *> displays 2:15      

           set timer3 to timer1 + timer2   *> Plus operator adds 2 Timer types
           display timer3::pHour & ":" & timer3::pMins   *> displays 4:30
       end program.

The appropriate operator is executed according to the types of operands being operated on. The operator with a matching signature (procedure division using clause) is executed. Where two Timer types are being added, the operator with a signature that adds two Time types is executed.

Note that the operator signature requires that the arguments are passed in the defined order. In the above example, the Timer type must be first and the binary-long second, as follows:

       set timer3 to timer1 + myMins
       ...
       operator-id + .
       procedure division using value a as type Timer
                                      b as binary-long  
                            returning c as type Timer.

To enable the arguments to be passed in the opposite order, you need to declare an another operator where the operands are in the opposite order in the operator's signature. For example:

       set timer3 to myMins + timer1 
       ...
       operator-id + .
       procedure division using value a as binary-long 
                                      b as type Timer 
                            returning c as type Timer.

While the arithmetic operators for addition, subtraction, multiplication and division can all be overloaded, their verb equivalents cannot. For example, ADD in the following statement cannot be overloaded:

       add a to b giving c 

Further Examples

See also the Operator Overloading sample, available from .