0

My question is how the JNI takes the returned value of a native method and give it to the JVM (push into native stack).

For example if i have a native method toString, and the JVM knows where is the code of toString method, how JNI call the method?

LeAdErQ
  • 45
  • 10
  • WTF? What does the return value of a JNI call have to do with a return value from "system()". Besides the word "return??? See also: [JNI String return value](https://stackoverflow.com/questions/15268426/jni-string-return-value) – paulsm4 Jan 03 '18 at 03:03
  • yes i agree with you, but my question is how calls a native method, and then how takes the returned value. If you want a different question but the same thing : How with C++ can I call a function from a other file and take the returned value from it.(without serialization) – LeAdErQ Jan 03 '18 at 03:08
  • @paulsm4 i want to know how this virtual machine works and no how to convert an array of chars to java string. – LeAdErQ Jan 03 '18 at 03:36
  • 1
    SUGGESTION: Study *ONE* method of "passing a return value" for *ONE* language on *ONE* platform. Understand how that works. Then apply that understanding by comparing with some *DIFFERENT* "return value" on some *DIFFERENT* platform and language (for example, Java JNI). Good article about C++ function calls: [x86 calling conventions](https://en.wikipedia.org/wiki/X86_calling_conventions) – paulsm4 Jan 03 '18 at 21:57

2 Answers2

0

Java does not receive C-style array of chars from native method. The callee builds a Java Object (e.g., String) and passes it by reference to the caller. With primitive types, e.g. int, the result is passed by value, but implementation details are private to JVM.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
0

Java only supports primitives and references. There is no String object types passed or return, only every a reference to a String. This reference is a void * from a C point of view and no conversion occurs to the data in the object.

i want to know how this virtual machine works and no how to convert an array of chars to java string

Convert char* to jstring in JNI, when char* is passed using va_arg

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130