Re: Way to optimize this?
- From: Lew <noone@xxxxxxxxxxxxx>
- Date: Sat, 07 Nov 2009 10:09:48 -0500
Since you never use this 'null' value it's superfuous and you
shouldn't assign it..
Leif Roar Moldskred wrote:
Well, you're not going to get away from the _assignment_ in Java,
it's just a question of whether it's done implicitly (Object value;)
or explicitly (Object value = null;)
Wrong. The implicit assignment doesn't exist for local variables. The OP's example
Integer fileCount = null;
for a local variable would not have assigned 'null' if the declaration had been
As a matter of style, I prefer to write them out explicitly to separate
"this variable is supposed to start as null" from "the programmer forgot
to initialize this variable."
As a matter of avoidance of compilation error, a local variable must be definitely assigned prior to use. If "the programmer forgot to initialize this variable" the program would not compile when the variable was read.
Furthermore, the superfluous assignment of 'null' hides a failure to assign the desired value, which would otherwise be a compiler error rather than a runtime exception. As a matter of good engineering, which trumps "style", it is better to catch mistakes at compilation than run time.
For primitive types where an initial assignment at declaration can be
superfluous, I still prefer to provide an initial value (if possible,
something out of the range of what the variable will be assigned.) This
is less a matter of style and more a matter of making bugs behave
consistently. (With modern compilers and code analysers, this isn't
really needed in Java -- the use of a variable before it's assigned to
should be detected during coding -- but it's an old habit of mine and I
still sometimes program in other languages so I don't see any reason to
In Java if you read a local variable before it's assigned the compiler barfs. In some cases, it's even harmful to assign a superfluous initial value.
Integer fileCount = null;
Integer fileCount = expression();
I object to the superfluous assignment of zeroesque initial values to member variables also, since any Java programmer already knows it happens on instantiation. The overhead of the repeated 'null' assignment (or zero or 'false') is small, but why incur it at all if it isn't needed?
- Prev by Date: 〓〓〓 ED Hardy T-shirt cheap *** at www.fjrjtrade.com ●<***>○
- Next by Date: ☆๑♣๑☆ free shipping *** cheap fashionable brands T-shirts in www.ecyaya.com
- Previous by thread: Re: Way to optimize this?
- Next by thread: Re: Way to optimize this?