Originally posted by Arnb Sen:
Hi Henry,
That's right. Since "abc" is a local varibale, it must be initialized. hence the first code shows a compilation error while the 2nd code does not as it is assigned with a null value.
My question is since in the first code, "abc" is not initialized, doesn't it mean that "abc" points to null ? If that is true then the 2nd code should not compile as well as "abc" still points to null. Right ?
No, abc does not "point to null" in the first version because it is uninitialized. When you declare a local variable, the compiler will allocate some space in memory for it. However, this location in memory already has some data in it from the last time it was used. If Java allowed the code in your first example, then it would use this unknown value as the reference. This uknown value could be pointing to a supposed object just about anywhere in memory. Can you see why this is bad? So, abc will point to null, only if you explictly say so as in your second example.
Layne