The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
August 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

The mysterious java array implementation

Posted by Kishori Sharan on August 27, 2000 at 4:23 PM

Hi

Java treats an array as an object. When you have an array like
long[] temp = new long[10] ;

then temp is an object. So, I jumped into a conclusion ( though I was wrong ) that long[] must be a class and tried to inherit a class from "long[]" class as follows.

class Test extends long[] {
public static void main ( String[] args ) {
// do somethong, if you want
}
}

When I tried to compile Test.java file then I got a big string of error with a message to report this bug to Sun and I did so. The compiler error message is at the end. But, this made me more curious to know about the array implementation in java and so, I tried to know the class of a java array and the class structure and I did the folowing experiments.
long[] temp = new long[10] ;;
String classname = temp.getClass().getName() ;

The class name for temp is [J . When I queried the class "[J" for its fileds, constrcutors, methods etc. then I got the following .

public abstract final class [J extends java.lang.Object implements java.lang.Cloneable , java.io.Serializable {

}

Now I have the following questions.
1. The class "[J" has been declared as "abstract" as well as "final" . But this combination of modifiers "abstract" and "final" is not allowed for declaring a class. The temp array is an object of class "[J" which is shown by temp.getClass().getName() , but the class "[J" is defined abstract. How is it possible to have an object of an abstract class ? I know this has been implemented by Sun's developers ( the big dogs ) and they are free to do anything inside java compiler and JVM and stop us from doing the same in java program. I would like to know how did they do it .
2. Since the class template of class "[J" shows that it inherits java.lang.Object class and implements Cloneable interface you can use all the methods defined in java.lang.Object class with an array object like temp.equals() , temp.hashCode ( ) etc. If you want to have a clone of your array you can have it as follows.
long[] temp = {1,2,3 }
long ll ;

// Get a clone of temp array
ll = temp.clone() ; // Valid call

We also use a property named "length" for java array. But, it is neither defined in java.lang.Object from which [J class inherits , nor in [J class itself. Since temp is an object and its class inherits Object class and implements Cloneable and Seriazable interfaces then the "length" property must be defined somewhere in these classes or interfaces. But, query revealed it is defined nowhere in those classes and interfaces. I would like to know the place where this mysterious "length" property for an array comes from.

Any input about the array implentation in java is greatly appreciated.


Thanx
Kishori

//////// Compiler exception /////////////

Program is as follows:
/*******************************************************/
public class Test extends long[] {
public static void main ( String[] args ) {

}
}

/****************************************************/


/*** Compiler Error ************

An exception has occurred in the compiler (1.3.0). Please file a bug at the Java Developer Connection (http://java.sun.com/cgi-bin/bugreport.cgi). Include your program and the following diagnostic in your report. Thank you.
java.lang.NullPointerException
at com.sun.tools.javac.v8.comp.Flow._case(Flow.java:746)
at com.sun.tools.javac.v8.tree.Tree$Apply.visit(Tree.java:785)
at com.sun.tools.javac.v8.comp.Flow.analyze(Flow.java:321)
at com.sun.tools.javac.v8.comp.Flow.analyzeExpr(Flow.java:339)
at com.sun.tools.javac.v8.comp.Flow._case(Flow.java:719)
at com.sun.tools.javac.v8.tree.Tree$Exec.visit(Tree.java:699)
at com.sun.tools.javac.v8.comp.Flow.analyze(Flow.java:321)
at com.sun.tools.javac.v8.comp.Flow.analyzeStat(Flow.java:394)
at com.sun.tools.javac.v8.comp.Flow.analyzeStats(Flow.java:413)
at com.sun.tools.javac.v8.comp.Flow._case(Flow.java:520)
at com.sun.tools.javac.v8.tree.Tree$Block.visit(Tree.java:492)
at com.sun.tools.javac.v8.comp.Flow.analyze(Flow.java:321)
at com.sun.tools.javac.v8.comp.Flow.analyzeStat(Flow.java:394)
at com.sun.tools.javac.v8.comp.Flow._case(Flow.java:488)
at com.sun.tools.javac.v8.tree.Tree$MethodDef.visit(Tree.java:441)
at com.sun.tools.javac.v8.comp.Flow.analyze(Flow.java:321)
at com.sun.tools.javac.v8.comp.Flow.analyzeDef(Flow.java:379)
at com.sun.tools.javac.v8.comp.Flow._case(Flow.java:456)
at com.sun.tools.javac.v8.tree.Tree$ClassDef.visit(Tree.java:402)
at com.sun.tools.javac.v8.comp.Flow.analyze(Flow.java:321)
at com.sun.tools.javac.v8.comp.Flow.analyzeDef(Flow.java:379)
at com.sun.tools.javac.v8.JavaCompiler.compile(JavaCompiler.java:380)
at com.sun.tools.javac.v8.Main.compile(Main.java:247)
at com.sun.tools.javac.Main.main(Main.java:16)
Process completed with exit code 4




Replies:
  • I too join u Santosh Kumar November 16, 2000 at 7:26 AM (0)

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us