aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Issue on arrays 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 "Issue on arrays" Watch "Issue on arrays" New topic
Author

Issue on arrays

neha kanwal
Greenhorn

Joined: Jan 30, 2007
Posts: 11
Pls have a look at the following code:



The above code compiles fine. An instance of Object can store an object as specified in the line 2. But how can I save i in an array of an Object.

I failed to understand this logic. Can anyone pls help me in understanding this code.
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi



------------------------------------------------------------------
1. int i[]=null;
2. Object obj2=i;
3. Object obj1[]=i; //Compile Time error
-------------------------------------------------------------------
Object obj1=i;
Here one thing you have to remember.
In java arrays are objects.

at line 3 you are just assigning the reference of the integer array ie(i[])
to an object array.
now i[] and obj1 both are pointing to same arry in the heap.

[ April 30, 2007: Message edited by: anil kumar ]
[ April 30, 2007: Message edited by: anil kumar ]
neha kanwal
Greenhorn

Joined: Jan 30, 2007
Posts: 11
My main concern is -- Since arrays are object in java, so I understand when we say

obj1=i;

but how can I assign i to obj2[] ?

Lets be more precise...Pls look at the following code


Now the line 2 will generate compilation error .
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
I dont know how you are compiling .I am getting compile time error:cannot convert form int[] to Ojbect[]
[ April 30, 2007: Message edited by: raj malhotra ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Same:


Does not compile. (Java jdk 1.6)

However, this:
compiles OK.

That's because for any subtype S of a type T, S[] is a subtype of T[] (covariance of arrays).
[ April 30, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi All,

Hey i have so much of doubt in line2 and line 3. Please explain me the below code

1. int i[]=null;
2. Object obj2=i;
3. Object obj1[]=i;

Thanks All
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
------------------------------------------------------
1. int i[]=null;
2. Object obj2=i;
3. Object obj1[]=i;
-------------------------------------------------------------

primitive array extends Object
means
if you declare int a[]=new int a[20];

Object o=a;

object reference array extends Object

means like this
Object
| |--int a[]
| |--long a[]
|
|-----object a[]
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Anil,
Thats fine it extends Object but in the below mentioned code. My doubt is do this line generates Object o=a;
a runtime error.

int a[]=new int a[20];
Object o=a;
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

arora

here we are passing the reference of sub type to super type

if you have doubt execute this code

class check
{

public static void main(String... args)
{
int a[]=new int[20];
a[0]=10;
Object o=a;
System.out.println("The value is :"+o);
}
}

it will print hashcode of the array
[ April 30, 2007: Message edited by: anil kumar ]
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Anil,
This statement Object obj1[]=i generates a compile time error but how about the statement mentioned below.

obj1=i;
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

arora

i have shown the tree structure above see that one once . You will get a an idea
you will get compile time error
Thanks
Anil Kumar
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
Anil wrote:


it will print hashcode of the array


That's incorrect. It prints the memory address of the object.

Try this:


prints:
The value is :[I@130c19b
The value is :19972507
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
0x130c19b is 19972507
So hashcode wins!
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Thanks everybody
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
Oh, is it. sorry then.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue on arrays