1
class Faltu
{
	public static void main(String aa[])
	{
		int a=4,b;
		if (a==4)
			b=5;
		System.out.println ("b="+b);
	}
}

Here it says : "variable b might not have been initialized", but in the following :

class Faltu
{
	public static void main(String aa[])
	{
		int a=4,b;
		if (true)
			b=5;
		System.out.println ("b="+b);
	}
}

Here it compiles perfectly. Why ?

Edited by daudiam: n/a

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by daudiam
0

Because in the second case the compiler knows that b will always have a value, but it does not know that in the first version (the compiler does not compute expression values).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.