Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java new byte array of size 2^32

Tags:

java

In Java, it won't allow me to use a long for something like this:

long size = 0xFFFFFFFF; //2^32-1

byte [] data = new byte[size];

And an int can only go as high as 0x7FFFFFFF (2^31-1). Is it possible to declare a byte array of this size?

like image 806
Eric B Avatar asked Nov 16 '12 18:11

Eric B


2 Answers

Answer is NO as this is the max possible initialization:

    int size = Integer.MAX_VALUE;
    byte [] data = new byte[size];
like image 150
Yogendra Singh Avatar answered Oct 21 '22 06:10

Yogendra Singh


Declare the size as an int and try again:

int size = 0x7FFFFFFF; // 0x7FFFFFFF == Integer.MAX_vALUE == 2^32-1

An array can only be declared to have a positive int size, not a long. And notice that the maximum int positive value (and hence, the maximum possible size for an integer array, assuming there's enough memory available) is 0x7FFFFFFF == Integer.MAX_vALUE.

like image 34
Óscar López Avatar answered Oct 21 '22 05:10

Óscar López