HI when we define String s=new String; then i dont understand in java class hierarchy,s object is a subclass of which class?? is it directly a subclass of Object class or any other class. Because in one of the books I read that all object arrays inherit from Object. Also I want to know lenght property of arrays i.e. s.length is defined in which class & how come it is available for all the array objects.
This suggests that arrays are not class instances and therefore don't have to inherit from any classes. But in another section (here, find 10.7) it says that all arrays have public members including all the methods inherited from the Object class, except clone which is overridden, AND a public field called length. It is a final public variable; if you try to assign it "array.length = 12;" you will get a compiler error.
It might mean that arrays have all the methods of the Object class but don't inherit from a class. As I said: not sure.
Joined: Oct 13, 2005
You know I said to look in the JLS for section 10.7? This is what section 10.8 says:
Every array has an associated Class object, shared with all other arrays with the same component type. The direct superclass of an array type is Object. Every array type implements the interfaces Cloneable and java.io.Serializable. etc etc
If you're going to quote from the JLS, you might as well have pointed the OP to the section on arrays. It should answer most of the OP's questions.
Which you have just done . Ignore this post. [ March 23, 2007: Message edited by: Joanne Neal ]
Joined: Oct 13, 2005
But the links I posted do link to the array section of the JLS.
Joined: Apr 29, 2006
HI thanks all for reply. Just to check I wrote the following code Object s=new Object; System.out.println(s.getClass());
It prints the following: class [Ljava.lang.Object;
If instead of object we use String s=... then the output is : class [Ljava.lang.String;
So where is this class [Ljava.lang.String; class defined in Java??
Joined: Aug 05, 2005
'class [L<object type>;' indicates an array of <object type>, so 'class [Ljava.lang.String;' indicates an array of String objects. produces Object class = class java.lang.Object String class = class java.lang.String Object array class = class [Ljava.lang.Object; String array class = class [Ljava.lang.String;