I have to do this declaration String op = tokens.next();
inside the loop while
, if I put it outside the loop, ie String op=null; while{...op=tokens.next();...}
I get an error that says: The assigned value is never used
, but it is a contradiction because I do use it inside the while
.
Then on other occasions it requires that the declaration be outside the loop, such as in loops for
.
public static int postfixEvaluate(String exp) {
Stack<Integer> s = new Stack<Integer> ();
Scanner tokens = new Scanner(exp);
//String op =null;
while (tokens.hasNext()) {
if (tokens.hasNextInt())
s.push(tokens.nextInt());
else {
int num2 = s.pop();
int num1 = s.pop();
String op = tokens.next();
if (op.equals("+"))
s.push(num1 + num2);
else {
if (op.equals("-"))
s.push(num1 - num2);
else{
if (op.equals("*"))
s.push(num1 * num2);
else
s.push(num1 / num2);
}
}}}
return s.pop();
}}
I. It is neither an error nor a compiler thing
Actually it is not an error, but a warning (warning), but not from the compiler, but rather a functionality of the IDE, in this case Netbeans (as you have confirmed in comments).
This functionality is called Unused Assignment , and it was introduced in NetBeans 7.1:
You can see this JavaWorld article about it: NetBeans 7.1's Unused Assignment and Dead Branch Hints
What happens is what @Flowen says in his answer: you assign a value that you never use , so in colloquial language the IDE tells you: Why assign me a value if you are not going to use it ?
Let's look at two code snippets:
Fragment 1:
The IDE won't ask you for anything, because you assign the value and use it:
Fragment 2:
The IDE will claim you, because you assign the value and don't use it:
And if I do it like this, does he claim me too? Here if I use
op
right?It also claims you, because you assigned
null
for nothing (what is printed is the value ofop
changed to the valuea
.II. It is a functionality loaded with bugs
As already stated in I , this is an IDE feature, which is full of bugs. To check it out, just do this google search:
site:netbeans.org unused assignment
it will show you all about this feature on netbeans.org, not by chance most of the search results are bug reports.III. What I do?
The lesson that Unused assignement wants to teach us is that you do not declare anything with values that you are not going to use.
If you are not going to use the
null
there is no use declaring your variable like this:Declare it like this:
And assign the value only if you are going to use it:
But if you do this:
In
op="b"
it you will have the same warning: Unused assignment . Why? The IDE is not as dumb as it is made out to be. Even though it goes through the loop, thatop=b
one will never be used.The warning would be removed if you did this:
What it tells you is that the value
null
you have assigned to it is never used. And it's true, since you replace it withtokens.next();
inside thewhile
.That is, it tells you about
valor
the variable, not the variableop
itself.The assigned value is never used
means that it is useless to assign a value to that variableop
when declaring it, since the following expression that refers toop
is precisely an assignment of a new value withop = tokens.next();
If you want to declare the variable
op
outside the loopwhile
, you can do so without assigning values to it, like so:and when assigning it inside the loop, you use:
You use the variable
op
as long as the is executedelse
and for the parser, both theif
and theelse
have the same chance of being executed so it warns you that if the is executedif
, the variableop
is not being used. So it is recommended that you declare the variable in theelse
.If you remove the initial assignment the warning will go away:
Or if for example you print the value of
op
theif
, the warning will also disappear:You obviously don't use the variable
op
in the branchif
which indicates that the parser isn't smart enough to ignore the warning.