Here are some snippets:
int[] arr = { }; arr[-1] = arr[-2]; // Throws ArrayIndexOutOfBoundsException: -2 int[] arr = { }; arr[arr[-1]] = arr[arr[-2]]; // Throws ArrayIndexOutOfBoundsException: -1
int[] arr = null; arr[-1] = (arr = new int[0])[0]; // Throws ArrayIndexOutOfBoundsException: 0 int[] arr = null; arr[-1] = (arr = new int[1])[0]; // Throws NullPointerException
Update: HAHAHA Nevermind all that confusion stuff. This is as obvious as it can get:
- left
- get array reference (even if it's null)
- get index (even if it's out of bounds)
- right
- get right hand side value
- assign
- may
throw NullPointerException
- may
throw ArrayIndexOutOfBoundsException
throw
either NullPointerException
(if the reference is null) or ArrayIndexOutOfBoundsException
(if the index is out of bounds) sooner rather than later. This is not the case. The following snippet compiles and runs "fine".
((Object[]) null)[-1] = new Object() { { while (Boolean.TRUE); } };Analogously, this "fail-slow" mechanism is also the case with instance member access. The following compiles and runs "fine".
((Object) null).equals(new Object() { { while (Boolean.TRUE); } });
No comments:
Post a Comment