• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

parameter passing question for array reference

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
following example works fine:
void m(long l) {}
....
int i=99; m(i); //numeric promotion
but why does this one not?
void m(long[] l) {}
....
int[] i={1,2,3}; m(i);
I get a compiler error message: "m(long[]) cannot be applied to (int[])"
BTW: If I use an assignment, the compiler error says 'Incompatible Types'.
I thought that assignment of arrays (which are references) work fine as long as the source type (int) can be converted to the destination type (long).
-Bernd
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arrays containing primitives are special and are not treated like arrays containing references.
As you already know arrays are objects in Java, thus we have to do with a widening reference conversion issue.
From JLS 5.1.4 Widening Reference Conversions


The following conversions are called the widening reference conversions:
- blablabla
- From any array type SC[] to any array type TC[], provided that SC and TC are reference types and there is a widening conversion from SC to TC.


This is the key, int and long are not reference 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
An array of references can only be converted into another array of references, if the reference types held by the arrays are convertible .
Doesnt work for primitive arrays !
 
Bernd Stransky
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the info.
I guess I should start reading the JLS!
-Bernd
 
Bernd Stransky
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK,
so this works:
void m(Number [] n) {}
...
Integer I[] = { new Integer(1),new Integer(2)}
m(I);
:roll: Bernd
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes I wonder why though
 
reply
    Bookmark Topic Watch Topic
  • New Topic