• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

incompatible types

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1249
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic