Results 1 to 4 of 4
  1. #1
    SwordMaster is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Factorial - java.lang.stackoverflow? Where's the mistake?

    Hey Guys, I get the following error, when I'm trying to use the following method:

    Java Code:
        public long fak(int n)
        {
            if((n==1)||(n==0))
            { return 1;
            }
            else {
                return (fak(n));
            }
        }
    Error:
    java.lang.stackoverflowerror:
    null


    When I use the code like this, it works... but I don't understand why the Code on top should lead to a Loop or something similar...

    Java Code:
        public long fak(int n)
        {
            if((n==1)||(n==0))
            { return 1;
            }
            else {
                return (n*fak(n-1));
            }
        }
    Thank you for your help!

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Factorial - java.lang.stackoverflow? Where's the mistake?

    As you can see in your code the recursive call to fak(n) will never stop if n != 0 or n != 1. When you do something like fak(10) it will repeating the call to fak(10) until you run out of memory. Because the value of n is never change.

  3. #3
    SwordMaster is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: Factorial - java.lang.stackoverflow? Where's the mistake?

    Quote Originally Posted by wsaryada View Post
    As you can see in your code the recursive call to fak(n) will never stop if n != 0 or n != 1. When you do something like fak(10) it will repeating the call to fak(10) until you run out of memory. Because the value of n is never change.
    So the "return" in my Code doesnt say "give me the Value of fak(n)" but says "jump back to fak(n)"?

    And why does the upper Code returns the Value "24" when I use n=4?
    4 * fak(4-1)
    = 4 * 3
    = 12
    ?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Factorial - java.lang.stackoverflow? Where's the mistake?

    Quote Originally Posted by SwordMaster View Post
    So the "return" in my Code doesnt say "give me the Value of fak(n)" but says "jump back to fak(n)"?
    It does mean "give me the Value of fak(n)", but before it can give you that value it has to actually run fak(n), which will result in it getting to the same line, which results in another call to fak(n) to find the answer...and so on, down the rabbit hole.

    Quote Originally Posted by SwordMaster View Post
    And why does the upper Code returns the Value "24" when I use n=4?
    4 * fak(4-1)
    = 4 * 3
    = 12
    ?
    And fak(4-1) does what?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. factorial java thread problem
    By alwan2009 in forum New To Java
    Replies: 14
    Last Post: 03-18-2012, 04:23 PM
  2. Factorial in java
    By spidey32 in forum New To Java
    Replies: 18
    Last Post: 04-09-2011, 01:04 PM
  3. factorial sum in java
    By java157 in forum New To Java
    Replies: 9
    Last Post: 03-17-2011, 10:07 AM
  4. StackOverFlow
    By s0meb0dy in forum New To Java
    Replies: 1
    Last Post: 02-17-2011, 06:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •