Friday, August 10, 2018

Jython and working with java's generic types.

Hello all!

Let's talk today about Java's Generics. I will demonstrate how you can work with these. As you work with other programmers' libraries, you may come across a library that is built around Java's generic types.

Using Jython to work with these types takes a little work. Trying to access them directly does not work. Why? It's the basic structure of Jytjpm tjat the system is dynamically typed, while Java is quite strict in that area. For example, if you, in the Jython interpreter, type:

i = 100

Then the type is 'int'. If you use the same variable name:

i = 100.5

The type is now a 'float' (or Double per Java). You do not need to declare the type you are using, as Java demands you do.

So, how to utilize Java's Generics? Simply by explicily casting your type prior to passing it to Java.

Allow me to demonstrate. I have created a simple class that actually takes TWO types. Both types can be the same, but they can be different also.

=======TwoTypes.java====================
public class TwoTypes<T, V> {
    T ob1;
    V ob2;
   
    // Constructor
    public TwoTypes(T o1, V o2) {
        ob1 = o1;
        ob2 = o2;
    }
   
    //Methods
    public void showTypes() {
        System.out.println("Value of type T is: " + ob1.getClass().getName());
        System.out.println("Value of type V is: " + ob2.getClass().getName());
    }
   
    public T getob1() { return ob1; }
   
    public V getob2() { return ob2; }
}

============end of java file ===============

The showTypes method will return the types as Java sees them. The getob1 and getob2 methods are used to return the data contained in the object that was created. If you think about it, one variable name can be used to contain two diffenent types!

After compiling the .java into a .class file, go to the directory where the .class file is stored and start your Jython interpreter. The following is from my own terminal, showing the demonstration of using the TwoTypes class.

==================
>>> import TwoTypes
>>> iOb = int(100)
>>> fOb = float(100.2)
>>> ttOb = TwoTypes(iOb, fOb)
>>> ttOb.showTypes()
Value of type T is: java.lang.Integer
Value of type V is: java.lang.Double
>>> # To see what's contained in the object.
>>> ttOb.getob1()
100
>>> ttOb.getob2()
100.2
>>> # You can even do operations with the objects!
>>>
>>>
>>> print "Adding the objects:", (ttOb.getob1() + ttOb.getob2())
Adding the objects: 200.2

===============

Very cool, indeed.

But really, the whole point of Java's Generic types is to ensure type safety, which Jython doesn't care about due to its dynamic nature. You can assign one TwoTypes object to another and Jython will do it happily. But, you will get strange and unexpected bahavior (and exceptions) when attempting to pass an incompatible object to java. This demonstrates what you can do to appease Java's requirements and get Jython and Java working smoothly.

As always, if you have questions or comments, don't be shy.

Happy coding!

No comments:

Post a Comment