what kind of exception do you get when you concatenate 2 strings where the combined length would be more than Integer.MAX_VALUE ?
You’ll get OutOfMemoryError long before you even get a chance of doing that, unless you can allocate over 8 GB memory. A string of Integer.MAX_VALUE characters requires at least about 4 GB memory (16 bits * (2^31 - 1) + some object overhead, assuming that the char arrays are stored optimally in memory), and you would need at least double that to keep all the three strings in memory during the copy operation.
If it were possible to allocate that much memory, I think you would get NegativeArraySizeException from java.lang.String#concat(String) (which will try to create a char[] with negative length) or IndexOutOfBoundsException from java.lang.StringBuilder#append(String) (which calls java.lang.String#getChars which calls java.lang.System#arraycopy).
Interestingly java.lang.AbstractStringBuilder#expandCapacity might truncate the length to Integer.MAX_VALUE characters (although I’m not anymore 100% sure about that), but java.lang.AbstractStringBuilder#append will not call that method because newCount will be negative. I wonder if this is by design or by chance. I also think that it’s best to throw an exception instead of truncating the string and corrupting data.
EDIT: I think I got it right now. A StringIndexOutOfBoundsException will not be throw even though I at first thought that it would.
EDIT2: Would somebody know the memory limit of Sun’s 64-bit JVM? Here 2 GB and 4 GB are mentioned: http://www.theserverside.com/discussions/thread.tss?thread_id=45758
It bombs with java.lang.NegativeArraySizeException at that line where it tries to create a new char buffer.
edit: That will even happen if both have a length of Integer.MAX_VALUE.
edit2: Grammar b0rkage o_O
vm memory limits are os dependent, additionally the heap have to be contiguous,
see table 4.1:
http://docs.sun.com/source/819-0084/pt_tuningjava.html
Why on earth would you want to do that? :o
I’ve also wondered what would happen if I changed the value of the instance variable for length in an array.
Ex.) int[] arr = new int[5];
int.length = Integer.MAX_VALUE;
int[999] = 1000;
Will I start overwriting my operating system?
heh i really dont of course, i just wondered
eh its final you cant change it
There isn’t a length field at all in an array - it’s a nasty hack in the compiler
Oh, bummer. I thought it was a public instance variable.
Well, better a nasty compiler hack than a public non-final instance variable