aspose file tools*
The moose likes Beginning Java and the fly likes Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object" Watch "Object" New topic
Author

Object

alfred jones
Ranch Hand

Joined: Apr 19, 2005
Posts: 279
class Test
{
public static void main(String[] args)
{String[] str={"IT","is","a","test"};
Object objs=str; // How this work
Object[] objts=str; // how this work



}
}
Esam Ahmed
Ranch Hand

Joined: Aug 10, 2005
Posts: 101
class Test
{
public static void main(String[] args)
{ String[] str={"IT","is","a","test"};
Object objs=str; // How this work
Object[] objts=str; // how this work

}
}

You have to consider two things here.

First, a reference of type Object that is > objs
and a reference of type Objcet[] that is > objts

Arrays are genuine objects in Java.
And Object is the mother of all classes i.e at the top of the hierarchy.


Here is the key:

A reference of an array can always be cast to a referece of type Object.
And a referece to array of type objects can be cast to a reference of type Object[]

In the example above: String[] str={"IT","is","a","test"};
This is essentially a two dimentional array: a String[] (i.e. String array) of Strings.

Here, str is reference to an array, so you can cast to type Object, i.e.
cast it to > Object objs=str;

And since, str is also a reference to an array of Objects (i.e. String within this String[], "IT, "is", "a", "test")
It can be cast to that of Object[] (Object array).
Thus >Object[] objts=str;


But if your code had the array as, say: int[] intArray = {1,2,3,4,5}
then you could only do the first cast, but not the second cast.

Why?

Esam.


Esam<br />SCJP 1.4, SCWCD 1.4
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Essam,
This is essentially a two dimentional array: a String[] (i.e. String array) of Strings.

Please could you explain why String[] str={"IT","is","a","test"};
is a two dimensional array.
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
hmmm i thought 2D arrays are:



I'm a Hood Ornament
Esam Ahmed
Ranch Hand

Joined: Aug 10, 2005
Posts: 101
U r right,
It is not a two dimentional array..!!
It is rather an array of String ...
Since String (s) are objects...
It is considered - an array of Objects
Thus the whole thing can be considered an array of Objects..

Thus the cast.. Object[] objArray = String[] strArray{"One", "two"} is valid
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
String[] str={"IT","is","a","test"};
Object objs=str; //
Object[] objts=str; //

after these lines, 3 references will point to 1 single instance

objs, str, and objts

just that objs is an Object and so you can only perform stuff allowed for an Object, for this reference.

and objts is an array of Objects, and in same way, you can only perform stuff allowed for this type of reference
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Originally posted by Abdulla Mamuwala:
Essam,

Please could you explain why String[] str={"IT","is","a","test"};
is a two dimensional array.


this is a single dimensional array
Esam Ahmed
Ranch Hand

Joined: Aug 10, 2005
Posts: 101
Hi All,
I embarassed myself. why did i even say that?

To sum it up: as i should have said...

All reference types are subtypes of Object type.
All arrays of reference types are also subtypes of Object[] type (arrays of primitive types are not)

Object[] is also a subtype of Object type.

Esam
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Object