> I hate to do the following:
>
> double num1 = 0.0;
> double numABC = 0.0;
> double iX = 0.0;
Why?
> I think such message should be a warning, instead of error. Of course,
> the variable has at least a default value.
local variables are not automatically initialized in Java, and if not
initialized explicitely will contain garbage. The fact that Java
compiler catches that turns your potentially run-time errors into
compile times ones, and the common wisdom is that former is much worse
than the latter.
Shawn - 24 Oct 2006 20:52 GMT
> local variables are not automatically initialized in Java,
I am not aware of this. Are you sure?
By the way, for my education purpose, how about a local array, say
int[] aArray = new int[10];
No need to initialize every item to 0, right?
Yan - 24 Oct 2006 21:36 GMT
> > local variables are not automatically initialized in Java,
>
[quoted text clipped - 5 lines]
>
> No need to initialize every item to 0, right?
Right, no need to explicitely set every array item to 0. But that
probably has more to do with array initialization rather than with
local variables.
Tor Iver Wilhelmsen - 24 Oct 2006 21:49 GMT
> int[] aArray = new int[10];
>
> No need to initialize every item to 0, right?
Correct, because array elements are initialized by the "constructor".
The local variable here is aArray, which you initialize explicitly.
Mike Schilling - 24 Oct 2006 22:07 GMT
>> local variables are not automatically initialized in Java,
>
> I am not aware of this. Are you sure?
Yes, it's well-known.
> By the way, for my education purpose, how about a local array, say
>
> int[] aArray = new int[10];
>
> No need to initialize every item to 0, right?
Yes, because the members of the array aren't local variables, they're part
of the array object .
Thomas Hawtin - 25 Oct 2006 16:49 GMT
> By the way, for my education purpose, how about a local array, say
>
> int[] aArray = new int[10];
>
> No need to initialize every item to 0, right?
It would be useful to statically check whether the elements of the array
have been initialised before use. However, there isn't a sensible way of
the language declaring the rules. Not least because a reasonable
programmer would use library methods, such as Arrays.fill, to manipulate
the array contents. Much the same argument goes for fields. As it is,
definite assignment/unassignment takes up one very dull chapter of the
JLS (actually it isn't so bad once you get into it).
Tom Hawtin
> I am writing a program and I run into a lot of annoying errors: local
> variable may not have been initialized. For example:
(snip)
> How to avoid such errors? Maybe initialized to 0:
Yes, or initialized to *something*. As in many languages, local
variables contain garbage unless you initialize them. Java is
actually being very helpful by telling you so at compile time rather
than taking the "programmer-knows-best" approach a C compiler would.
> double number = 0.0;
> will get rid of the error. But my program has about 30 local variables.
Well, you get to initialize all of them then. The amount of time that
takes is guaranteed to be much less than you would spend debugging the
random errors that would otherwise result.
> I am using:
> double num1, numABC, iX, ..., w;
double num1=0.0D, numABC=0.0D...
if you insist on declaring everything on one line.
> I think such message should be a warning, instead of error. Of course,
> the variable has at least a default value.
Not in this language. *Class* variables get default values, but local
variables do not.

Signature
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Shawn - 24 Oct 2006 20:53 GMT
>> I am writing a program and I run into a lot of annoying errors: local
>> variable may not have been initialized. For example:
[quoted text clipped - 29 lines]
> Not in this language. *Class* variables get default values, but local
> variables do not.
I see. Thank you all.