这是否会导致堆栈溢出错误? [英] Will this ever result in a stack overflow error?

查看:189
本文介绍了这是否会导致堆栈溢出错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

增加对象的实例变量是否会导致堆栈溢出错误?

Will incrementing the instance variables of an object ever lead to a stack overflow error?

例如:

此方法(java)将导致堆栈溢出错误:

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]) ; 
    } 

}

上述目前不会导致堆栈溢出错误,但如果我在main中更改了这一行,我也可以得到它: for(int i = 0; i< 6000000; i ++)这样代替600万那里有足够高的东西?

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?

推荐答案

堆栈溢出?不,但它可能导致整数溢出,这是一个非常不同的事情。

Stack overflow? No, but it could lead to an integer overflow which is a very different thing.

堆栈溢出意味着方法调用堆栈上的空间已用尽(可能是因为失控递归电话)。如果增量超出其最大值,整数溢出将导致int绕圈到其最低值。

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.

这篇关于这是否会导致堆栈溢出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆