Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array

 
swarupa patil
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • 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;
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object array is not the same as an object.
 
mambe nanje
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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;
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • 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]);
}
}
 
Graham Walsh
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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]);
}
}
 
Prad Schikanov
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic