• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in arrays

 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Object o[]=new Object[]{ "aaa", new Object(), new Vector(), new String[]{"anil","kumar"} };//line 1

The above statement is compiling fine.

String s[]=new String[]{"anil",new String[]{"kumar","raj"}};//line 2

Line 2 is not compiling why?

I thought like this.


Here s contains references which are pointing to an object in the heap.So here arrays are also objects.So there is no problem with line 1 ,But the line 2 is giving compiler error why?


Can any body tell me?

Thanks

Anil Kumar
[ May 31, 2007: Message edited by: anil kumar ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object reference variable can hold array of any dimension.

Object o1 = new String[5][4]; //OK
Object o1 = new String[5][4][4]; //OK

new String[]{"kumar","raj"}; is an array object and ref of object can't
be assigned to the String ref variable.





Thanks,
[ May 31, 2007: Message edited by: Chandra Bhatt ]
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thank you man

i think it would be better to say like this

ref of arrayobject of String type can't
be assigned to the String ref variable.


Thanks

Anil Kumar
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yeah thats true!



Any array is an instance of Object. Ref variable of Object can hold object
of any class.

Thanks,
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


says, the new object created is a reference to an array, which can not be assigned to reference variable to a String. Its as if like



Thats why the compiler throws an error.
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s[] = null;

Here s can have Strings not the String arrays.
An object of String is different from an Object of String array.
Here s is an object of String arrray.

String s1 = null;

Here s1 is the object of String class.

so, s1 can contain a bunch of String objects not the String array references.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi krishna,


so, s1 can contain a bunch of String objects not the String array references.


I think it should have been for the reference variable "s" and NOT "s1".

Aint I?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic