aspose file tools*
The moose likes Beginning Java and the fly likes Evaluation order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Evaluation order" Watch "Evaluation order" New topic
Author

Evaluation order

Mani Pava
Greenhorn

Joined: Apr 29, 2008
Posts: 5
Please try this piece of code in JAVA and C++, and you will get two different answers...

java code {

int j = 0 ;
int k = 5 ;
j = k++ + --k;
System.out.println(j) ;
System.out.println(k) ;

}

cpp code {

int j = 0 ;
int k = 5 ;
j = k++ + --k ;
cout << j ;
cout << k ;

}

Answers:
JAVA : j=10, k=5
CPP : j=8, k=5


Please explain....

Thanks in Advance,
Mani
Rich Wright
Greenhorn

Joined: Oct 08, 1999
Posts: 17
I ran both and got what you got for Java but got 9 and 6 for C & C++. I used gcc and g++. The order of precedence is different for the two languages.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

The reason why the results are different is simply that C++ and Java are different programming languages, with different rules for the evaluation of expressions.

If you want to know exactly why you get the answer that you do in Java, you can study the Java Language Specification.

Rich: Are you sure you got 9 and 6, and that you are running the exact code shown above? Because I just tried it out (with g++ 4.2.3 and Java 6 update 6 on Ubuntu Linux) and I do get 8 and 5 for C++, and 10 and 5 for Java.
[ April 29, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Nickolas Case
Ranch Hand

Joined: Apr 26, 2008
Posts: 98
Java does the arithmetic first then increments/decrements. Hence:
J = 5 + 5
K(5)++ = 6
--K(6) = 5

so J = 10 and K = 5

I am not a C guru but it looks like C does subtraction and then addition. arithmetic before increment/decrement. So..
Since there is no arithmetic subtraction...
J = 5++ + 4
Now we do the arithmetic:
J = 9
Now we increment our 5

The end result:
J = 9
K = 6


Hope this helps and if I am wrong on the C someone please chime in set the record straight.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

I think I read somewhere that the C and C++ specifications don't specify what the executing order should be, therefore leaving it completely to the compiler developer. Basically, you could get different results with the same code but 2 different compilers.
[ April 29, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Originally posted by Rob Prime:
I think I read somewhere that the C and C++ specifications don't specify what the executing order should be, therefore leaving it completely to the compiler developer. Basically, you could get different results with the same code but 2 different compilers.
You do. A sinilar question came up about 2 years ago and I tried it with three C compilers. I think it was

int i = 4;
i = i++;
printf("%d", i);

in C. Code from two of my compilers printed 4 and the 3rd printed 5!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Yep, Rob is correct. There are many computations in C/C++ the result of which is officially undefined, meaning it can be anything convenient for the compiler-writer. One of Java's strengths is that its specification eliminates virtually all of these undefined cases; the remaining ones have to to with multithreading.


[Jess in Action][AskingGoodQuestions]
Rich Wright
Greenhorn

Joined: Oct 08, 1999
Posts: 17
I checked and got the same results. It was on a sparc workstation with Solaris 10 using g++ 3.3.2 and and I also tried 3.4.6.

I guess this is a good reason to use () to be sure you're going to get what you what.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Originally posted by Nickolas Case:
Java does the arithmetic first then increments/decrements.
No, the post-increment operator has the highest precendence in the Java precendence tables. The confusion arises because it returns the OLD VALUE.

Pre-increment has a lower precedence than post-increment, and higher than arithmetic, and returns the NEW VALUE.

If you find this confusing, then keep all your increment and decrement operators in statements by themselves.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Evaluation order