JFront - Operator Overloading for Java

Back to G&G Inc's home page.

Check out G&G Inc's training page.

Why Operator Overloading?

One of the many desirable features of C++, is the ability to overload operators in the language to give them new meaning for operands of a specific class. This is most easily demonstrated for even class String which is part of the Java Language; without operator overloading a simple operation like string concatenation would be need to accomplished by using a method in the String class:
     String str1, str2, str3;
     //  ...
     str1 = str2.concat(str3);
whereas with the operator overloading this is written as
     str1 = str2 + str3;
This specific operation is so fundamental that it has been included as a special case operator in Java.

The capability of allowing user defined operators is not part of the Java language defined by Sun Microsystems. The need is clear especially when looking at non primative data types like complex arithmetic. Imagine the confusion if every time you wanted a simple arithmetic operation like:

     a = b + c * d;
you were forced to write:
     a = b.add(c.multiply(d));
doesn't this seem a bit cumbersome? It does to many programmers. Visual Numerics has also addressed this problem in the design and definition of their Java Numerical Library.

The Java Grande working group has been working on this problem too.

James Gosling has a positive opinion on this subject.

Bjarne Stroustrup wrote a preprocessor to the C language many years ago called "cfront" which was a complete language translation system, one of cfront's features was the operator overloading component of the C++ langauge. G&G Inc. has written a preprocessor, currently called "JFront", to add a similar feature to Java.


In the class definition file, create a method with the word "operator" followed immediately by a legal Java operator. Example:
class Complex {
         Complex operator+(Complex z2) {  
         // this will be changed to: "operatorplus" by JFront
            Complex c_add = new Complex();
            c_add.real_part = real_part + z2.real_part;
            c_add.imag_part = imag_part + z2.imag_part;
	    return c_add;
to use the operator, simply use it as a normal arithmetic operator with an object of the correct class on the left.
  Complex a, b, c;
  a = b + c;    // this will be changed to: " a = (b.operatorplus(c)) " by JFront
Note: the extra parenthsis are included so that operator precedence is correctly ordered for more complicated statements. Try it out.


Questions? Comments? Any errors? This is a new program. We would totally suprised if there wasn't a bug in it somewhere.
Would you be willing to pay for this program? How much?
Help us out and send a note to G&G Inc. with our online form or send us regular e-mail

Product Plans

None. Given Guy Steele's talk at OOPSLA98, it appears that this kind of functionality will be added directly into a future version of Java. (see: Growing a Language, Guy L. Steele Jr.) For the near term, however we would rather see this preprocessor become a part of a mainstream Integrated Development Environment. However after contacting all of the IDE developers we could find, so far not one has shown any great interest in purchasing and intergrating our Jfront program. IDE vendors contacted: Progress(Apptivity), Sun(Workshop), Step Ahead Sw(Javelin), Tech-Tools(Kawa), Sybase(PowerJ), Penumbra(Mojo), Asymetrix(SuperCede), PFU(teikade), Imperial SW Tech(Visaj), IBM(VisualAge), Symantic(Cafe), Xelfi(Xelfi).

Garnatz and Grovender, Inc.
17 South 1st St. Apt A1202
Minneapolis, Minnesota 55401-1831 USA

e-mail: philg@gginc.biz
URL: http://www.gginc.biz

Copyright © 1999, 2016 Garnatz and Grovender, Inc.