stackoverflow 和 arrayindexoutofbounds 有什么区别?
看起来实际上这些应该几乎相同。您正在引用或粘贴数据不应该去的地方。
It seems like in reality these should be almost identical. You are referencing or sticking data where it shouldn't go.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
StackOverflowError
在无休止的递归调用期间发生。它与数组无关。发生ArrayIndexOutOfBoundsException
当您尝试使用超出数组大小的索引时。StackOverflowError
occurs during endless recursive calls. It has nothing to do with arrays.ArrayIndexOutOfBoundsException
occurs when when you try to use an index beyond the size of the array.StackOverFlow错误:
当 JVM (Java 虚拟内存) 耗尽分配的堆空间时发生。
ArrayIndexOutOfBoundsException:
当应用程序尝试访问不在声明范围内的数组索引时发生。
StackOverFlowError:
Occurs when the JVM (Java Virtual Memory) is running out of allocated heap space.
ArrayIndexOutOfBoundsException:
Occurs when an application tries to access the array index which does not fall in declared range.
这两者实际上是非常不同的事情。正如其他人已经提到的,ArrayIndexOutOfBoundsException 是当您尝试使用不正确的索引访问数组时发生的异常。这可能是由编程逻辑中一个非常简单的错误引起的。
StackOverflowError 是更底层的东西。它与数组无关,而且与其他人所说的相反,它也与堆无关。尝试用 List 来实现这个技巧,你会得到一个 OutOfMemoryError 。现在它是一个与 StackOverflowError 比 ArrayIndexOutOfBoundsException 更相似的错误。 OutOfMemoryError 和 StackOverflowError 都表明内存不足,只是在不同的段中。堆栈内存耗尽几乎总是由于无限递归而发生(除非你有一个非常长的方法链,相互调用其中包含大量局部变量),并且当你分配太多并保留所有这些变量时,就会发生堆内存耗尽引用,因此即使垃圾收集器也无法为您释放一些内存。
因此,当 ArrayIndexOutOfBoundsException 确实是“将数据粘贴到不应该去的地方”时,StackOverflowError 和 OutOfMemoryError 更好地描述为“内存不足,无法将数据粘贴到其中”。
These two are actually very different things. As the other guys have already mentioned, ArrayIndexOutOfBoundsException is an Exception that occurs when you are trying to access an array using an incorrect index. This could be caused by a very simple bug in programming logic.
StackOverflowError is something more low-level. It has nothing to do with arrays and, contrary to what the other guys said, has nothing to do with the heap either. Try doing that trick with a List and you'll get an OutOfMemoryError instead. Now it's an Error that is much more similar to StackOverflowError than ArrayIndexOutOfBoundsException. Both OutOfMemoryError and StackOverflowError indicate that you're running out of memory, only in different segments. Running out of stack memory almost always occurs due to infinite recursion (unless you have a ridiculously long chain of methods calling each other with lots of local variables in them), and running out of heap memory happens when you allocate too much and keep all those references so even the garbage collector can't free some memory for you.
So when ArrayIndexOutOfBoundsException is indeed "sticking data where it shouldn't go", StackOverflowError and OutOfMemoryError are better described as "running out of memory to stick your data into".
arrayindexoutofbounds :尝试超出数组范围读取时引发的异常。
stackoverflow :发生堆栈溢出时发出的错误。
arrayindexoutofbounds : The exception that is thrown when an attempt is made to read beyond the bounds of an array.
stackoverflow : An error that is issued when a stack overflow occurs.
Arrayindexoutofbounds:您限制变量,例如定义一个大小为 50 的数组,如果您想使用该数组的 51. 元素,则会出现此异常。
Stackoverflow:例如,考虑一下您没有限制变量;
您正在向此列表添加元素。
如果添加太多元素,堆内存无法分配更多空间来保存变量,则会引发该异常。
首先,你的限制是 50,在 51 处你会得到一个异常,第二次,如果你的堆足够空闲,你不会在 51.element 处得到异常,但当你的堆不够用时,你会得到一个异常。
Arrayindexoutofbounds: You limit your variable for example defining an array with size of 50 and if you want to use the 51. element of that array you get this exception.
Stackoverflow: Think about that you didn't limit your variable for example;
And you are adding elements to this list.
If you add too much element that your heap memory can't allocate any more space to keep your variable it throws that exception.
At first your limit is 50, at 51 you get an exception, at second you don't get an exception at 51. element if your heap is free enough but you can get an exception when your heap not enough for you.