Operator Precedence

K

kbd

Trying to read up on basic java, so this will be a newbee question.

1 2
while( tokenType = tokenizer.nextToken()!= tokenizer.TT_EOF ) {
//Do something

}


Based upon operator precedence operation 2 will take place first.
Meaning nextToken() will be executed and the result compared with TT_EOF.
If the value returned by nextToken() is NOT equal to TT_EOF, the
returned by will be placed in tokenType.

If the value returned is equal to TT_EOF, then the assignment to tokenType
will not take place.

Is this correct?

Regards

kd
 
J

Juha Laiho

(e-mail address removed) (kbd) said:
Trying to read up on basic java, so this will be a newbee question.

1 2
while( tokenType = tokenizer.nextToken()!= tokenizer.TT_EOF ) {
//Do something

}

Based upon operator precedence operation 2 will take place first.
Meaning nextToken() will be executed and the result compared with TT_EOF.
If the value returned by nextToken() is NOT equal to TT_EOF, the
returned by will be placed in tokenType.

If the value returned is equal to TT_EOF, then the assignment to tokenType
will not take place.

No, it would place the comparision result (a boolean value) into
tokenType - so, false if the values were inequal, true if the values
were equal. My best guess is that the above code doesn't even compile.

What I guess was attempted in the above is a common shorthand to assign
and compare in one go, in which case the line should be written as

while( (tokenType = tokenizer.nextToken()) != tokenizer.TT_EOF ) {
...
}

So, call nextToken(); assign whatever it returns to tokenType, and
compare tokenType with TT_EOF -- entering the while loop if tokenType
was not equal to TT_EOF. This is a very handy construct, as writing
the code "open" would (by me, anyway) be considered less readable:

tokenType = tokenizer.nextToken();
while (tokenType != tokenizer.TT_EOF) {
...
tokenType = tokenizer.nextToken();
}


It doesn't seem bad here, but consider a situation where the loop
body has several lines. It may become hard to find out what changes
the value of tokenType from loop iteration to another. Also, with
this construction it'll become messier to prematurely end an iteration
through the loop. With the first construction it's possible to just
to place a "continue;" statement into the loop body to start the
next iteration (with new value of tokenType). With this latter form,
yet another separate call to nextToken() would be needed (or
alternatively a nested if structure within the loop).
 
T

Tim Ward

Is this correct?

Doesn't matter.Just use the same precedence rule that works for *every*
programming language and it will work for Java.

The rule is: "If you don't know what the precedence is, put in the
brackets."

This has the advantage of not only working for you, but also saving time for
the next person to read the code.
 
K

kbd

Tim Ward said:
Doesn't matter.Just use the same precedence rule that works for *every*
programming language and it will work for Java.

The rule is: "If you don't know what the precedence is, put in the
brackets."

This has the advantage of not only working for you, but also saving time for
the next person to read the code.

Yes very true parentheses would make it much clearer.
However, on interviews you very often spend time on trivia.

kd
 
K

kbd

Juha Laiho said:
(e-mail address removed) (kbd) said:

No, it would place the comparision result (a boolean value) into
tokenType - so, false if the values were inequal, true if the values
were equal. My best guess is that the above code doesn't even compile.

What I guess was attempted in the above is a common shorthand to assign
and compare in one go, in which case the line should be written as

while( (tokenType = tokenizer.nextToken()) != tokenizer.TT_EOF ) {
...
}

So, call nextToken(); assign whatever it returns to tokenType, and
compare tokenType with TT_EOF -- entering the while loop if tokenType
was not equal to TT_EOF. This is a very handy construct, as writing
the code "open" would (by me, anyway) be considered less readable:

tokenType = tokenizer.nextToken();
while (tokenType != tokenizer.TT_EOF) {
...
tokenType = tokenizer.nextToken();
}


It doesn't seem bad here, but consider a situation where the loop
body has several lines. It may become hard to find out what changes
the value of tokenType from loop iteration to another. Also, with
this construction it'll become messier to prematurely end an iteration
through the loop. With the first construction it's possible to just
to place a "continue;" statement into the loop body to start the
next iteration (with new value of tokenType). With this latter form,
yet another separate call to nextToken() would be needed (or
alternatively a nested if structure within the loop).


Thanks for your reply.
You are correct it does not compile.
Parentheses are missing. Miss print in the book.

thanks again.

kd
 
T

Tim Ward

kbd said:
The rule is: "If you don't know what the precedence is, put in the
brackets."

Yes very true parentheses would make it much clearer.
However, on interviews you very often spend time on trivia.[/QUOTE]

If I were asked a question on operator precedence at interview I would say
"I haven't a clue, I just put the brackets in then I can worry about
something important instead". It is very rare indeed that I don't get a job
I interview for.
 
R

Roedy Green

The rule is: "If you don't know what the precedence is, put in the
brackets."

This has the advantage of not only working for you, but also saving time for
the next person to read the code.

Java has a rather complex 13-level precedence structure. Keep in mind
others may not know the fine points, especially of the more obscure
operators, so when in doubt add () for others not so bright as you.
 
G

Grant Wagner

kbd said:
Trying to read up on basic java, so this will be a newbee question.

1 2
while( tokenType = tokenizer.nextToken()!= tokenizer.TT_EOF ) {
//Do something

}

Based upon operator precedence operation 2 will take place first.
Meaning nextToken() will be executed and the result compared with TT_EOF.
If the value returned by nextToken() is NOT equal to TT_EOF, the
returned by will be placed in tokenType.

If the value returned is equal to TT_EOF, then the assignment to tokenType
will not take place.

Is this correct?

Nope.

The code won't even compile unless tokenType is declared as a boolean. I'm
surprised no one has pointed this out up to now.

What happens is: the comparison is done, the result of the comparison is a
boolean, Java attempts to place the boolean in tokenType (which is not a
boolean) and you get a compile time error.

Java never attempts to store the value of either tokenizer.nextToken() or
tokenizer.TT_EOF in tokenType.
 
D

Dale King

kbd said:
Doesn't matter.Just use the same precedence rule that works for *every*
programming language and it will work for Java.

The rule is: "If you don't know what the precedence is, put in the
brackets."
[/QUOTE]


To which I also add that you should try to not know what the precedence is
except for the most common and obvious cases.
 
R

Roedy Green

To which I also add that you should try to not know what the precedence is
except for the most common and obvious cases.

It is not that you don't want to know it. Otherwise how to do read
other people's code? It is just you don't want to write fancy pants
code that depends on the fine points of it.
 

Ask a Question

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.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top