Java JNI Interview Questions And Answers
Download Java JNI Interview Questions and Answers PDF
Strengthen your Java JNI interview skills with our collection of 9 important questions. These questions will test your expertise and readiness for any Java JNI interview scenario. Ideal for candidates of all levels, this collection is a must-have for your study plan. Download the free PDF to have all 9 questions at your fingertips. This resource is designed to boost your confidence and ensure you're interview-ready.
9 Java JNI Questions and Answers:
Java JNI Job Interview Questions Table of Contents:
1 :: Define JNI functions and pointers?
JNI functions are those which are used by the developers to interact with JVM within a native method. Every JNI function receives a special parameter as its first argument – JNIEnv ; which points to a special JNI data structure of the type JNIEnv_ . One of the elements of JNI data structure is a ‘pointer to an array’ generated by JVM, and each element of this array is again a pointer to a JNI function. A JNI function can be invoked from the native method by referencing these pointers. Every JVM provides a unique implementation of the JNI functions.
Read More2 :: Why does Java have different data types for integers and floating-point values?
The integer and floating-point types are different in terms of the consumption of the bits which represent them. There are certain data types which are rightly apt for speed and perfect memory utilization. This makes the java applications to execute with efficiency. For example the number 95 is within the range of byte, short, int and long integer types. The data type byte will occupy 8 bits, short occupies 16 bits, and int occupies 32 bits and long 64 bits. If the variable is restricted to hold only the values -128 to +127, then throughout the application it is wise to declare that variable as ‘byte’. Similar applicability is applied for the rest of integer types and floating-point types.
Read More3 :: What is finalize()? Is finalize() similar to a destructor?
The finalize() method of java is invoked by JVM to reclaim the inaccessible memory location, When the ‘orphan’ object (without reference) is to be released, JVM invokes the finalize() method, when the next garbage collection passes and reclaims the memory space. Choosing to use finalize() provides the ability for performing some important cleanup action at the time of garbage collection.
The destructor of C++ language will always destroy the objects. But Garbage collection is not destruction. In case, some functionality needs to be performed prior to garbage collection. The respective functionality must be hard coded by the developer. As Java has no destructor or similar concept, the process need to be embedded into a method for cleanup. The finalize() method can be overridden to perform this cleanup.
Read MoreThe destructor of C++ language will always destroy the objects. But Garbage collection is not destruction. In case, some functionality needs to be performed prior to garbage collection. The respective functionality must be hard coded by the developer. As Java has no destructor or similar concept, the process need to be embedded into a method for cleanup. The finalize() method can be overridden to perform this cleanup.
4 :: Explain the advantages and disadvantages of using JNI?
Advantages of JNI
Using JNI, we can access c and c++ code which adds performance boost to JAVA.
JNI allows JAVA to access some hardware features using other languages like c and c++.
Disadvantages of JNI
JNI uses native languages which mean it has portability issue.
Code debug is big problem for the developers who use JNI features in JAVA.
Read MoreUsing JNI, we can access c and c++ code which adds performance boost to JAVA.
JNI allows JAVA to access some hardware features using other languages like c and c++.
Disadvantages of JNI
JNI uses native languages which mean it has portability issue.
Code debug is big problem for the developers who use JNI features in JAVA.
5 :: What is Native Interface in JAVA?
Java Native Interface permits the developers to integrate the native code such as C or C++ into a java application. To take the advantages of C programming and implementing the Java GUI features together makes the applications more efficient and powerful.
JNI is used to handle situations where the entire application can not be written in java.
Read MoreJNI is used to handle situations where the entire application can not be written in java.
6 :: Explain Exception Handling in JNI?
JNI exceptions are handled by using the following:
* Throw( )
Throws an existing exception object. Used in native methods to rethrow an exception.
* ThrowNew( )
Creates a new exception object and throws.
* ExceptionOccurred( )
Determines the exception status of throws and not yet cleared.
* ExceptionDescribe( )
Displays the exception and stack trace
* ExceptionClear( )
A pending exception is cleared.
* FatalError( )
Causes a fatal error to raise and does not return.
Read More* Throw( )
Throws an existing exception object. Used in native methods to rethrow an exception.
* ThrowNew( )
Creates a new exception object and throws.
* ExceptionOccurred( )
Determines the exception status of throws and not yet cleared.
* ExceptionDescribe( )
Displays the exception and stack trace
* ExceptionClear( )
A pending exception is cleared.
* FatalError( )
Causes a fatal error to raise and does not return.
7 :: What is Java Native Interface (JNI)?
JNI stands for Java Native Interface. It is an interface between Java, applications and libraries that are written in other languages. JNI is solely used to interact with “native” code (code written in system level language such as C). For instance, JNI enables the usage of C libraries and C programs to be used in Java programs.
Read More8 :: Describe exception handling in JNI?
JNI exceptions can be handled by using C++ exception handling. Using throw and catch blocks of C++ and invoking those methods through JNI is one of the solutions. JNI has built in functions for handling exceptions, which is a better choice.
The following methods can be used for handling exceptions:
throw() : An exception object is thrown. It is used in native method for rethrowing an exception
throwNew() : Creates a new object of an exception and throws it.
exceptionDescribe() : The stack trace and the exception will be printed
exceptionOccurred() : It is used for determining whether an exception is thrown
exceptionClear() : A pending exception will be cleared
fatalError() : A fatal error is raised and does not return anything.
Read MoreThe following methods can be used for handling exceptions:
throw() : An exception object is thrown. It is used in native method for rethrowing an exception
throwNew() : Creates a new object of an exception and throws it.
exceptionDescribe() : The stack trace and the exception will be printed
exceptionOccurred() : It is used for determining whether an exception is thrown
exceptionClear() : A pending exception will be cleared
fatalError() : A fatal error is raised and does not return anything.
9 :: Advantages and disadvantages of using Java Native Interface (JNI)?
Advantages:
Developers can take the advantage of Java platform.
Legacy code investments need not be abandoned.
Interoperability issues can be addressed
Implements two-way interface that allows java applications to invoke native code and vice versa.
Disadvantages :
It is difficult to debug runtime error in native code
Security risk is high
JVM causes down by the errors in JNI code and do not provide mechanism for recovery
Heavy weight process as all the queries use strings..
Read MoreDevelopers can take the advantage of Java platform.
Legacy code investments need not be abandoned.
Interoperability issues can be addressed
Implements two-way interface that allows java applications to invoke native code and vice versa.
Disadvantages :
It is difficult to debug runtime error in native code
Security risk is high
JVM causes down by the errors in JNI code and do not provide mechanism for recovery
Heavy weight process as all the queries use strings..