Note that there are some explanatory texts on larger screens.

plurals
  1. POWill this ever result in a stack overflow error?
    text
    copied!<p>Will incrementing the instance variables of an object ever lead to a stack overflow error? </p> <p>For example: </p> <p>This method (java) will cause a stack overflow error: </p> <pre><code>class StackOverflow { public static void StackOverflow (int x) { System.out.println (x) ; StackOverflow(x+1) ; } public static void main (String[]arg) { StackOverflow (0) ; } </code></pre> <p>but will this?: (..... is a gap that i've put in to shorten the code. its long enough as it is.) </p> <pre><code>import java.util.*; class Dice { String name ; int x ; int[] sum ; </code></pre> <p>....</p> <pre><code>public Dice (String name) { this.name = name ; this.x = 0 ; this.sum = new int[7] ; } </code></pre> <p>....</p> <pre><code>public static void main (String[] arg) { Dice a1 = new Dice ("a1") ; for (int i = 0; i&lt;6000000; i++) { a1.roll () ; printDice(a1) ; } } </code></pre> <p>....</p> <pre><code> 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&lt;Dice.sum.length; i++) System.out.println ("#of "+i+"'s: "+Dice.sum[i]) ; } } </code></pre> <p>The above doesn't currently cause a stack overflow error but could i get it too if i changed this line in main: <code>for (int i = 0; i&lt;6000000; i++)</code> so that instead of 6 million something sufficiently high were there? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload