This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes array objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "array objects" Watch "array objects" New topic
Author

array objects

venkatesh pendharkar
Ranch Hand

Joined: Apr 29, 2006
Posts: 106
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

Joined: Oct 13, 2005
Posts: 39103
    
  23
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

Joined: Oct 13, 2005
Posts: 39103
    
  23
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

Joined: Aug 05, 2005
Posts: 3606
    
  15
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 ]

Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39103
    
  23
But the links I posted do link to the array section of the JLS.
venkatesh pendharkar
Ranch Hand

Joined: Apr 29, 2006
Posts: 106
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

Joined: Aug 05, 2005
Posts: 3606
    
  15
'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
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
venkatesh pendharkar
Ranch Hand

Joined: Apr 29, 2006
Posts: 106
as you said Object[] is a subclass of Object, so is there any class defined in java which represents Object[] .
 
GeeCON Prague 2014
 
subject: array objects