I really donβt know your usecase, but the question does not mean that you really need to convert the entire byte array to an array of shorts / ints / longs (otherwise a ByteBuffer or manuall conversion would be fine).
If you just need to treat any value in this array as short / int / long, all you have to do is assign it and the upcast will be implicit:
int i = byteArray[x];
In the same way, you can pass any value there to a method that expects long or something else, without the need for casting.
Edit:
As an answer to your comment, you can only use array covariance if you can allow the use of object wrappers instead of primitives (this implies that you control the code in the method you specify):
// needs to be Number because Byte does not inherit from Short myMethod(Number[] numbers) { // do something with number.shortValue() ...
And you can call it using:
myMethod(new Byte[10]);
But that would be even less acceptable than assuming the conversion of a byte[] to short[] .
source share