0

I have the following code where variable gcd is inside the gcd() function and this is showing the error:

The local variable gcd may not have been initialized.

The code is:

import java.util.Scanner;

public class GreatestCommonDivisorMethod {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Enter two numbers");
        Scanner input = new Scanner(System.in);
        int num1 = input.nextInt();
        int num2 = input.nextInt();
        System.out.println("the gcd of the " + num1 + " and " + num2 + " is " + gcd(num1,num2));
    }

    public static int gcd(int n1, int n2) {
        int gcd ;
        for (int n = 1; n <= n1 && n <= n2 ; n++) {
            if (n1 % n == 0 && n2 % n == 0)
                gcd = n;
        }
        return gcd;

    }
}

Why gcd should be initialized?

Andreas
  • 5,393
  • 9
  • 44
  • 53
iceiceice
  • 9
  • 2

2 Answers2

3

Essentially, because of the way Java works, gcd needs to be declared at some point.

Your method declares int gcd but never assigns it a value except inside your if-statement. There is the possible case where your if-statement never gets entered, because the boolean statement inside it never evaluates to true. In that possible case, there would be a problem, as return gcd; would execute even though no value has been assigned to it. Java does not want this to occur and is warning you.

You probably want to set a default value in case it doesn't find a gcd. This value would obviously be 1, so declare it as such: int gcd = 1;.

sameer-s
  • 415
  • 3
  • 12
-4
  • The first thing is every variable must be initialised before it is used.
  • Compiler does this to make sure that at the time of the execution or using the variable in the program there is atleast one value in the variable .
  • To avoid compile-time error. this is used.
  • In your case if the value n1 and n2 are 0 , then the control will not go into the for loop and the gcd will not be initialised . While returning the gcd value there will be compile-time error..
  • I would also like to point out this part from java docs ,

    Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

    you can get the complete docs in this link.

    Credit-SO Question

    This says that as your gcd is a local variable thats why it is not initialised with default value.

  • Therefore you have to initialise the variable.

  • This is what you should do-

    public static int gcd(int n1, int n2) {
    int gcd = 0;
    for (int n = 1; n <= n1 && n <= n2 ; n++) {
    if (n1 % n == 0 && n2 % n == 0)
         gcd = n;
    }
    return gcd;
    
    }
    

    int gcd = 0;

Hope this answer helps you.

Community
  • 1
  • 1
Sagar Nayak
  • 2,138
  • 2
  • 19
  • 52
  • 1
    Primitives cannot be null – Raphael Roth Apr 11 '16 at 04:38
  • I completely disagree with you. see my edited answer @RaphaelRoth. – Sagar Nayak Apr 11 '16 at 04:46
  • No seriously, **primitives cannot be `null`.** – Makoto Apr 11 '16 at 04:52
  • this primitive which is local is null . see the edited answer . you will find your answer . – Sagar Nayak Apr 11 '16 at 04:53
  • the docs specifically says the words which will make you understand what you are doing wrong. and dont say anything if you are not clear . it says ---- Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error. – Sagar Nayak Apr 11 '16 at 04:55
  • if @Makoto needs the proof for it , go to this link and study it. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html – Sagar Nayak Apr 11 '16 at 05:15
  • @RaphaelRoth did you saw the docs http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html – Sagar Nayak Apr 11 '16 at 05:17
  • 1
    @SagarNayak, it seems that you do not understand what is `null` – Dennis Zinkovski Apr 11 '16 at 05:22
  • Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error. this is what the java docs says , link - http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html have a look at it @DenisZinkovskiy – Sagar Nayak Apr 11 '16 at 05:23