• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Issue on arrays

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
0x130c19b is 19972507
So hashcode wins!
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, is it. sorry then.
 
Forget Steve. Look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic