This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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: 2341
posted
0
An object array is not the same as an object.
mambe nanje
Ranch Hand
Joined: Feb 22, 2006
Posts: 31
posted
0
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
posted
0
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
posted
0
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
posted
0
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..!!