File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array 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 "Array" Watch "Array" New topic
Author

Array

swarupa patil
Ranch Hand

Joined: Feb 22, 2006
Posts: 62
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

Joined: Feb 07, 2005
Posts: 2367
An object array is not the same as an object.
mambe nanje
Ranch Hand

Joined: Feb 22, 2006
Posts: 31
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;


Da Clone in programming world
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
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

Joined: Mar 08, 2006
Posts: 23
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

Joined: Feb 28, 2006
Posts: 47
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array