AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

priority of operators

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
alias
Guest





PostPosted: Mon May 16, 2005 3:52 pm    Post subject: priority of operators Reply with quote




Why do i get 2 different results for the 'same' expression (priority of
parentheses) ?
I notice that although (x = 3) is between parentheses in a), the first term
of the addition (x) is evaluated to 10
before working out the second term (x=3).

int x = 10;

a) x = x + (x = 3); -> result x = 13;

b) x = (x = 3) + x; -> result x = 6;


Does anyone know the answer ?

Thanks


Back to top
klynn47@comcast.net
Guest





PostPosted: Mon May 16, 2005 3:58 pm    Post subject: Re: priority of operators Reply with quote



Expressions are evaluated from left to right. In the first case,
originally x is 10. As an expression, an assignment statement has the
value of what is assigned into the left-hand side. So the expression
assigns 3 to x, (x=3) has the value 3, and this is added to the
original 10.

In the second expression, (x=3) has the value 3 and assigns 3 to x, so
you get 6.

Back to top
alias
Guest





PostPosted: Mon May 16, 2005 4:06 pm    Post subject: Re: priority of operators Reply with quote



But the priority-rules are :
1) ( )
2) +
3) =

So in a) normally you first do (x = 3), this evaluates to 3,
then x + 3, this evaluates to 6 because x has been set to 3
and finaly x = 6.

Or am I wrong ?

<klynn47 (AT) comcast (DOT) net> schreef in bericht
news:1116259109.479209.165470 (AT) g43g2000cwa (DOT) googlegroups.com...
Quote:
Expressions are evaluated from left to right. In the first case,
originally x is 10. As an expression, an assignment statement has the
value of what is assigned into the left-hand side. So the expression
assigns 3 to x, (x=3) has the value 3, and this is added to the
original 10.

In the second expression, (x=3) has the value 3 and assigns 3 to x, so
you get 6.




Back to top
klynn47@comcast.net
Guest





PostPosted: Mon May 16, 2005 4:22 pm    Post subject: Re: priority of operators Reply with quote

In the binary operation, +, the left-hand operand appears to be fully
evaluated before any part of the right-hand operand is evaluated. This
is according to the Java Language Specification.

Back to top
alias
Guest





PostPosted: Mon May 16, 2005 4:29 pm    Post subject: Re: priority of operators Reply with quote

Thanks.
This will be the answer to my problem.

<klynn47 (AT) comcast (DOT) net> schreef in bericht
news:1116260555.642693.290920 (AT) g49g2000cwa (DOT) googlegroups.com...
Quote:
In the binary operation, +, the left-hand operand appears to be fully
evaluated before any part of the right-hand operand is evaluated. This
is according to the Java Language Specification.




Back to top
Virgil Green
Guest





PostPosted: Wed May 18, 2005 5:50 pm    Post subject: Re: priority of operators Reply with quote

alias wrote:
Quote:
Thanks.
This will be the answer to my problem.

[email]klynn47 (AT) comcast (DOT) net[/email]> schreef in bericht
news:1116260555.642693.290920 (AT) g49g2000cwa (DOT) googlegroups.com...
In the binary operation, +, the left-hand operand appears to be fully
evaluated before any part of the right-hand operand is evaluated.
This is according to the Java Language Specification.

"will be the answer"? Was this homework?

--
Virgil



Back to top
Simon
Guest





PostPosted: Fri May 20, 2005 6:56 pm    Post subject: Re: priority of operators Reply with quote

Hello,

You can check out www.eassignment.net for help with your assignments.
They are really helpful.

Simon Tanner
alias wrote:
Quote:
Why do i get 2 different results for the 'same' expression (priority
of
parentheses) ?
I notice that although (x = 3) is between parentheses in a), the
first term
of the addition (x) is evaluated to 10
before working out the second term (x=3).

int x = 10;

a) x = x + (x = 3); -> result x = 13;

b) x = (x = 3) + x; -> result x = 6;


Does anyone know the answer ?

Thanks


Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.