• Post Reply Bookmark Topic Watch Topic
  • New Topic

A little help to understand a question explanation (Sybex)  RSS feed

 
João Victor Gomes
Ranch Hand
Posts: 85
8
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, how are you guys?

I have a doubt about an explanation in a question of the Mock Exam 2 from the Sybex online tool.

Let's take a look




What it means when it says "because the assignment operator has the highest order of precedence in this expression"? I thought that the ternary would have a higher precedence. And the given example of the parentheses corroborates with that. The explanation shows the following code, to indicate what the compiler does:

So, it means that the ternary has a higher precedence, right? What am I missing?
 
Roel De Nijs
Sheriff
Posts: 11095
173
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
João Victor Gomes wrote:So, it means that the ternary has a higher precedence, right? What am I missing?

The ternary operator is listed above the assignment operator in the Operator Precedence table, so it definitely has a higher precedence. But don't get fooled: to evaluate any expression, operator precedence is definitely not the only thing that matters to define in which order the expression will be evaluated. A very detailed explanation with a boatload of code examples can be found in this excellent topic. Definitely worth reading!

The following statement compiles successfully (note: the parantheses are not optional in this statement)
 
João Victor Gomes
Ranch Hand
Posts: 85
8
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
João Victor Gomes wrote:So, it means that the ternary has a higher precedence, right? What am I missing?

The ternary operator is listed above the assignment operator in the Operator Precedence table, so it definitely has a higher precedence. But don't get fooled: to evaluate any expression, operator precedence is definitely not the only thing that matters to define in which order the expression will be evaluated. A very detailed explanation with a boatload of code examples can be found in this excellent topic. Definitely worth reading!

The following statement compiles successfully (note: the parantheses are not optional in this statement)


Hello Roel

I think that I understand the precedence in this case. I've tested the parentheses yesterday, and it compiles perfectly, as you said. So, with the parentheses, the " is Bigger" is gonna be assigned to the tiger variable, before the return of the ternary operation.
Let me explain what I understand about the code snippet that you gave, and let me know If I'm doing something wrong, if you don't mind

Ok, the statement assignment is evaluated, but the right side must be completed first.
Then, the boolean expression is evaluated, resulting in false. So, the second return (:) of the ternary operator is evaluated. And here is the point. Because of the parentheses, we don't return the original tiger value ("Tiger") right away. First we assign " is Bigger" to it, so the tiger variable now has a new value, then the new tiger value is returned by the ternary operator, and then it is assigned to the variable statement.
If there was no parentheses, the ternary operator would try to return the original value of tiger (which is "Tiger"), and then there would by an attempt to assign a value to another value (in this case, an attempt to assign " is Bigger" to "Tiger") .

My confusion was in the question explanation, when it says "because the assignment operator has the highest order of precedence in this expression". Maybe it should be "assignment operator has the lowest order of precedence", since the ternary would return the original value of tiger first, and then try to apply the assignment, so the expression would try to assign " is Bigger" to a value returned by the ternary operation, and not to a variable. Am I in the right path?

I've read the topic that you mentioned, post by post, and I gotta tell you, it's really a must read for everyone who wants to know about operators precedence. Specially when you and Henry Wong mention about the difference between operators precedence, associativity and order of evaluation. It eliminates any doubts about the subject. I still had some doubts about this subject, but now, after reading that topic, I'm pretty confident to face a tough expression. And by sharing that topic in here, you gave me a lot of code to test tonight hahaha. Thanks for that.

In fact, I've already created a topic about this subject once, and Henry helped me to understand it. Now, I went back there, and there is a message of yours mentioning the topic above, and other topics, but I didn't see the e-mail warning me about new replies. I saw it just today, and I feel bad about that, because I could have learned a lot about it before. But still, as the old saying says, better late than never, right?

Thanks again for sharing your knowledge!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!