jeudi 27 octobre 2016

A conflicting if and else statements in Java. Why adding one else statement is changing whole scenario?

In Java if we provide following statement, it will produce compilation error:

class A {

public static void main(String[] args){
    int i = 10;
    int j;
    if(i == 10) {
       j = 20;
    }
    System.out.println(j);
   }

But in the following statement, no compilation error:

class A{ 

public static void main(String[] args){
    int i = 10;
    int j;
    if(i == 10) {
       j = 20;
     }
    else {
       j = 30;
     }
    System.out.println(j);
    }
  }

What is the reason?

Aucun commentaire:

Enregistrer un commentaire