aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in arrays" Watch "Doubt in arrays" New topic
Author

Doubt in arrays

anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
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

Joined: Feb 28, 2007
Posts: 1707
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 ]

cmbhatt
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Apr 20, 2006
Posts: 3344



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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
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.


Thanks&Regards, Krishna.
SCJP1.4, SCWCD1.4, SCBCD 5.0
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in arrays