When I try to make a very large boolean array using Java, such as:
boolean[] isPrime1 = new boolean[600851475144];
I get a possible loss of precision error?
Is it too big?
The results show the maximum size is 2,147,483,645. The same behavior can be observed for byte, boolean, long, and other data types in the array, and the results are the same.
The boolean array can be used to store boolean datatype values only and the default value of the boolean array is false. An array of booleans are initialized to false and arrays of reference types are initialized to null.
Initialize an Array of Boolean Values using for loop #const arr2 = new Array(3); for (let i = 0; i < arr2. length; i++) { arr2[i] = false; } // 👇️ [false, false, false] console. log(arr2); We used the Array() constructor to create an array of 3 empty elements, just like in the last example.
In Java, a primitive variable has a default value. For example, a primitive int variable's default value is 0, and a primitive boolean variable will hold false by default. Therefore, if we want to initialize a boolean array with all false, we can simply create the array without setting the values.
To store 600 billion bits, you need an absolute minimum address space of 75 gigabytes! Good luck with that!
Even worse, the Java spec doesn't specify that a boolean
array will use a single bit of memory for each element - it could (and in some cases does) use more.
In any case, I recognise that number from Project Euler #3. If it needs that much memory, you're doing it wrong...
Consider using a BitSet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With