 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
E11 Guest
|
Posted: Mon Oct 24, 2005 3:03 pm Post subject: Precedence of &, ^, |, &&, II |
|
|
Right,
Among &, ^, and |, the precedence is & -> ^ -> |.
And among && and ||, the precedence is && -> ||.
But what happens when these 5 operators are mixed together?
i.e.
g = a & b ^ c | d && e || f;
What is the precedence then?
Thanks and Regards,
Edwin
|
|
| Back to top |
|
 |
Oliver Wong Guest
|
Posted: Mon Oct 24, 2005 3:10 pm Post subject: Re: Precedence of &, ^, |, &&, II |
|
|
"E11" <edwinlee.11 (AT) gmail (DOT) com> wrote
| Quote: | Right,
Among &, ^, and |, the precedence is & -> ^ -> |.
And among && and ||, the precedence is && -> ||.
But what happens when these 5 operators are mixed together?
i.e.
g = a & b ^ c | d && e || f;
What is the precedence then?
|
I'm assuming you're asking in the context of the Java programming
language. A quick google search reveals
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
<quote>
The following table shows the precedence assigned to the operators in the
Java platform. The operators in this table are listed in precedence order:
The higher in the table an operator appears, the higher its precedence.
Operators with higher precedence are evaluated before operators with a
relatively lower precedence. Operators on the same line have equal
precedence. When operators of equal precedence appear in the same
expression, a rule must govern which is evaluated first. All binary
operators except for the assignment operators are evaluated from left to
right. Assignment operators are evaluated right to left.
[table appears here; go to the website to actually see it.]
</quote>
When you're programming in "real life", if you're not sure that the
operator precedence will make the expression do what you want it to do, just
add parenthesis to force the order. It has the added bonus of making it
clear to the next person who has to read your code (who may not be as
familiar with the precedence rules as you are), and it will not degrade
performance on any sane compiler.
- Oliver
|
|
| Back to top |
|
 |
E11 Guest
|
Posted: Mon Oct 24, 2005 4:16 pm Post subject: Re: Precedence of &, ^, |, &&, II |
|
|
This table is great, i couldn't find it (nor any relevant information)
after reading through the JLS. Thanks :)
Regards,
Edwin
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Tue Oct 25, 2005 2:02 am Post subject: Re: Precedence of &, ^, |, &&, II |
|
|
On 24 Oct 2005 08:03:41 -0700, "E11" <edwinlee.11 (AT) gmail (DOT) com> wrote,
quoted or indirectly quoted someone who said :
| Quote: | i.e.
g = a & b ^ c | d && e || f;
What is the precedence then?
|
see http://mindprod.com/jgloss/precedence.html
Then you have a precedence tie, the operators either are evaluated
left to right or right to left. This is where Java made the mistake of
copying C in having by having both behaviours.
The one possible exception would be prefix/postfix ++.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
|
|
| Back to top |
|
 |
E11 Guest
|
Posted: Tue Oct 25, 2005 2:51 pm Post subject: Re: Precedence of &, ^, |, &&, II |
|
|
Roedy Green wrote:
| Quote: |
g = a & b ^ c | d && e || f;
see http://mindprod.com/jgloss/precedence.html
Then you have a precedence tie, the operators either are evaluated
left to right or right to left. This is where Java made the mistake of
copying C in having by having both behaviours.
The one possible exception would be prefix/postfix ++.
|
hmm? Why would it be a precedence tie? From your website, as well as
the one Oliver provided earlier, the precedence of among the five
operators are &, ^, |, &&, || in that order, why would it be a
precedence tie?
(btw, in that case when would a precedence tie actually occur?)
Thanks and Regards,
Edwin
|
|
| Back to top |
|
 |
Oliver Wong Guest
|
Posted: Tue Oct 25, 2005 5:11 pm Post subject: Re: Precedence of &, ^, |, &&, II |
|
|
"E11" <edwinlee.11 (AT) gmail (DOT) com> wrote
| Quote: |
hmm? Why would it be a precedence tie? From your website, as well as
the one Oliver provided earlier, the precedence of among the five
operators are &, ^, |, &&, || in that order, why would it be a
precedence tie?
(btw, in that case when would a precedence tie actually occur?)
|
The * and / operator have the same precedence, so if both of those
operators occur, there's a tie.
<code>
public static void main(String args[]) {
/*In what order are the following terms executed?*/
int x = expressionA() * expressionB() / expressionC();
}
public static int expressionA() {
System.out.println("Expression A");
return 1;
}
public static int expressionB() {
System.out.println("Expression B");
return 1;
}
public static int expressionC() {
System.out.println("Expression C");
return 1;
}
</code>
- Oliver
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Tue Oct 25, 2005 11:26 pm Post subject: Re: Precedence of &, ^, |, &&, II |
|
|
On 25 Oct 2005 07:51:26 -0700, "E11" <edwinlee.11 (AT) gmail (DOT) com> wrote,
quoted or indirectly quoted someone who said :
| Quote: | hmm? Why would it be a precedence tie? From your website, as well as
the one Oliver provided earlier, the precedence of among the five
operators are &, ^, |, &&, || in that order, why would it be a
precedence tie?
|
I did not intend to answer you particular example, just to give you
information in general about precedence in Java. See the link
http://mindprod.com/jgloss/precedence.html
My goal is not to solve your particular problems but to help you to
learn to solve whole classes of problem on your own. In other words I
deliberately make you sweat a bit, but hopefully not so much you get
hopelessly discouraged.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Tue Oct 25, 2005 11:39 pm Post subject: Re: Precedence of &, ^, |, &&, II |
|
|
On Tue, 25 Oct 2005 19:09:00 +0100, Thomas Hawtin
<usenet (AT) tackline (DOT) plus.com> wrote, quoted or indirectly quoted someone
who said :
| Quote: | You are confusing operator precedence and order of evaluation. In Java,
but not C or C++, expressions are evaluated in left to right order. If
you change a line of your code to
int x = expressionA() + expressionB() * expressionC();
|
some exceptions:
= is to the left of the expression the assignment, an the assignment
is done after the expression is evaluated. (In ActV, and Forth the
assignment proceeds left to right after the expression)
(int) is evaluated after the expression on the right.
If my some miracle the editor could have been invented before the
first computer languages, you would be writing Java like this, the way
SPASM is written with a consistent left to right flow of execution.
a + b -> c;
sin( x ) -> height;
byteBuf[i] ^ scrambler (byte) -> byteBuf[ i+off ];
Please view with a fixed pitch font to see the intended alignment.
PS. ActV was a Fortran like language used on the LGP-30, my first
personal computer that had no RAM, and lots of tubes and a rotating
drum. You even had to manually parse the tokens for it my separating
them with '
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|