• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Urgent...Operator Precedence

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am totally confused with operator precedence.
It is said that expr++ has higher precdence than ++exprlink.
What does this mean?
Let me come down to 2 questions with examples
1.
int x = -1;
int y = ++x + x++;
Will the above line be executed as 1 + -1 = 0
or as 0 + 0 = 0. Because when i try to execute it i will always get the answer as 0 but i don't know which is first evaluated is it ++x or x++. please help!!!
2. Unary operators are evaluated in right to left order. What does this mean...
y = x+++x;
is it equivalent to (x++) + x or x + (++x) .
The answer is same in both cases.
I hope I am clear in my questions.
Thank you
Lalitha
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As for your first question, the precedence has been discussed also in this thread.
 
Lalitha Chandran
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mika Leino
Thanks for the answer....
Just to confirm the answer the first one is executed as
0 + 0 = 0
Thanks
Lalitha
 
Mika Leino
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is correct.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why was this urgent?
 
Lalitha Chandran
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have my exams in a few days. And I was giving one of the mock exams and got this one wrong. And I got a bit tensed... :roll:
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lalitha Chandran:
Hello,
2. Unary operators are evaluated in right to left order. What does this mean...
y = x+++x;
is it equivalent to (x++) + x or x + (++x) .
The answer is same in both cases.
I hope I am clear in my questions.
Thank you
Lalitha


In JLS, it is mentioned that "While translating Unicode stream into sequence of input elements(white spaces, comments and tokens), the longest possible translation is used at each step.
As such x+++x, will always be evaluated as (x++) + x
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About the first question,
I suggest you to read the artical in follow link,
it can help you a lot.
https://coderanch.com/t/190825/java-programmer-SCJP/certification/Array
[ October 02, 2003: Message edited by: Chao Chihwai ]
[ October 02, 2003: Message edited by: Chao Chihwai ]
 
Let nothing stop you! Not even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic