What is the output of this program?
class Variable9
{
public static void main(String[] alt)
{
int i=10;
int j;
if(i==10)
j=20;
else
j=30;
System.out.println(j);
}
}
/*
Output:- 20
Explanation:- In this case compiler will be confirmed whether if statement true or false
variable "j" will be initialized. Hence compiler will not rise any error.
It is never recommended to perform initialization for local variables inside
logical blocks because there is no guarantee for the execution of these blocks
always at runtime. It is highly recommended to perform initialization for local
variables at the time of declaration.
*/
{
public static void main(String[] alt)
{
int i=10;
int j;
if(i==10)
j=20;
else
j=30;
System.out.println(j);
}
}
/*
Output:- 20
Explanation:- In this case compiler will be confirmed whether if statement true or false
variable "j" will be initialized. Hence compiler will not rise any error.
It is never recommended to perform initialization for local variables inside
logical blocks because there is no guarantee for the execution of these blocks
always at runtime. It is highly recommended to perform initialization for local
variables at the time of declaration.
*/
0 comments: