Question

Why does Java provide the ability to call from a Java program native methods that are...

Why does Java provide the ability to call from a Java program native methods that are written in, say, C or C++? Provide an example of a situation in which a native method is useful.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Actually we must first know that all programs written in Java are intended to be platform I/O independent means that we can run those Java programs almost on any platform be it C, C++, Windows, etc. Hence, following are the reasons why Java provides ability to call native methods from a Java program:

  • Unfortunately, the Java language does not provide facilties like accessing various system resources such as reading/writing from/to Input-Output devices or ports. So, that is why Java provides the ability to use native methods. Hence, an example of situation where using a native method is useful is when we want to perform such system specific I/O operations such as reading, writing from/to Input-Output devices or ports. So, to use such features we must first have to write program codes in a language that supports the above features like system specific I/O operations (like C/C++). The crux is that Java provides the ability to call from a Java program, native methods that are written in some other language like C/C++ just to increase the code functionality and extend its execution to different platforms, thus making the Java programs platform independent. The main purpose of native methods is to interface with system calls or libraries that are written in other programming languages in order to make direct system calls, which is not provided by the Java language itself. However, we must know that if a Java program calls a native method that is written in some other language like C/C++, then it won't remain architecture-neutral anymore.
  • Also, another reason is that, Java, as we know is an object-oriented language and it offers the concept of code reusabilty. Hence, Java allows calling such native methods that are written in some other programming language to implement code reusability in order to save time of the user.
  • Another reason is that using native methods allows a user to write codes that perform much faster due to better CPU assembly instructions being present in the native methods that belong to languages like C/C++. Hence, the user can easily use these native methods in case he/she wants to write a code for a time-critical part of the program where high execution speed is important and the program is required to run and perform operations/output in minimum possible time.

Thanks!

Add a comment
Know the answer?
Add Answer to:
Why does Java provide the ability to call from a Java program native methods that are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java code provide the desired Big O notation methods and then call those methods in...

    In Java code provide the desired Big O notation methods and then call those methods in the main. Problem: Let n be the length of an integer array a, for each of the following classes ?(log(log(log(?)))) ?([log(?)] 3 ) ?(? 5 ) ?(4 ? ) ?(?!) ?(? ? ) write a method that takes as sole input an array a of length n, non-recursive, whose running time belongs to only one of the above classes.

  • A java program for this question please! Recursion: A word is considered elfish if it contains...

    A java program for this question please! Recursion: A word is considered elfish if it contains the letters: e, l, and f in it, in any order. For example, we would say that the following words are elfish: whiteleaf, tasteful, unfriendly, and waffles, because they each contain those letters. Write a recursive method called elfish(), that, given a word, tells us whether or not that word is elfish. The signature of the method should be: public static boolean elfish(String word)...

  • Write a Java program that has the following methods: findSum - a method that takes in...

    Write a Java program that has the following methods: findSum - a method that takes in three integers and returns the sum of these three integers; findAverage - a method that takes in three integers and returns the average of these three integers (as a double value) Within the main method read in 3 integers from the user and call above two methods by passing the three integers. Your program should print the user provided three integers and results of...

  • please code in java Write a program that illustrates rethrowing an exception. Define methods CISP401Method and...

    please code in java Write a program that illustrates rethrowing an exception. Define methods CISP401Method and CISP401Method2. Method CISP401Method2 should initially throw an exception. Method CISP401Method should call CISP401Method2, catch the exception and rethrow it. Call CISP401Method from method main, and catch the rethrown exception. Print the stack trace of this exception. The following is the result of the program.

  • In this lab, you complete a partially written Java program that includes methods that require multiple...

    In this lab, you complete a partially written Java program that includes methods that require multiple parameters (arguments). The program prompts the user for two numeric values. Both values should be passed to methods named calculateSum(), calculateDifference(), and calculateProduct(). The methods compute the sum of the two values, the difference between the two values, and the product of the two values. Each method should perform the appropriate computation and display the results. The source code file provided for this lab...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • Write a Java program that first reads a positive integer from the user - let's call...

    Write a Java program that first reads a positive integer from the user - let's call it howMany. Then the program reads howMany pairs of integers - let's call them n1 and n2. For each pair, the program determines if the first component in the pair is a multiple of the second component, in other words, if n1 is a multiple of n2. For example, if numberOfPair is 5, your program will read 5 pairs of integers, and for each...

  • Write a JAVA program with methods that initializes an array with 20 random integers between 1...

    Write a JAVA program with methods that initializes an array with 20 random integers between 1 and 50 and then prints four lines of output, containing 1)The initialized array. 2)Every element at an even index. 3)Every even element. 4)All elements in reverse order. Requirements (and hints): 1. Build a method that takes any integer array as input and prints the elements of the array. ALL printing in this lab must be done using a call to the printArray method and...

  • In Java, write your own methods to do the following: LinearSearch BinarySearch SelectionSort MergeSort InsertionSort BubbleSort...

    In Java, write your own methods to do the following: LinearSearch BinarySearch SelectionSort MergeSort InsertionSort BubbleSort On the given array with the following elements ; Array : 87, 39, 3, 5 ,9 ,7 ,27,1 , 8 ,6 (use this Array as data for all methods except the BinarySearch method). All your methods will be in a class. Write a tester program that will call each of the listed methods from the class above using the given Array as data. For...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT