Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Why is jsni similar to the java native interface?


Asked by Amy Arellano on Dec 06, 2021 Java



That's what JSNI is all about. It's named similarly to the Java Native Interface (JNI) because it uses the same basic idea: declare a Java method "native" and then use another language to implement it. In the case of JSNI, that other language is JavaScript.
Additionally,
JavaScript Native Interface The JSNI interface is used as a gateway between your Java and JavaScript code. It allows you to create Java methods that have JavaScript code in the body, and then to have the JavaScript code call Java methods.
Moreover, JNIEnv – a structure containing methods that we can use our native code to access Java elements JavaVM – a structure that lets us manipulate a running JVM (or even start a new one) adding threads to it, destroying it, etc… 3.
And,
Writing JSNI methods is a powerful technique, but should be used sparingly because writing bulletproof JavaScript code is notoriously tricky. JSNI code is potentially less portable across browsers, more likely to leak memory, less amenable to Java tools, and harder for the compiler to optimize.
In this manner,
JSNI method signatures are exactly the same as JNI method signatures except that the method return type is left off. The following table shows type signatures: It demonstrates passing numbers, strings, booleans, and Java objects into JavaScript. It also shows how a JavaScript method can make a method call on a Java object that was passed in.