• Post Reply Bookmark Topic Watch Topic
  • New Topic

arrays in java  RSS feed

 
Douglas Chorpita
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that arrays in Java are a subclass of Object. I also recently found out that arrays implement both the Cloneable and the Serializable interfaces.


Is there any source code for arrays? The class Object and the interfaces mentioned all have source code.

Or are arrays simply an internal aspect of the compiler like primitives? I am trying to figure out arrays up close?

Any help is highly appreciated!!!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are asking about implementation details of Java datatypes, I am moving this topic to Java In General (Intermediate).
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "source code", as it is, would be in C, and would be part of the JVM implementation. There's no Java code.
 
Douglas Chorpita
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method "Object newInstance(Class, int);" of the class "Array" in java.lang.reflect calls the NATIVE method "Object newArray( Class, int)".

Does this native method hook in to the same (native) code that generates a "normal" array?

This "Array" class appears to be a (Java source) API that maps to the native code that implements ("normal") arrays.

It is interesting that "length" is a PUBLIC final variable (the compiler knows this), but there is no file that describes this class definition.

Is there any literature on this topic?
[ July 22, 2006: Message edited by: Douglas Chorpita ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "Arrays" class is a collection of utilities for working with arrays; it has nothing to do with the implementation of arrays themselves. It's a relatively new class, added in JDK 1.4, I believe; Java got along for many years without it.

Yes, I'm sure that Arrays.newInstance() invokes, ultimately, the same code that executes when you use "new" to create an array.

The fact that "length" appears as a public final field in an array object is handled by the compiler; it's nothing at all like a normal Java member. Given a Java array object, there's a specific bytecode that returns its length: its mnemonic is "arraylength".

"length", along with other details about arrays, is documented in the Java Language Specification.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Arrays class has been around since JDK 1.2; it was introduced at the same time as the collections framework, and offers similar functionality to the Collections class, but for arrays. But yeah, it isn't the answer to Douglas' questions. Douglas, most of the functionality of arrays is implemented in the JVM directly, not as part of any class. Arrays are not classes in the usual sense; they are a category all their own.
 
Douglas Chorpita
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two different classes, "Array" and "Arrays". You are getting them confused.

After looking at the code and reading a bit, I have have come to the following conclusions:

Correct me, if I am wrong....

"Arrays" is a class that carries out "higher-order behaviors" for "normal" arrays like various kinds of sorting (merge, quick, etc.), binary search, testing array equality, etc. It is in java.util and does NOT have any native methods.

"Array" (in java.lang.reflect) is a class that hooks into to "core array operations", like creating a (single or multi-dimensional) array, getting and setting element values, initializing an array. It allows complete use of "normal" arrays, without having to use the "[]" operator. This class is useful for reflection. There are many native methods here. And I imagine, as discussed above, that calls to these methods map directly to the same code that creates and dereferences arrays and that sets and gets array values. These native methods have the same Exception signatures as "normal" array operations have.

I think the topic of arrays is a bit "under-documented" in Java. There is a lot of effort put into explaining the behavior and use of arrays, but very little information about how the compiler and the JVM fit together to make arrays work (parsing the syntax, translating arrays into byte code, etc.). This must be rather complicated as both the compiler (regarding syntax) and the JVM (treating arrays as objects that are both serializable and cloneable) have a lot of work to do here. There must be a formal contract for how arrays get "encoded" into byte codes since the compiler must (in theory) work properly for every JVM.

Any thoughts?
[ July 23, 2006: Message edited by: Douglas Chorpita ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you'd learn a lot from reading the Java VM specification here. The whole implementation of arrays hides behind a very small number of dedicated bytecodes. There's one for creating an array of a given type and length, a few for storing different types into an array, a few more for retrieving different types, and one for checking the length. The VM spec does describe these things, but it's really far less complex than you imagine.

And yes, I'm sorry, I misunderstood which class you meant. The "Array" class doesn't have anything more to do with implementing arrays than the "Method" class has to do with implementing methods, or the "Field" class has to do with implementing fields. You probably would enjoy learning about the Java Native Interface, as well, to get an idea of what these native methods would look like. See here for the dirt on JNI.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!