Can anyone help me to understand why java compiler not giving any error for code 1. As in code 1, there is no chance of executing line 2 at run-time. is compiler not validating if condition ? And why it is giving error on code 2.
Code 1 :
public class HelloWorld{
public static void main(String []args){
m1();
}
public static String m1() {
if(true){
System.out.println("if");
return ""; //line 1
} else {
System.out.println("else"); // line 2
return "";
}
}
}
Code 2 :
public class HelloWorld{
public static void main(String []args){
m1();
}
public static String m1() {
return "";
System.out.println("last statement"); //compile error : unreachable statement
}
}