Thursday, November 10, 2011

Primitive type long as Array index


Tip: Using a long index value to access an array causes a compilation error.

Let's try it with small piece of code.

public class TestArray {

 public static void main(String[] args) {

  int [] a = {1, 2, 3};

  long i = 1;

  System.out.println(a[i]);

 }}

If you try to compile and run above code, you will get following compile time exception:

Type mismatch: cannot convert from long to int.

Use SCJP Champ to learn Java The Fun Way!
Available in Android Market

No comments:

Post a Comment