• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Ternary operator

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI guys, I'm having some problems understandin why this code doesn't compile (from the OCA 8 study book)

What is theresult of the following code snippet?


A: Tiger
B: Lion
C: Tiger is Bigger
D: Lion is Bigger
E: is Bigger
F: doesn't compile because of line 5



I didn't quite understand the explanation which says that it's effectively a metter of operator precedence. Here is my own partial interpretation of the code:
As 250 < 338 only the first part of the expression doesn't execute, leaving us with final (it's the value of tiger that we're trying to copy into the variable statement ). So basically we're trying to assign is Bigger to another literal? is that it?
thanks
 
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Attin wrote:
I didn't quite understand the explanation which says that it's effectively a metter of operator precedence.


Well, basically, it is.... meaning after the compiler applies the precedence (and also, associatively), since the ternary operator has higher precedence than the assignment (and also, assignment has right to left assoc), this expression...

is basically the same as ...

and of course, this statement is invalid because the second assignment has a value on the left hand side (instead of a variable).

So...

Jason Attin wrote: leaving us with final (it's the value of tiger that we're trying to copy into the variable statement ). So basically we're trying to assign is Bigger to another literal? is that it?


Yes. Assigning a value to another value is not allowed. And your analogy worked...

Henry
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool, thanks!
 
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason -

Here's how you can use () to modify and compile the code:



With much respect,
Mala
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic