Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.arraycopy code?

 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you can find this by downloading the SDK source code here. It ought to be buried in C code somewhere. Enjoy.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tomas Hamal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this topic is too difficult for “beginning”, so I shall move it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic