OK, I've been following some Java tutorials which all work so this may be a Q about style or etiquette or good programming practice so your input is appreciated.
The tutorials regularly declare variables as having null values before they get used such as String name=""; I thought I'd streamline this as shown in the code example below by declaring the variable at the point of its use such as... String name=SomeCalculatedResult;
Do any of you have any views on my comments in the Code's footnotes.
Roger Wolfenden wrote:
The tutorials regularly declare variables as having null values before they get used such as String name="";
"" isn't a null value. It's the empty String. Very different from null.
There are a couple different scenarios:
1. Initializing member variables to their default values:
This is pointless, and can lead to unexpected behavior depending on how you write your constructors.
2. Initializing member variables to other than their default values:
This makes sense when you are able to declare and initialize in one shot, without needing extra work first, and if you're going to use the same value regardless fo which constructor gets invoked.
3. Initializing local variables at declaration.
Don't do this unless that value actually means something worthwhile in your method, and you might actually use that value, such as initializing a counter to 0, or initializing a success boolean to false, etc. Don't do it blindly just to make the compiler happy. That's a good way to turn an easy-to-spot compile-time error into a hard-to-debug runtime problem. For instance, in this case, I forgot the case where x = 0 (which could have been handled with a plain old "else"). If I hadn't initialized result, the compiler would have complained that it might not be set when I try to return it. The compiler error would have made me look more closely at my code, and I'd have found my logic error. Now, though, I'll sometimes have my method returning 1 and I won't know why.
For the starters, null is different than empty string. That given,
is different thanNow, coming to tutorial, IMHO, there are two things which can be done:
orPersonally I prefer the second approach. Also, please bear in mind that sayingmeans you are creating a string of zero length, and when you assign new value to str, you are making that original zero length string eligible for garbage collection, which, in my opinion, is not a good practice.
Perhaps my tutorials are using these String variable=""; declarations to make it easier for newbies like me to understand.
For this particular one, for a member variable I would only use it if I wanted the value to never be null, and for a local variable, I'd probably only use it in the following situations:
1. I'm building up a String from pieces, and I need to start with an empty String and add to it as I go.
2. My method returns a String, and which String to return is determined by some complex logic, and the default return value if none of the logic sets it to something else is the empty String.
However, in #1 I would usually use a StringBuilder (though if it's guaranteed to be no more than a few smallish pieces, I might be lazy and take this route), and #2 I can't imagine arising in real code.
Joined: Oct 08, 2012
Jeff, fully understand the logic of needing to set an empty string for your #1 example. I've not yet come across StringBuilder but if it does what its name suggest, I can also appreciate the logic of that too.
One or two of you mention Garbage collection which my tutorials have briefly touched on but not extensively so I'm guessing that its all about making sure that stuff isn't allowed to 'stay alive' past its intended use which makes good sense. Anyway, you've given me something that I ought to explore further.