0

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.

Jain
  • 65
  • 2
  • 8
  • 4
    The C++ one is undefined behaviour. The Java one is documented. – chris Feb 11 '14 at 05:38
  • 2
    possible duplicate of [Undefined Behavior and Sequence Points](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) - C++ and Java are two different languages; in Java the behavior is explicitly defined. – Brian Roach Feb 11 '14 at 05:41
  • @BrianRoach: That question explains the C++ behavior. He's not curious about the C++ behavior (though he should be, as his interpretation of it is entirely wrong). It's the Java behavior that he doesn't understand. – Benjamin Lindley Feb 11 '14 at 05:43
  • @chris.. may be c++ is not documented but have well defined rules for calculating such expressions. And if Java is well documented then please suggest any refined rule. – Jain Feb 11 '14 at 05:44
  • @BenjaminLindley It's kinda obvious the doesn't understand either ;) This is a dup question on both sides, I picked the C++ side. – Brian Roach Feb 11 '14 at 05:44
  • @ALEN please try[this link](http://stackoverflow.com/a/21180773/3110262) this will help you in C++ – A B Feb 11 '14 at 05:44
  • @ALEN No, it doesn't. This is **undefined behavior** in C++. See the Dup – Brian Roach Feb 11 '14 at 05:45
  • Actually, I found the perfect Dup on the second try: http://stackoverflow.com/questions/11311140/unary-operators-in-java-vs-c?rq=1 , covers both – Brian Roach Feb 11 '14 at 05:47
  • @ALEN please try[this link](http://stackoverflow.com/a/21180773/3110262) this will help you in C++ and for java go through [operator Precedence](http://www.seas.upenn.edu/~palsetia/java/precedenceTable.html). – A B Feb 11 '14 at 05:50
  • Does C++ really return 12 for l? In what universe does that make any sense? I understand that the order of the ++ operations is undefined in C++, but ++ precedes +, so this should either be equal to 5 + 6 or 6 + 5, and last time I checked, neither of these was 12. – Dawood ibn Kareem Feb 11 '14 at 06:01
  • @DavidWallace: It's not just the order of the operations that is undefined. It is the behavior of the whole expression that is undefined. The compiler is free to assume that you don't do things like this. Imagine what ways you might translate and optimize the expression `i = j++ + k++;` into machine code if `i`, `j` and `k` are distinct variables. Well even if they aren't distinct variables, the compiler may very well assume they are, and depending upon the translation/optimization, that can cause strange resutlts. – Benjamin Lindley Feb 11 '14 at 06:12
  • @DavidWallace It could return that, or unicorns could fly out :-D – Brian Roach Feb 11 '14 at 06:13
  • +1 for showing (again) what you should never use. – Hannes Feb 11 '14 at 06:14

1 Answers1

1

The rule for Java is quite simple here. The sub-expressions (each call to the postfix operator) are evaluated left to right, each one being fully evaluated before the next.

int k = 5;
k = k++;

This increments k to 6, but returns its previous value (5) and reassigns that to k. So k is 5.

int l = 5;
l = l++ + l++;

The first l++ increments l to 6, and evaluates to its previous value (5). Then the next l++ is called, which increments l to 7, and evaluates to 6. The 5 and 6 are added together and assigned to l, resulting in 11.

int m = 5;
m = m++ + m++ + m++;

Using the same process as before, we just do it one more time, so this is equivalent to adding 5 + 6 + 7 and assigning the result to m, which becomes 18.

The C++, despite your claim that you understand it, is not so simple. In fact, it is undefined behavior, as thoroughly explained in the answers to this question. There are no rules, and you can see any results whatsoever. Don't do it.

Community
  • 1
  • 1
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • @Benjamin.. I have also made same workaround like you have shown above. Surprisingly it works and gives correct answer. But not sure it's a workaround or can be taken as a DEFINED RULE. – Jain Feb 11 '14 at 06:07
  • 1
    @ALEN: What workaround are you talking about? Workaround to what? – Benjamin Lindley Feb 11 '14 at 06:09
  • @Benjamin.. I'm talking about "5+6+7 workaround". Unary increment and decrement operators manipulate upfront the memory location of oprands. So in C++ it will be like first add all 5 and then perform unary operators. eg .5+5+5=15 and then perform three post decrement – Jain Feb 11 '14 at 06:15
  • @ALEN There's no "workaround" he's explaining *how it is defined and works*. And no, in C++ it doesn't work like that since again, the behavior is undefined. – Brian Roach Feb 11 '14 at 06:19
  • Thanks @BenjaminLindley. From now on it is a well structured rule for me. – Jain Feb 11 '14 at 09:20