• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

about native

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
folks,

i have a doubt about native methods.how and when native methods are implemented.

how and when the JVM will implement these native methods?there are lot of native methods in the API like

Runtime.getRuntime().gc(); new Object().hashcode();

if you call Runtime.gc().from where this gc() method will be called??
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"native" means that the body of the method is not written in Java, but rather in a "native" language like C/C++. The mechanism through which you would do this (and yes, you can do this yourself in your own classes) is called the Java Native Interface, or "JNI". You can learn about JNI here.

Many methods in core classes like Object, Runtime, and Thread are native because these classes provide some "magic" that can't be implemented in Java itself. For example, methods like wait() and notify() in Object can only be implemented by using operating-system calls to the platform's thread library.
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for your quick response.

now i am a little bit clear.but,who will implement these native methods?

i have just installed the j2se1.5.0_15 and now if i call something like this

System.out.println(objectname_of_myclass);

it should automatically call toString() of Object class as i haven't implemented toString() in my class.

but in source, the toString() method of Object class contains something like this

"
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
"

here toString itself is calling hashCode(); of Object class which is a native method. then,who has implemented this hashCode() method and where it is implemented ? is that JVM?
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have got a toString() method implemented in your class; it is inherited from the Object class, which you obviously know about. You mean you haven't overridden a toString() method.
The hashCode method is implemented somewhere inside the JVM; I am not sure where. It was implemented by whoever wrote that part of the JVM, which means different people for different implementations of the JVM.

And EF-H has told you already where you can learn to write your own native methods.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

here toString itself is calling hashCode(); of Object class which is a native method. then,who has implemented this hashCode() method and where it is implemented ? is that JVM?



I guess the "who" is the developers of the JVM. And depending on the platform, it is implemented in a shared object, or a DLL. If you really want to understand the plumbing, you should read the JNI that EFH recommended. But to try to answer your question in summary....

There is a method provided by the JVM to load a shared object (or DLL), and in general, classes use this method to load the libraries in the static initializer of the class (but it doesn't have to).

When you call a native method, Java knows that it is a share object method call, and sets up the call stack for it -- and then locates and calls the native method in the shared object.

Henry
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much guys for your replies. now, i have understood about native methods.

so, now i can conclude that native methods mechanism is one of the things which makes java as platform independent?is it or not?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic