• 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

conversion from int[] to long[]

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can i convert a int[] to long[] .. ???
automatic conversion dosent happen..
public class A {
public static void main (String[] args) {
int sn[] = {1};
get(sn);
}
public static void get(long []in){
//do somting}
}
it gives error cant convert int[] to long[]
can it be done by casting ??? how ???
TIA
anil
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is correct you cannot get automatic conversion of arrays even if their type will automatically convert. you have to iterate through the array and convert each element. actually since you are going from int to long you can just assign elements from one array to the other.
int[] array1;
long[] array2;
//the rest
for(i=0,i<array1.length,i++) {>
array2[i]=array1[i];
}
I think that will work

[This message has been edited by Randall Twede (edited January 23, 2001).]
 
anil bisht
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cant we use casting ???
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you cant cast from one array type to another
It is not an arbitrary rule. I think it is because array size is fixed when it is created and cant be changed
I should go to bed

[This message has been edited by Randall Twede (edited January 23, 2001).]
 
anil bisht
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Randall
it works fine for the array of objects
this code compiles fine ...
Object[] ov;
String[] sa = { "Green", "Blue", "Red" };
ov = sa; // this is ok ..this works fine
String []sb;
sb = (String [])ov;
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I know what I am going to say is slightly unrelated to the original question but I would still like to add this point.
In case of object arrays, if the individual elements can be converted(down the hierarchy), their object arrays can also be assigned to each other. In case of primitive data-types arrays, even if individual elements can be converted to each other(as in a conversion from int[] to long[]), the arrays' reference can not be converted to each other.
In case of object arrays, the following is allowed:
class SUPERCLASS {}
class SUBCLASS extends SUPERCLASS {}
SUPERCLASS[] supArr = new SUPERCLASS[10];
SUBCLASS[] subArr = new SUBCLASS[10];
supArr = subArr; // allowed because individual elements can be converted.
subArr = supArr;// not allowed because inidividual elements can also not be casted like this. they need explicit casting
subArr = (SUBCLASS[])supArr; // allowed because individual elements can also be casted with explicit casting.
Point is that in case of object arrays, rules are the same as for their individual elements but in case of primitive data-type arrays, rules are not the same as they are for individual items.
That's it.
Thanks for reading...smile...
rgds,
Roshan
 
I am mighty! And this is a mighty small ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic