I ran below program in C++(Dec-c++) :
int k = 5;
k = k++;
cout<<"Value of K :"<<k<<endl;
int l = 5;
l = l++ + l++;
cout<<"Value of L :"<<l<<endl;
int m = 5;
m = m++ + m++ + m++;
cout<<"Value of M :"<<m<<endl;
and got 5,12 and 18 for variables k,l and m.. But same program when I ran in java --
int k = 5;
k = k++;
System.out.println("Value of K :"+k);
int l = 5;
l = l++ + l++;
System.out.println("Value of L :"+l);
int m = 5;
m = m++ + m++ + m++;
System.out.println("Value of M :"+m);
I got 5,11 and 18 for variables k,L and M..
Explain why there is difference in calculations of unary post operators ? I am aware of operator's precedence and priority rules. But every thing fails here. But answer from c++ seems rational according to rules. Confused how java is calculating ?
It's well known C++ and Java are different. Please Suggest how operator-precedence and operator-priority is handled in Java.