• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object

 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



}
}
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm i thought 2D arrays are:

 
Esam Ahmed
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic