Search code examples
javafactorial

How to apply long's L when using variable names in Java?


I've tried to build a factorial generator out of words, but every time I enter some big numbers, it makes my numbers negative.

I figured I should use a long with an identifier (L). I've applied it to the initialization of the long, but that didn't fix it. I guess the problem is somewhere around here:

for (int j = 1 ; j <= wordLength; j++) {
    temp *=  j;
}    

how do I assign the L to temp so it reserves enough space for the number to fit in?


Solution

  • The l isn't applied to variable-names. You can of course do that to include the type of a variable in it's name, but that's rather a question of style than anything else, like this:

    long avariableL = someLong;
    
    for(int i = 0; i < 10; i++)
        avariableL *= i;
    

    l is only used for constants in the code, like

    long someLong = 123456789L;
    

    Variables don't magically change their type during execution of code. You'll have to declare the variable as long.