This discussion is archived
14 Replies Latest reply: Jul 20, 2012 1:46 AM by EJP RSS

Code Behavior is different in C and Java.

950445 Newbie
Currently Being Moderated
Code Behavior is different in C and Java.

public class Incr {
     

     /**
     * @param args
     */
     public static void main(String[] args) {
          int x=1;
          x=x++;
          System.out.println(x);
          System.out.println(x);
     
     }

}
Expected output is 1 1 but actual output is
0
0

I tried same code in C as well and output is 1 1 , so why in java we are getting output as 0 0
Please explain about this.
Email id: samadhanborse@gmail.com
  • 1. Re: Code Behavior is different in C and Java.
    rp0428 Guru
    Currently Being Moderated
    public class Incr {
    /**
    * @param args
    */
    public static void main(String[] args) {
    int x=1;
    x=x++;
    System.out.println(x);
    System.out.println(x);
    }
    }
    Expected output is 1 1 but actual output is
    0
    0
    I get 1,1 as output.
  • 2. Re: Code Behavior is different in C and Java.
    950445 Newbie
    Currently Being Moderated
    Sorry there was some change in code , please try it now...and reply..

    Code Behavior is different in C and Java.

    public class Incr {

    /**
    * @param args
    */
    public static void main(String[] args) {
    int x=0;
    x=x++;
    System.out.println(x);
    System.out.println(x);
    }

    }
    Expected output is 1 1 but actual output is
    0
    0

    I tried same code in C as well and output is 1 1 , so why in java we are getting output as 0 0
    Please explain about this.
    Email id: samadhanborse@gmail.com
  • 3. Re: Code Behavior is different in C and Java.
    rp0428 Guru
    Currently Being Moderated
    >
    I tried same code in C as well and output is 1 1 , so why in java we are getting output as 0 0
    Please explain about this.
    >
    Yep - that gives 0,0 just like it is supposed to do.

    As per the Java Language Spec section 15.14.2
    http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.14.2
    >
    The value of the postfix increment expression is the value of the variable before the new value is stored.
    >
    So what is the value of x before the new value is stored? It is zero. So the value of the postfix increment expression is zero and that is assigned to x.
  • 4. Re: Code Behavior is different in C and Java.
    EJP Guru
    Currently Being Moderated
    Code Behavior is different in C and Java.
    I agree. They are different languages. The behaviour of expressions with side-effects is specifically very different, and only partly specified in C. If you expect them to behave the same and they don't, the only problem is with your mistaken expectation.
  • 5. Re: Code Behavior is different in C and Java.
    gimbal2 Guru
    Currently Being Moderated
    EJP wrote:
    Code Behavior is different in C and Java.
    If you expect them to behave the same and they don't, the only problem is with your mistaken expectation.
    Its one of those moments that leads up to the realization that programming is in fact difficult - assumptions really are the mother of all production failures causing ships to sink, bridges to collapse and space shuttles to fall from the sky.

    Exciting job we have!
  • 6. Re: Code Behavior is different in C and Java.
    950445 Newbie
    Currently Being Moderated
    Can you please explain why the behavior of this code is different in C and Java. As in both programs we have done post increment but still output is different, Why??

    Is post increment works differently in C and Java??
  • 7. Re: Code Behavior is different in C and Java.
    Tolls Journeyer
    Currently Being Moderated
    user13544180 wrote:
    Can you please explain why the behavior of this code is different in C and Java. As in both programs we have done post increment but still output is different, Why??

    Is post increment works differently in C and Java??
    It can work differently in different C implementations.
    As far as I'm aware this is an undefined thing in C (as EJP implies I think)?

    The important thing (for this forum anyway) is that it's defined in Java.
  • 8. Re: Code Behavior is different in C and Java.
    EJP Guru
    Currently Being Moderated
    Can you please explain why the behavior of this code is different in C and Java. As in both programs we have done post increment but still output is different, Why??
    Because (a) they are different languages; (b) the behaviour of expressions with side-effects is specifically very different; (c) it is only partly specified in C; and (d) if you expect them to behave the same and they don't, the only problem is with your mistaken expectation.

    What part of that wasn't clear the first time?
    Is post increment works differently in C and Java?
    If it doesn't, what is this thread about?
  • 9. Re: Code Behavior is different in C and Java.
    800381 Explorer
    Currently Being Moderated
    user13544180 wrote:
    Can you please explain why the behavior of this code is different in C and Java. As in both programs we have done post increment but still output is different, Why??
    It's a lesson in why you never, ever write code that depends on the order of side effects.

    No matter what your language is.
  • 10. Re: Code Behavior is different in C and Java.
    aksarben Journeyer
    Currently Being Moderated
    Because they're different languages (doh!). That's a little asking, "why do greetings sound different in English & German?"

    What C, Pascal, COBOL or Lisp do or don't do is irrelevant to this forum.

    On the other hand, if you're trying to understand why Java behaves a certain way, the first place to look is the language specification. If that doesn't answer your question, Google is a good next step. If you're still stumped, THEN come to this forum.

    But to ask why two languages work differently is a ludicrous question from the start.
  • 11. Re: Code Behavior is different in C and Java.
    rp0428 Guru
    Currently Being Moderated
    >
    Can you please explain why the behavior of this code is different in C and Java. As in both programs we have done post increment but still output is different, Why??

    Is post increment works differently in C and Java??
    >
    You didn't post any c code for anyone to look at so until you do we can't even be certain that your c example is, in fact, equivalent.

    This Microsoft article has what I consider to be a classic example of using the postfix operator in c
    http://msdn.microsoft.com/en-us/library/cw4d5fdx.aspx
    >
    if( var++ > 0 )
    p++ = q++;

    In this example, the variable var is compared to 0, then incremented. If var was positive before being incremented, the next statement is executed. First, the value of the object pointed to by q is assigned to the object pointed to by p. Then, q and p are incremented.
    >
    But in C a variable like p and q above are 'pointers' and are not integer variables. So the statement '*p++ = *q++' is really performing two separate operations as the test says.

    One operation is to make as assignment: the assignment IS NOT as assignment of q to p but an assignment of the object POINTED TO by q to the object POINTED TO by p. Then the pointers themselves are incremented. But the increment IS NOT necessarily (or even usually) by an integer value of 1; it is an increment of the length of the type of object that p or q point to. So if I increment a pointer to a list of objects (an array perhaps) that are each 8 bytes long then p++ will increment to point to the next object or the equivalent of p + 8 (not really but for purposes of explanation).

    So the very definition of what ++ or -- does and by how much it does it are definitely different in C and Java. Java has no pointers - when you use ++ or -- it is incrementing the only primitive involved.
  • 12. Re: Code Behavior is different in C and Java.
    950445 Newbie
    Currently Being Moderated
    Code is the same in Java and C , and we are not using any pointers in this code .. Only primitives are involved , hen why the behavior of the code is different
    in the two language.


    Please explain??
  • 13. Re: Code Behavior is different in C and Java.
    gimbal2 Guru
    Currently Being Moderated
    user13544180 wrote:
    Code is the same in Java and C , and we are not using any pointers in this code .. Only primitives are involved , hen why the behavior of the code is different
    in the two language.


    Please explain??
    EDIT: nvm.
  • 14. Re: Code Behavior is different in C and Java.
    EJP Guru
    Currently Being Moderated
    Code is the same in Java and C
    But C and Java are not the same, as you have now been told at least four times.

    Unless you have evidence that Java isn't complying with its Language Specification, your question is also off-topic.

    Locking this futile thread.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points