• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

About Object class problem?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class gc1{
static Object xx;
public static void main(String[]args){
Object oa1=new Object[10];
Object oa2=new Object[0];
Object[] oa3=new Object[0];
System.out.println(oa1);
System.out.println(oa2);
System.out.println(oa3);
System.out.println(xx);
}
}//what's the different from oa1,oa2,oa3,xxand what's the meaning oa1,oa2,oa3,xx?I can't get it?
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Terrific question Tu Ran, I'll add this to my treasure chest ... it made my head hurt
This question started off innocently enough, but got more and more hairier as I pondered the possibilities!
I not sure what 'new Object[0]' returns? But let me take a crack at answering your posting!
1) oa1 is a reference to an object
2) oa2 is a reference to an object
the reason you can get away with these funny assignments to oa1 and oa2 has to do with the fact that an array in java is consider to be 'object', in fact it's a sub-object of class Object
proof that an array is a sub-object of class Object
class A {}
A a1 = new A[10]; // won't compile
A a2 = (A) new A[10]; // type casting doesn't help
A a3 = (A)(Object)new A[10]; // this works!!!
why does a3 compile while a2 fails? because we need to upcast and then down cast!

For example, given this class hierarchy
class A extends Object {}
class B extends Object {}
Object a = new A(); // this is valid
Object b = new B(); // this is valid
A a = new B(); // won't compile
A a = (A)(Object) newB(); // this will compile , need to upcast then downcast

Try it!


3) oa3 is an array of references to objects of class Object (incidentlly this array gets initialized and reports a size of zero!)
4) xx is just a static reference which get assigned a default value of 'null'

Can anyone explain what's going on with reference 'o3a'? and what 'new Object[0]' returns? Curious to find out!
[ March 02, 2002: Message edited by: Rajinder Yadav ]
 
Tu Ran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajinder Yadav
I am relly thanks your help~~in java~~
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read somewhere that we can declare an array with 0 length.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic