Gregory, in your first example, did you mean for the comment to read, "This example String variable ex does need to be initialize?" Notice that your comment says the variable does not need to be initialized, which is incorrect.
In general, a variable must always be initialized before you use it for something other than assigning a value to.
In your first example, the variable is not initialized, and then you tried to use it to do something in the if-statement. Do you understand how that's a problem?
On the issue of when to initialize a variable, I'd recommend not declaring a variable until you're ready to initialize and use it. So, initialize variables when they're declared.
Joined: Jun 01, 2004
oh boy... I was so confused that I did not read my comment thoroughly. Yes you are right.
Thank you for replying to me. Why do you "recommend not declaring a variable until you're ready to initialize and use it"? I had the same thought but my manager was questioning why I did that in a loop. For example:
Is it because I am creating several string instances?