• 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

Array Cast

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class CheckArrayCast
{
public void getArray(long[] x)
{
for(int i = 0; i<x.length;i++)>
{
System.out.println("# "+i+" = " + x[i]);
}
}


public static void main(String[] args)
{
int[] small = {1,2,3,4,5,6,7,8,9,0};
CheckArrayCast c = new CheckArrayCast();
c.getArray(small);
}
}
the following code gives me a compiler error
Incompatible type for method. Can't convert int[] to long[].
c.getArray(small);
Could anyone please explain.. i thought i knew casting pretty well.

------------------
denice the menace
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can convert an int into a long but not an array (which is an Object) of ints to an array of longs. They are two different types. That's why the compiler is complaining: Incompatible arg type.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can, of course, convert or cast an int to a long - but you can't convert or cast an array of ints to an array of longs. Remember that an array is an object! If an array is of primitive types, it can only be cast or converted to an array of the same primitive types, Object, or Cloneable. However, if you have an array of object references, you can follow the same casting and conversion rules as you use for object references.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic