• Post Reply Bookmark Topic Watch Topic
  • New Topic

incompatible types  RSS feed

 
Costa lamona
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why this does not work ?

int i[] = new int[3];
long[] l = i;

while this works ?

String x[] = new String[3];
Object o[] = x;
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object is a base type for all objects in java. long is not a base type for int. The types long and int are primitive types, and therefore they have no relation to objects and the way objects work (although mysteriously an array can be converted to an Object type).

However, given that you can do this:

int i = 10;
long lng = i;

it wouldn't take a stretch of the imagination to believe you could do this:

int[] nums = {10, 20, 30};
long[] lnums = nums;
[ November 10, 2006: Message edited by: sven studde ]
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mohammed EL-Adawi:
why this does not work ?

int i[] = new int[3];
long[] l = i;

while this works ?

String x[] = new String[3];
Object o[] = x;



I agreed, with Mr Sven,

What you want to do is you are changing the base address.

Perhaps, I guess you want to do this.



And it is perfectly fine. But you cann't change the base address which is not compatible.
 
Costa lamona
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int[] ,long[] are references, and all java arrays are objects.
and this does not work

int[] nums = {10, 20, 30};
long[] lnums = nums;

you can't cast two (brothers) in the hierarchy, regardless it is upcast or downcast, because they both inherite from Object and does not inherite from each
other, while

Integer[] i = new Integer[3];
Number[] n = i;

is ok because Integer extends Number

and you can't

int[] x = new int[3];
Object[] o = x;

and I am confused
and I will ask this question on java intermediate

Thanks anyway
 
Costa lamona
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I was thinking, no need for more questions

int[] is array of integers which does not extend Object
while int[] it self extends Object



nice to talk you mohammed, see you

yes yes, nut case
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!