Why can't I pass a short array to java for a method that takes an int array?

I have a method that takes an int [] parameter. I want to give him a lot of shorts. But all I get is "incompatible types: short [] cannot be converted to int []". Why is this? example:

short[] arr = new short[2];
arr[0] = 8;
arr[1]=9;
example(arr);

example(int[] anArray){
}

As far as I know, the abbreviation from int is just expanding, so it should be automatically entered, right? How to convey it then? Thank.

+4
source share
3 answers

, short[] int[]. , short int, Java , short int. .

Java-API , Arrays.sort, .

short[], int[], , example(short[] anArray).

JLS, 5.1, , , , 5.1.12, " ":

> Any conversion that is not explicitly allowed is forbidden.

, .

+4

Java , short ints, .

, IS-not-A int.

0

int , , , :

short x = 4;
int y = x;

.

, .

22 :

short to byte char

char

int , char

long to byte, short, char int

float to byte, short, char, int long

double to byte, short, char, int, long float

, .

double float IEEE 754 (Β§4.2.4). , , . NaN float NaN, .

T , n , n - , . , .

a char T , n , n - , . , , 16- .

for more information: http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3

0
source

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


All Articles