0

In this program why do I have to initialize a and b, since their scope is not limited, but I can't use them on the line d=a+b?

import java.util.Scanner;
class DivAndSum {
    public static void main(String[] args) {
        int a = 0, b = 0;
        Scanner kb = new Scanner(System.in);
        try {
            a = kb.nextInt();
            b = kb.nextInt();
            int c = a / b;
            System.out.println("Div=" + c);
        } catch (ArithematicException e) {
            System.out.println("Please Enter a non zero denominator");
        } catch (InputMismatchException e) {
            System.out.println("Please Enter integers only");
            System.exit(0);
        }
        int d = a + b;
        System.out.println("Sum=" + d);
    }
}

and the program below compiles fine:

import java.util.Scanner;
class DivAndSum {
    public static void main(String[] args) {
        int a,b,d;
        Scanner kb = new Scanner(System.in);
            a = kb.nextInt();
            b = kb.nextInt();
            d = a + b;
            System.out.println("Sum=" + d);}}
Shreyash Sharma
  • 310
  • 2
  • 11
  • 1
    Why do you think you *shouldn't* have to initialize them? What would you expect to happen if an exception were thrown before they were assigned? (For example, imagine if `nextInt()` threw an `ArithmeticException`? – Jon Skeet Mar 23 '16 at 06:57
  • Please refer this existing question: http://stackoverflow.com/questions/415687/why-are-local-variables-not-initialized-in-java – sAm Mar 23 '16 at 07:00

3 Answers3

2

If you are talking about int a=0,b=0; then a and b both are local variables and local variables have to initialized

Only class level and instance level variables no need not be initialized

If you do not want to initialize then see the code below

import java.util.Scanner;
class DivAndSum {
int a,b; // here a and b are instance variables so no need to be initialized. Both will have value 0 which is default
    public static void main(String[] args) {
       // int a = 0, b = 0; a and b are loca variables so both should be initialized
        Scanner kb = new Scanner(System.in);
        try {
            a = kb.nextInt();
            b = kb.nextInt();
            int c = a / b;
            System.out.println("Div=" + c);
        } catch (ArithematicException e) {
            System.out.println("Please Enter a non zero denominator");
        } catch (InputMismatchException e) {
            System.out.println("Please Enter integers only");
            System.exit(0);
        }
        int d = a + b;
        System.out.println("Sum=" + d);
    }
}
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • See the second program: In that both a and b a re local and that one gives no compilation error. – Shreyash Sharma Mar 23 '16 at 14:21
  • @ShreyashSharma the second program will not compile because defining d two times. – SpringLearner Mar 24 '16 at 05:20
  • changed it, but still, why does the second one runs and not the first one.. – Shreyash Sharma Mar 27 '16 at 14:08
  • @ShreyashSharma what do you mean by second one runs and not the first one? – SpringLearner Mar 29 '16 at 09:14
  • First. Thank you for taking the time to reply, I just want to know that in the first program the line "d=a+b", is in the same scope as "a" and "b", same as in the second code, but in the first one I have to initialize them, knowing that they will surely be initialized by Scanner, if not the program will either terminate due an exception, or that exception will be handled and then terminated by :"System.exit(0)". – Shreyash Sharma Apr 01 '16 at 12:04
1

If you don't initialize them (i.e. replace int a=0,b=0; with int a,b;), the try block in which you initialize them with user input may fail to initialize them (for example, if the user enters a String instead of an int, kb.nextInt() will throw an exception).

In such a scenario, they won't have a value at the line int d=a+b;. Therefore they must be initialized.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

If assigning the variable a goes wrong in your try-catch, no matter why, then is be never initialized, after that you are going to do math operations on values that are not initialized, that is not good at all

therefore is the complining in your IDE..

try instead declaring and initializing a and b

int a, b;
a=0;
b=0;
try {
...
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97