tags:

views:

103

answers:

3

Can I do this in Java?

At Runtime:

int length = some arithmetic that loads length

then I use length to do this:

byte [] b = new byte[length];

Will this throw an Exception at Runtime? If so which one?

A: 

Yes it can be created at run time.

But you should try and see :)

Why not using ArrayList ?
Can't, I am a limited Java platform that doesn't have the ArrayList class.
Kevin Boyd
Why do you say that you should try and see? Isn't there any concrete documentation on this subject?
Kevin Boyd
Concrete documentation is reading some books and some tutorials on the net. Your counter question could be? is this true for both Heap and Stack ?
+1  A: 

No that won't throw an exception at runtime, unless of course length is negative.

cletus
Or a big honkin' number :-)
paxdiablo
Array sizes and indices are signed integers so anything up to and including Integer.MAX_VALUE is fine. If there's not enough memory, technically you won't get an exception, you get an error (OutOfMemoryError). :)
cletus
A: 

Yes, read this!

Secko