• 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

Array

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi every one
I am studying on array part . I read that array in
java are Objects .is it Correct ?
If Yes then why it is showing compile time error

int j[][]={
{ 12,12,34,56,76,34,78,78},
{ 12,12}


};

Object obj[] = j;
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An object array is not the same as an object.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a two dimensional array cannot be passed to a one dimensional array, it will not cast it, thought the integer primitives can be boxed to Integer then cast to implicitly to objects so if u change it like this it will go

Object[][] obj=j;
 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
Good question.

I think it will work as we are assigning an object only...

Definitely it will compile and run....


package temp;

class Temp
{
public static void main(String args[])
{
int j[][]={
{ 12,12,34,56,76,34,78,78},
{ 12,12}
};
Object obj[] = j;
for(int i=0;i<obj.length;i++)
System.out.println(obj[i]);
}
}
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

2 things....

I need to know the reason to this. I'm swatting up for this test as well...

Am I right in saying that the reason it works is because the declaration/initialisation statement;

int j[][]={{ 12,12,34,56,76,34,78,78}, { 12,12} };


causes (in effect) an array of Array objects to be created. Thus we have one array (of array objects).

the following line works;

Object obj[] = j;

because we are assigning an array of array objects (j) into an array of objects (obj).

PLEASE tell me thats right!

ASIDE and completely unrelated. Everything... all java classes inherit from the base class Object, yes. I mean EVERYTHING (except primitive types). yes?

Have a nice day.

GrahamO

Originally posted by Sandeeep Vaid:
Hey,
Good question.

I think it will work as we are assigning an object only...

Definitely it will compile and run....


package temp;

class Temp
{
public static void main(String args[])
{
int j[][]={
{ 12,12,34,56,76,34,78,78},
{ 12,12}
};
Object obj[] = j;
for(int i=0;i<obj.length;i++)
System.out.println(obj[i]);
}
}

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swarupa,

Array in Java is an object. Having said that, the way that an array object is instantiated and accessed is different from to that of a conventional object. Opcodes for conventional objects and opcodes for array objects, are different.

Coming back to your question, I just get that sneaky feeling that your code would get compiled as the code itself doesn't cause any breach of conduct..!!

Regards,
Priyanka.
[ March 08, 2006: Message edited by: Priyanka Kolamba Patabendige ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic