| Author |
System.arraycopy code?
|
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Hello, Condsidering the code of the final class java.lang.System and the arraycopy method: I don't see a method body. Where is it hiding? Thank You, -Dirk Schreckmann
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Hello, To clarify, I understand from the JLS:
8.4.3.4 native Methods A method that is native is implemented in platform-dependent code, typically written in another programming language such as C, C++, FORTRAN, or assembly language. The body of a native method is given as a semicolon only, indicating that the implementation is omitted, instead of a block.
I'm just curious if there is a way of finding the code for native methods. Thank You, -Dirk Schreckmann
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
I believe you can find this by downloading the SDK source code here. It ought to be buried in C code somewhere. Enjoy.
|
"I'm not back." - Bill Harding, Twister
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
|
Thanks again.
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
If anybody cares... The SDK source code referrences the JVM source code. Inside the HotSpot 2 JVM for Windows source code in this directory: src\share\vm\prims inside this file: jvm.cpp is this code: Good Luck, -Dirk Schreckmann
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Hello, There is quite a bit more in other source files - but who cares? And it won't let me post the code for some reason:
We cannot process your posting, because you have exceeded the maximum number of images allowed per post. The current maximum is 8.
Don't ask and I won't tell. Good Luck, -Dirk Schreckmann
|
 |
Tomas Hamal
Greenhorn
Joined: May 17, 2012
Posts: 2
|
|
Hello, i was looking for method body of this method, too. But from another reason. I'm interested with method parameters, how its possible
that this method "return" object to reference from atribut ... I don't know if you understarnd, what i mean. My english is bad. Its because of C++ or its possible in java too, but i don't know why.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
Welcome to the Ranch
I would warn you that the code might have been changed in ten years.
That method does not return anything; it uses the destination array as an output parameter.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
Tomas Hamal wrote:Hello, i was looking for method body of this method, too. But from another reason. I'm interested with method parameters, how its possible
that this method "return" object to reference from atribut ... I don't know if you understarnd, what i mean. My english is bad. Its because of C++ or its possible in java too, but i don't know why.
Of course, it is possible to do this in pure Java. It is just copying array elements around. Not sure why the java designers chose to do it natively, except maybe (and I am speculating) it was faster with access to the processor block processing instructions.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Tomas Hamal
Greenhorn
Joined: May 17, 2012
Posts: 2
|
|
|
Thank you for replying. I am trying to write our method for "editing" array, and I want that method can work with array of all data types. Dont know how to write it ... can you help me?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
Tomas Hamal wrote:Thank you for replying. I am trying to write our method for "editing" array, and I want that method can work with array of all data types. Dont know how to write it ... can you help me?
As you probably already figured out, if you need to work with a limited and known set of array types, then the answer is easy -- just use the instanceof operator to check the type, cast it, and then use a loop to copy it.
If the array types are not limited, or large enough that it is not feasible to code for each type, or are not known at compile time, then IMO, the best option is to use the reflection library.
First, use the getClass() method of the Object class to get an instance of the java.lang.Class class. This instance will allow you to confirm that your source and destination objects are indeed arrays, and that the elements are assignable to each other. Then, with the use of the java.lang.reflect.Array class, you can check the length of the arrays -- and get and set values from and to arrays, which of course, when called from a loop, will copy ranges of the arrays.
Henry
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
I think this topic is too difficult for “beginning”, so I shall move it.
|
 |
 |
|
|
subject: System.arraycopy code?
|
|
|