Tuesday, January 25, 2011

Block initialization of Java variables

Java provides a number of ways to initialize variables. Initializing complex data structures like lists and maps in Java could be tedious but there is a nicer way to do that through the inline initialization using a block. The block here is similar to the static block, but without the static keyword. For example:
 

String[] stringList=new String[10];
...
List list=new ArrayList(){{
for(int i=0; i<stringList.length; i++){
add(stringList[i]);
}
}};

this initializes a static ArrayList defined in a class by adding a list of strings also defined inside the class. However the strange thing is when a block is used to initialize variables inside a method environment, the variables used to initialize must have been declared final. For example:

    public int someFunction(final String[] stringList)    {

        List<String> list = new ArrayList<String>(){{
            for(int i=0; i<stringList.length; i++){
                add(stringList[i]);
            }
        }};
    }

Using non-final variables for instance initialization does not work. In the next post, I will try to post more on final, subclassing and method overriding using final.

Monday, January 24, 2011

Calling Matlab from Java

Matlab is an awesome thing for doing some heavy numerical computation and analysis. Though, Matlab is powerful in it's own right, it has wonderful support to run Java programs with-in the Matlab interface. Apparently, there is no easy way to call Matlab functions with-in Java and let Java read/print or use the output of those functions for some other purpose.

An article that seems to provide possible solution, calls for using a piece of code, MatlabControl class, that can be downloaded from here along with a patch that can be applied. The MatlabControl allows calling matlab functions with-in Java by creating a MatlabControl object and invoking the MatlabControl.blockingFeval function with arguments as function name and a list of arguments that the function takes. The output can be captured in a generic Object which can later be typecast depending on the expected output.

However, there is a major drawback with this. After setting the classpath in Matlab, a run of the corresponding Java code (invoking MatlabControl) in matlab makes the Matlab UI to hang and Matlab has to be restarted to do anything freshly. I figured out that it has to be invoked in a different thread so I tried the same but it hangs again.

In fact, if you are calling matlab functions at multiple places in your code or invoking MatlabControl with-in a for-loop or while loop and calling matlab functions for an indefinite number of times it never seems to work. Apparently, Mathworks provides the same kind of support to programs written in C or Fortran, much more easily. They only need to include the corresponding libraries and call the functions with the command to run. Hence, there is no easy way one can use the numerical computing power of matlab functions with-in the Java programs.

Recently, I came across another software from mathworks, Matlab Java Builder, that does some fancy stuff involving Java and Matlab. It converts the matlab programs in to Java classes that can be used stand alone with any other Java code without Matlab installed on the system. Perhaps, this might be helpful in using Matlab features with Java.