Ramesh Pramuditha Rathnayake wrote:
The final result of x is 100 in java. But in C it is 101.
I know what happens in java. But I don't understand why it's 101 in C.
Is this happened because, C don't have a Virtual Machine..?
Ramesh-X
Ramesh Pramuditha Rathnayake wrote:Thanks for the replies..
I got the answer. You both are trying to say that it is a work done by compiler...
As C compiler make machine code file, the result would be vary from compiler to compiler. But in java the Machine code is generated in JVM. Therefore the result may vary in Java from JVM to JVM..
Not necessarily. The JVM interprets the bytecode, and there may be no machine code generated at all.Ramesh Pramuditha Rathnayake wrote:. . . But in java the Machine code is generated in JVM.
Not at all. As Henry has already said, the JLS is very strict, so such code must behave the same way on all implementations.Therefore the result may vary in Java from JVM to JVM..
Ramesh-X
Ramesh Pramuditha Rathnayake wrote:As I know in postfix and prefix, increment is done first. I think that Mr. Henry said that older C compiler "stated that the increment occurred later"..
Ramesh Pramuditha Rathnayake wrote:
I thought that machine code is generated by JVM(by interpreter) and send to the OS. Is that wrong..? I think Mr. Campbell said that JVM controls the hardware. Not the OS..
Ramesh Pramuditha Rathnayake wrote:
-------------------------------------------
I download a new compiler. I gives answer 100.
But now, there is another problem..
Ramesh Pramuditha Rathnayake wrote:
Java gives 303 and C gives 302.
Then I write the following thing...
Answers are as follows..
Java-------------C-----
100 ------------100
201 ------------201
101-------------101
303-------------302
Why is that..?
It causes no end of confusion with postincrement that people don’t understand that they are dealing with two values. One is the value of i and the other is the value of i++. When you enter the expression the value of i is 101 but i++ is 100 still.Ramesh Pramuditha Rathnayake wrote:As I know in postfix and prefix, increment is done first.
I am quite sure I didn’t say that. The bytecode is interpreted by the JVM and it might do that without creating machine code. You would have to go through the JVM code to find out.. . . I think Mr. Campbell said that JVM controls the hardware. Not the OS..
And when I tried it with GCC I got 301. If you work it out following the Java Language Specification, you can work out how you get 303. You can write the values of the three subexpressions and they add up to 303. Of course, if you divide it up and use assignments, you have a different program which will behave differently. We have an FAQ about that.. . .
Java gives 303 and C gives 302.
. . .
Ramesh-X
CorrectRamesh Pramuditha Rathnayake wrote: . . . When assigning, we have to know that i++ has the earlier value and ++i has the increased value..(In java)
. . .
You're welcome
Thank you for your help..!
Consider Paul's rocket mass heater. |