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

Issue on arrays

 
neha kanwal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
------------------------------------------------------
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
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0x130c19b is 19972507
So hashcode wins!
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, is it. sorry then.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic