JCOBridge guarantees to the .NET developer a simple and direct way to write code running within JVM. From .NET, using the Dynamic Language Runtime feature, it is possible to write code like you are writing a Java code. The subsystem translates all requests into JVM context, executes them within the JVM and returns back the result.
The use of dynamic code in C# simplify the coding and results in “writing Java code in C# files”. Look at the snippet below:
double a = 2;
double b = 3;
double c = Math.PI/2;
/*Dynamic code*/
JVM.ImportPackage("JavaClass");
var jCInstance = DynJVM.JavaClass.@new();
var result = jCInstance.add(a, b);
var sin = jCInstance.sin(c);
With the dynamic access a developer doesn’t need to call the Invoke method to execute java code, the developer can simply call the Java method.
The snippet below shows how to obtain the same results using the direct interfaces.
double a = 2;
double b = 3;
double c = Math.PI/2;
/*Not Dynamic code*/
JVM.ImportPackage("JavaClass");
IJavaObject jCInstance = JVM.New("JavaClass") as IJavaObject;
double result = (double)jCInstance.Invoke("add", a, b);
double sin = (double)jCInstance.Invoke("sin", c);
From the point of view of the results both code snippets are equivalent.
The dynamic code is more clear and easy to use, but this come at a price. The dynamic object shall figure out what to do with syntax after every dot, and need to understand what to do. This internal search for the meaning of the syntax consumes resources.
The direct interfaces code is more complex to write, is less intuitive, but in situations similar to the above example the performance comparison figure out the code based on direct interfaces is 250x time faster.
The performance gain can be simply ignored if you need to call few objects/methods but can result in poor performances if you need to do thousands of calls.
With latest releases of JCOBridge there are new classes which helps the interaction with the JVM using the direct interfaces. The JNet GitHub repository contains, starting from JNet version 1.5.2, almost the full Java SE 11 classes managed with the new paradigm.