views:

151

answers:

6

I created a java program to count up toward infinity:

class up {

    public static void up (int n) {
        System.out.println (n) ;    
        up (n+1) ;
}

    public static void main (String[] arg) {
        up (1) ;

}
}

i didn't actually expect it to get there but the thing that i noticed that was a bit curious was that it stopped at the same number each time: 518669

what is the significance of this number? (or of this number +1 i suppose).

(also as a bit of an aside question, I've been told that the way i format my code is bad [indentation and such] what am i doing that isn't desirable?)

A: 

To answer your aside, the close brace of a method is generally aligned with the start of the method. So in your cases here, in the same column as the 'p' of public.

Lord Torgamus
+8  A: 

There is little significance to that number itself other than apparently 518669 multiplied by whatever the stack size is for that method equals the total available stack space on your system.

Eric Petroelje
A: 

There is nothing special about the number 518669 except that this is how many stack frames you can fit in your available memory before you run out.

There is no reason to use recursion here -- you should simply use a while loop instead.

Ether
+4  A: 

That specific value isn't significant, it's a result of your local setup. That you get the same value repeatedly is significant and predictable.

Your program crashes consistently at that point because each time you're running your program, the Java Virtual Machine starts with the same parameters and then performs the same actions until the stack space is gone.

You can change both, and change the result.

You can change the max stack size available to your JVM by passing a -Xss flag, for example:

java -Xss4096k MyClass

On my machine, with my default startup parameters, I run out of stack space after 10,518 recursive calls. I think the default is 1024k for my setup.

If I set the max stack size to 4096k as above, I can get 50,777 recursive calls before a stack overflow.

You can also add more operations, or different operations to your method, consume more space per call and change the number of possible invocations.

If I add the statement MyClass myClass = new MyClass(); to the code (a local instance of MyClass per call) after your println statement, the number of calls I can make before an overflow goes down from 10,518 to 9,709 as on each call a reference to an instance of MyClass has be stored.

Brabster
I don't think "coincidence" is the right word. It's actually unsurprising that it crashes with the same number each time. It's not a coincidence, it's simply a reflection of the stack size.
Bryan Oakley
I meant it was coincidental to his JVM configuration, rather than a coincidence over time. You're right it's not clear, I'll rephrase.
Brabster
@Brabster - A bit pedantic, but when you create a new MyClass instance, the instance isn't actually stored on the stack (it would be on the heap) but a pointer to that instance is stored on the stack.
Eric Petroelje
good point updated.
Brabster
It should also be pointed out that more or different operations doesn't matter, what matters is the size of the stack frame, which is not directly related to the complexity/size of the method.
erikkallen
Indeed, the declaration of local variables (as in the reference myClass in my example) and their values are stored in the stack frame increasing its size, not operations.
Brabster
+2  A: 

You have a finite amount of memory allocated to the call stack. It is the same amount of memory each time you run the application. So it stops counting in the same place each time, because you are out of stack memory.

As to your aside, I would format in this mannor

class up 
{
    public static void up(int n) 
    {
        System.out.println(n);    
        up(n + 1);
    }

    public static void main(String[] arg) 
    {
        up(1);

    }
}

This way it is much more clear when your blocks begin and end.

Matthew Vines
@Matthew Vines:I disagree with the comments about braces. "more clear" is a matter of personal taste. Your style makes the code artificially "tall", meaning you can see fewer lines of code at a time.
Bryan Oakley
agreed. IMHO, the main problem with formatting in the question was identation of the closing brackets (which I think there's usually no disagreement). The opening brackets are a matter of taste
Samuel Carrijo
@Bryan Oakley. It's certainly "more clear". I would agree that whether or not it is "most clear" is certainly a matter of personal taste. As long as you and your team are consistent it really doesn't matter too much.
Matthew Vines
@Matthew Vines: agreed -- consistency among team members is the key.
Bryan Oakley
A: 

Space before semicolon is also unusual, space between parenthesis and method name may be controversal, I personally don't like it, because it makes spotting function calls harder (at least for me). If you are concerned, try looking for some conventions, which are standardized. E.g. Sun's standards, Checkstyle, Eclipse formatter, hydrolabs formatter etc. For the number, maybe some upper limit, or println gets buffered and till something gets printed JVM gets busy... but that is just my guess.

Gabriel Ščerbák