Possible lossy conversion from long to int, JAVA

I am trying to pass a long value to a method and use it there to create a long array. However, when creating an array

I get the error "possible loss of conversion from long to int",
long[] array = new long[n];

although I did not use integer values.

import java.util.Scanner;
import java.util.ArrayList;

public class test{
public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    long n = input.nextLong();

    System.out. although("result is " + n + " is " + testing(n));
}

private static long testing(long n){
    long[] array = new long[n];

    return 0;
  }
}
+4
source share
1 answer

Array sizes can only be int. The compiler expects this type, but you pass in the type long. You can change the type of argument passed to int, and make the appropriate changes.

For completeness, here's what JLS says about variables in an array

, integer.

+5

Source: https://habr.com/ru/post/1569649/


All Articles