A
Anon
Hi,
How do I find out if a variable has been used or initialised yet?
Thanks,
Anon.
How do I find out if a variable has been used or initialised yet?
Thanks,
Anon.
Anon said:Hi,
How do I find out if a variable has been used or initialised yet?
Anon said:How do I find out if a variable has been used or initialised yet?
How do I find out if a variable has been used or initialised yet?
compile the code. Java won't let you write code with uninitialised
variables.
See http://mindprod.com/jgloss/initialisation.html
steve said:So you are saying a null variable is initialised ?
So you are saying a null variable is initialised ?
previously hanging around in that memory location. Null, in this case, is
definitely the result of initialization.
However, judging by your
responses I believe it may be wiser to use boolean variables to flag weather
or not the variables have already been set or not.
So you are saying a null variable is initialised ?
Tony Morris said:Care to back this up with a bug report ?
Sounds like baloney to me.
Eric said:If it's a local variable in a method, the compiler will
complain if it thinks the variable might be used before
being initialized. The compiler will sometimes issue bogus
complaints
Larry Barowski said:It's not a bug. The problem of determining if a variable is initialized is
not computable (for those not familiar with computational theory, this
basically means "impossible to solve in the general case using an
algorithm").
Here is a simple example:
int j;
int i = 5;
if(i == 5)
j = 0;
j++;
Here is an example you could not expect any compiler to catch
(assume you have a method to compute the nth digit of pi):
int j;
if(digitOfPI(98000009) == 3)
j = 0;
j++;
-Larry Barowski
Tony Morris said:No, it wouldn't contain anything - it would simpyly be uninitialized and
prevented from use.
Java != C
Chris said:I think the wording you've choosen is a bit misleading.
The complaints are "bogus" in the sense that a different analysis with
different rules might have recognised the case.
However they are not "bogus" in the sense of being wrong. There are definite
rules for deciding whether a variable has been intialised before use, those
rules are part of the definition of the Java language, and the compiler follows
them -- it is not allowed any choice. There may be bugs, of course, but I,
personally, don't know of any.
Tony Morris said:When the compiler complains about those cases, it is correct.
Consider the following example:
It is unreasonable to expect that the compiler should *know that*, in your
case, the int can never be altered by another thread (since it is a
primitive type and cannot be dereferenced), and therefore will evaluate
always to true.
Most JIT compilers will determine that the condition always evaluates to
true (unless it is declared with the volatile keyword to indicate
otherwise), and so will not bother evaluating the expression.
Either way, I'm not convinced that this case was the intended reference.
Unless proven otherwise, I'll maintain that the comment itself was bogus.
It's another example. Those examples were correct (if the 98000009th
digit of pi is actually 3).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.