This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Doubt in arrays

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic