• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java unary operator evaluation order

 
Kajal Dusseja
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Sheriff
Posts: 22537
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
praveen kumaar
Ranch Hand
Posts: 443
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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: 443
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to CodeRanch!
 
Kajal Dusseja
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
  • 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 .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!