You need to define the size as long
using the suffix L
:
out.setLength(50L * 1024L * 1024L * 1024L);
The problem is that by default, numeric literals are of type int
, and 50G is outside its range, so the result of overflow is multiplied. The actual value passed to setLength()
is -2147483648
.
In more detail, the type of multiplication result (as well as other numerical operations) is determined by its most common operand, so you actually do not need to add the suffix L
to each of them. It is enough to add it only to them (the first is a reasonable choice):
long wrong = 50 * 1024 * 1024 * 1024; // -2147483648 long right = 50L * 1024 * 1024 * 1024; // 53687091200
Natix source share