Factorial in Java

后端 未结 7 1380
庸人自扰
庸人自扰 2020-12-11 08:08

I\'ve been using this factorial program for Java:

public static long factorial(int a) {

    if(a<1) {
        return 1;
    }
    long result=1;
    long         


        
7条回答
  •  天涯浪人
    2020-12-11 08:46

    Check out http://en.wikipedia.org/wiki/Integer_overflow, I know the title refers to an integer but the principle stands for int, long , double etc.

    In short, a primitive datatype has a max value, when you go over that, it wraps aroun and starts again. if you really want to get geeky about it, learn about binary addition to fully understand it.

提交回复
热议问题