Issue
I've seen this question for other languages (C and C++). But I still don't get why it is like this in Java as well. Where could it be useful that a reference is declared but not set to null?
Solution
Thing ref;
if (someCondition)
ref = oneThing;
else
ref = anotherThing;
There is no benefit in initializing 'ref' to null in the above code, at least not as long as neither assignment can throw an exception.
It's not "useful" that it is uninitialized, it's merely that there's no point in initializing it.
I wish it were not like that - I'd prefer initialization of local variables to work like member variables - but that is how it is.
I assume it's for efficiency reasons. If you don't have to initialize local variables, allocation is pretty much just an adjustment of the stack pointer.
Answered By - user16632363 Answer Checked By - Timothy Miller (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.