From: Boris Punk on
long size = Integer.MAX_VALUE+1;
byte [] b = new byte[size];

-possible loss of precision

How can we make an array of long size?


From: Eric Sosman on
On 7/8/2010 4:30 PM, Boris Punk wrote:
> long size = Integer.MAX_VALUE+1;
> byte [] b = new byte[size];
>
> -possible loss of precision
>
> How can we make an array of long size?

Array elements are indexed from [0] through [Integer.MAX_VALUE-1],
and that's that. You can make an array whose elements are larger
than a single byte (`new long[N]', for example) and thus get an array
of more than Integer.MAX_VALUE-1 bytes, but you cannot have more than
that many elements.

Incidentally, you might want to print the value of `size' that
you calculate in your example. It might surprise you ...

(By the way: Yes, I really did intend the `-1's above. An int
index value could go one higher, but how could you create the array?
In `new byte[N]', N cannot exceed Integer.MAX_VALUE, which means that
the index cannot exceed N-1, or Integer.MAX_VALUE-1. You could try
to write `new byte[] { 0,0,0,... }' and so on for two giga-values,
but you'd exceed .class file limits long before you got there.)

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Eric Sosman on
On 7/8/2010 4:52 PM, Eric Sosman wrote:
> On 7/8/2010 4:30 PM, Boris Punk wrote:
>> long size = Integer.MAX_VALUE+1;
>> byte [] b = new byte[size];
>>
>> -possible loss of precision
>>
>> How can we make an array of long size?
>
> Array elements are indexed from [0] through [Integer.MAX_VALUE-1],
> and that's that. You can make an array whose elements are larger
> than a single byte (`new long[N]', for example) and thus get an array
> of more than Integer.MAX_VALUE-1 bytes, but you cannot have more than
> that many elements.
> [...]
> (By the way: Yes, I really did intend the `-1's above.[...]

Oh, drat. I really did mean the first one, but not the second.
It's been really hot here for the last few days, and my brain is
starting to resemble a poached egg.

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Lew on
Boris Punk wrote:
> long size = Integer.MAX_VALUE+1;
> byte [] b = new byte[size];
>
> -possible loss of precision
>
> How can we make an array of long size?
>

You can't.

Why do you want to?

From the JLS, which I strongly urge you to study:
"The type of each dimension expression within a DimExpr must be a type
that is convertible (§5.1.8) to an integral type, or a compile-time
error occurs. Each expression undergoes unary numeric promotion (§).
The promoted type must be int, or a compile-time error occurs; this
means, specifically, that the type of a dimension expression must not
be long."
15.10 Array Creation Expressions

Didn't you get a compiler error?

You can make some other data structure that would hold that much,
assuming you have the address space for it. For sure in a 32-bit
machine you'd have trouble even with 'new byte [Integer.MAX_VALUE]'.

But really, why do you want to?

--
Lew
From: Boris Punk on
Integer.MAX_VALUE = 2147483647

I might need more items than that. I probably won't, but it's nice to have
extensibility.