这会导致堆栈溢出错误吗?
增加对象的实例变量是否会导致堆栈溢出错误?
例如:
此方法(java)将导致堆栈溢出错误:
class StackOverflow {
public static void StackOverflow (int x)
{
System.out.println (x) ;
StackOverflow(x+1) ;
}
public static void main (String[]arg) { StackOverflow (0) ;
}
但这会吗?:(.....是我为了缩短代码而放入的间隙。它已经足够长了。)
import java.util.*;
class Dice
{
String name ;
int x ;
int[] sum ;
.. ..
public Dice (String name)
{
this.name = name ;
this.x = 0 ;
this.sum = new int[7] ;
}
....
public static void main (String[] arg)
{
Dice a1 = new Dice ("a1") ;
for (int i = 0; i<6000000; i++)
{
a1.roll () ;
printDice(a1) ;
}
}
....
public void roll ()
{
this.x = randNum(1, this.sum.length) ;
this.sum[x] ++ ;
}
public static int randNum (int a, int b)
{
Random random = new Random() ;
int c = (b-a) ;
int randomNumber = ((random.nextInt(c)) + a) ;
return randomNumber ;
}
public static void printDice (Dice Dice)
{
System.out.println (Dice.name) ;
System.out.println ("value: "+Dice.x) ;
printValues (Dice) ;
}
public static void printValues (Dice Dice)
{
for (int i = 0; i<Dice.sum.length; i++)
System.out.println ("#of "+i+"'s: "+Dice.sum[i]) ;
}
}
以上目前不会导致堆栈溢出错误,但如果我在 main 中更改此行,我也能得到它吗: for (int i = 0; i<6000000; i++ )
这样一来,不是有 600 万足够高的东西了吗?
Will incrementing the instance variables of an object ever lead to a stack overflow error?
For example:
This method (java) will cause a stack overflow error:
class StackOverflow {
public static void StackOverflow (int x)
{
System.out.println (x) ;
StackOverflow(x+1) ;
}
public static void main (String[]arg) { StackOverflow (0) ;
}
but will this?: (..... is a gap that i've put in to shorten the code. its long enough as it is.)
import java.util.*;
class Dice
{
String name ;
int x ;
int[] sum ;
....
public Dice (String name)
{
this.name = name ;
this.x = 0 ;
this.sum = new int[7] ;
}
....
public static void main (String[] arg)
{
Dice a1 = new Dice ("a1") ;
for (int i = 0; i<6000000; i++)
{
a1.roll () ;
printDice(a1) ;
}
}
....
public void roll ()
{
this.x = randNum(1, this.sum.length) ;
this.sum[x] ++ ;
}
public static int randNum (int a, int b)
{
Random random = new Random() ;
int c = (b-a) ;
int randomNumber = ((random.nextInt(c)) + a) ;
return randomNumber ;
}
public static void printDice (Dice Dice)
{
System.out.println (Dice.name) ;
System.out.println ("value: "+Dice.x) ;
printValues (Dice) ;
}
public static void printValues (Dice Dice)
{
for (int i = 0; i<Dice.sum.length; i++)
System.out.println ("#of "+i+"'s: "+Dice.sum[i]) ;
}
}
The above doesn't currently cause a stack overflow error but could i get it too if i changed this line in main: for (int i = 0; i<6000000; i++)
so that instead of 6 million something sufficiently high were there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(6)
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
堆栈溢出?不,但它可能会导致整数溢出,这是一个非常不同的事情。
堆栈溢出意味着方法调用堆栈上的空间已耗尽(可能是由于失控的递归调用)。如果增量超出其最大值,则整数溢出将导致 int 循环到其最低值。
Stack overflow? No, but it could lead to an integer overflow which is a very different thing.
A stack overflow means that space on the method invocation stack is exhausted (possibly because of a runaway recursive call). An integer overflow will cause the int to circle around to its lowest value if incremented beyond its maximum value.