Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array objects

 
venkatesh pendharkar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
when we define String s[]=new String[4]; 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure. In the Java Language Specification (section 4.3.1), it says that
An object is a class instance or an array.
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the links I posted do link to the array section of the JLS.
 
venkatesh pendharkar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI thanks all for reply.
Just to check I wrote the following code
Object s[]=new Object[5];
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??
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'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;
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer the hierarchy part of the original question.

String is a subclass of Object
Object[] is a subclass of Object
String[] is a subclass of Object[]

Basically, an object array is a subclass of object, and the other array types inherit from the object array in a hierarchy that mirrors the non-array objects.

Henry
 
venkatesh pendharkar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as you said Object[] is a subclass of Object, so is there any class defined in java which represents Object[] .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic