• 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

Java unary operator evaluation order

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.class TQ{
2.public static void main(String args[]){
3.int var1=0x14;
4.var1 *=(--var1);
5.System.out.println(var1);
6.}
7.}

please explain the evaluation of expression on line 4.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kajal Dusseja wrote:
please explain the evaluation of expression on line 4.



As specified by the JLS...

https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.7

The evaluation of an expression is done from left to right.  I am assuming that you are confused because the pre-decrement operator is not done before the compound assignment operator?

Henry
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we have,var1=var1*(--var1)
→var1=var1*(--var1)
The R.H.S is an expression and you need to evaluate it.
expressions are always evaluated from left to right.so on rhs var1 will be replaced by its current value which is 0x14.now it will head towards the next operand of "*" which is (--var1) it is an another expression itself which is equivalent to "var1-1=var1".which will have value (0x14-0x1) and it will set this value for var1.now your expression evaluates to value (0x14)*(0x14-0x1) and then it will be assigned to var1.

Note:the value of var1 changes 2 times in this evaluation,1st one when you evaluate --var1 and finally when the expression after evaluation is aasigned to var1.

HIH

Kind Regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And Welcome to CodeRanch!
 
Kajal Dusseja
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Kajal Dusseja wrote:
please explain the evaluation of expression on line 4.



As specified by the JLS...

https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.7

The evaluation of an expression is done from left to right.  I am assuming that you are confused because the pre-decrement operator is not done before the compound assignment operator?

Henry



Yes, I was bit confused over there, but that helped.
Thank You.
 
Kajal Dusseja
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

praveen kumaar wrote:we have,var1=var1*(--var1)
→var1=var1*(--var1)
The R.H.S is an expression and you need to evaluate it.
expressions are always evaluated from left to right.so on rhs var1 will be replaced by its current value which is 0x14.now it will head towards the next operand of "*" which is (--var1) it is an another expression itself which is equivalent to "var1-1=var1".which will have value (0x14-0x1) and it will set this value for var1.now your expression evaluates to value (0x14)*(0x14-0x1) and then it will be assigned to var1.

Note:the value of var1 changes 2 times in this evaluation,1st one when you evaluate --var1 and finally when the expression after evaluation is aasigned to var1.

HIH

Kind Regards,
Praveen.



Yes, got it.
Thank YoU .
 
We can walk to school together. And we can both read 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