What is the result of i == (i = 2)?











up vote
22
down vote

favorite
3












Run the following code:



// In Java, output #####
public static void main(String args) {
int i = 1;

if(i == (i = 2)) {
System.out.println("@@@@@");
} else {
System.out.println("#####");
}
}


But:



// In C, output @@@@@,I did test on Clion(GCC 7.3) and Visual Studio 2017
int main(int argc, char *argv) {
int i = 1;

if(i == (i = 2)) {
printf("@@@@@");
} else {
printf("#####");
}

return 0;
}


The motivation for asking this question comes from the following code:



// The code is from the JDK 11 - java.util.concurrent.atomic.AtomicInteger
// I am curious about the behavior of the variable prev.
public final int getAndUpdate(IntUnaryOperator updateFunction) {
int prev = get(), next = 0;
for (boolean haveNext = false;;) {
if (!haveNext)
next = updateFunction.applyAsInt(prev);
if (weakCompareAndSetVolatile(prev, next))
return prev;
haveNext = (prev == (prev = get()));
}
}


So, how to explain the above two different execution modes?









share




















  • 11




    One explains the two different execution modes by first noting that those are two entirely different languages. They happen to share some syntax, but that's where the similarities end.
    – StoryTeller
    yesterday






  • 3




    The result is : a messy code. Better not imitate this unless you are running for a java obfuscation contest.
    – Tristan
    yesterday






  • 3




    Possible duplicate of Logic differences in C and Java
    – user202729
    yesterday










  • (although the specific operator used (==, ++, =, etc.) are different, the answer (it's well defined in Java, sequence point --> undefined behavior) is the same)
    – user202729
    yesterday






  • 1




    Undefined behavior and sequence points
    – phuclv
    yesterday















up vote
22
down vote

favorite
3












Run the following code:



// In Java, output #####
public static void main(String args) {
int i = 1;

if(i == (i = 2)) {
System.out.println("@@@@@");
} else {
System.out.println("#####");
}
}


But:



// In C, output @@@@@,I did test on Clion(GCC 7.3) and Visual Studio 2017
int main(int argc, char *argv) {
int i = 1;

if(i == (i = 2)) {
printf("@@@@@");
} else {
printf("#####");
}

return 0;
}


The motivation for asking this question comes from the following code:



// The code is from the JDK 11 - java.util.concurrent.atomic.AtomicInteger
// I am curious about the behavior of the variable prev.
public final int getAndUpdate(IntUnaryOperator updateFunction) {
int prev = get(), next = 0;
for (boolean haveNext = false;;) {
if (!haveNext)
next = updateFunction.applyAsInt(prev);
if (weakCompareAndSetVolatile(prev, next))
return prev;
haveNext = (prev == (prev = get()));
}
}


So, how to explain the above two different execution modes?









share




















  • 11




    One explains the two different execution modes by first noting that those are two entirely different languages. They happen to share some syntax, but that's where the similarities end.
    – StoryTeller
    yesterday






  • 3




    The result is : a messy code. Better not imitate this unless you are running for a java obfuscation contest.
    – Tristan
    yesterday






  • 3




    Possible duplicate of Logic differences in C and Java
    – user202729
    yesterday










  • (although the specific operator used (==, ++, =, etc.) are different, the answer (it's well defined in Java, sequence point --> undefined behavior) is the same)
    – user202729
    yesterday






  • 1




    Undefined behavior and sequence points
    – phuclv
    yesterday













up vote
22
down vote

favorite
3









up vote
22
down vote

favorite
3






3





Run the following code:



// In Java, output #####
public static void main(String args) {
int i = 1;

if(i == (i = 2)) {
System.out.println("@@@@@");
} else {
System.out.println("#####");
}
}


But:



// In C, output @@@@@,I did test on Clion(GCC 7.3) and Visual Studio 2017
int main(int argc, char *argv) {
int i = 1;

if(i == (i = 2)) {
printf("@@@@@");
} else {
printf("#####");
}

return 0;
}


The motivation for asking this question comes from the following code:



// The code is from the JDK 11 - java.util.concurrent.atomic.AtomicInteger
// I am curious about the behavior of the variable prev.
public final int getAndUpdate(IntUnaryOperator updateFunction) {
int prev = get(), next = 0;
for (boolean haveNext = false;;) {
if (!haveNext)
next = updateFunction.applyAsInt(prev);
if (weakCompareAndSetVolatile(prev, next))
return prev;
haveNext = (prev == (prev = get()));
}
}


So, how to explain the above two different execution modes?









share















Run the following code:



// In Java, output #####
public static void main(String args) {
int i = 1;

if(i == (i = 2)) {
System.out.println("@@@@@");
} else {
System.out.println("#####");
}
}


But:



// In C, output @@@@@,I did test on Clion(GCC 7.3) and Visual Studio 2017
int main(int argc, char *argv) {
int i = 1;

if(i == (i = 2)) {
printf("@@@@@");
} else {
printf("#####");
}

return 0;
}


The motivation for asking this question comes from the following code:



// The code is from the JDK 11 - java.util.concurrent.atomic.AtomicInteger
// I am curious about the behavior of the variable prev.
public final int getAndUpdate(IntUnaryOperator updateFunction) {
int prev = get(), next = 0;
for (boolean haveNext = false;;) {
if (!haveNext)
next = updateFunction.applyAsInt(prev);
if (weakCompareAndSetVolatile(prev, next))
return prev;
haveNext = (prev == (prev = get()));
}
}


So, how to explain the above two different execution modes?







java c language-lawyer variable-assignment side-effects





share














share












share



share








edited 23 hours ago









Solomon Ucko

606718




606718










asked yesterday









kangjianwei

351210




351210








  • 11




    One explains the two different execution modes by first noting that those are two entirely different languages. They happen to share some syntax, but that's where the similarities end.
    – StoryTeller
    yesterday






  • 3




    The result is : a messy code. Better not imitate this unless you are running for a java obfuscation contest.
    – Tristan
    yesterday






  • 3




    Possible duplicate of Logic differences in C and Java
    – user202729
    yesterday










  • (although the specific operator used (==, ++, =, etc.) are different, the answer (it's well defined in Java, sequence point --> undefined behavior) is the same)
    – user202729
    yesterday






  • 1




    Undefined behavior and sequence points
    – phuclv
    yesterday














  • 11




    One explains the two different execution modes by first noting that those are two entirely different languages. They happen to share some syntax, but that's where the similarities end.
    – StoryTeller
    yesterday






  • 3




    The result is : a messy code. Better not imitate this unless you are running for a java obfuscation contest.
    – Tristan
    yesterday






  • 3




    Possible duplicate of Logic differences in C and Java
    – user202729
    yesterday










  • (although the specific operator used (==, ++, =, etc.) are different, the answer (it's well defined in Java, sequence point --> undefined behavior) is the same)
    – user202729
    yesterday






  • 1




    Undefined behavior and sequence points
    – phuclv
    yesterday








11




11




One explains the two different execution modes by first noting that those are two entirely different languages. They happen to share some syntax, but that's where the similarities end.
– StoryTeller
yesterday




One explains the two different execution modes by first noting that those are two entirely different languages. They happen to share some syntax, but that's where the similarities end.
– StoryTeller
yesterday




3




3




The result is : a messy code. Better not imitate this unless you are running for a java obfuscation contest.
– Tristan
yesterday




The result is : a messy code. Better not imitate this unless you are running for a java obfuscation contest.
– Tristan
yesterday




3




3




Possible duplicate of Logic differences in C and Java
– user202729
yesterday




Possible duplicate of Logic differences in C and Java
– user202729
yesterday












(although the specific operator used (==, ++, =, etc.) are different, the answer (it's well defined in Java, sequence point --> undefined behavior) is the same)
– user202729
yesterday




(although the specific operator used (==, ++, =, etc.) are different, the answer (it's well defined in Java, sequence point --> undefined behavior) is the same)
– user202729
yesterday




1




1




Undefined behavior and sequence points
– phuclv
yesterday




Undefined behavior and sequence points
– phuclv
yesterday












3 Answers
3






active

oldest

votes

















up vote
41
down vote













The behaviour of a C program that executes the expression i == (i = 2) is undefined.



It comes from C11 6.5p22:





  1. If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)




The i on the left-hand side of == is a value computation on the value of scalar object i and the right-hand side i = 2 has a side effect of assigning the value 2 to i. The LHS and RHS of == are unsequenced w.r.t. each other. Hence the entire program is meaningless in C.



Compile with gcc -Wall and GCC will spit out:



unsequenced.c:5:16: warning: operation on ‘i’ may be undefined [-Wsequence-point]
if(i == (i = 2)) {
~~~^~~~




Unlike C, Java guarantees the evaluation order for operands (left-to-right), therefore



haveNext = (prev == (prev = get()));


is correct in Java. The value of LHS is determined strictly before the evaluation of the side effect on the RHS occurs.



In C you have to write this as something like



newPrev = get();
haveNext = (prev == newPrev);
prev = newPrev;





share|improve this answer























  • Why is i on the LHS a value computation? Is it specified somewhere in the Standard?
    – Some Name
    yesterday






  • 1




    @SomeName of course it is specified somewhere in the standard. :D
    – Antti Haapala
    yesterday










  • So how about reference? :) The only I could find was the value term defined in 3.19, but no value computation defined there.
    – Some Name
    yesterday










  • @SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. If i there was not a value of an expression then a[i++] = i; would be defined. Footnotes are not normative though.
    – Antti Haapala
    yesterday








  • 1




    @SomeName 5.1.2.3p2 says "value computation of lvalue expression", which is a case here, i is an lvalue and its value need to be computed, as it is not an operand. 6.3.2.1p2 says "converted to the value stored in the designated object", but this is the one referred to by 5.1.2.3p2
    – Antti Haapala
    yesterday


















up vote
12
down vote













The Java Language Specification (§15.7) states:




The Java programming language guarantees that the operands of operators appear
to be evaluated in a specific evaluation order, namely, from left to right.




The specification (§15.21.1) also states that:




The value produced by the == operator is true if the value of the left-hand
operand is equal to the value of the right-hand operand; otherwise, the result is
false.




Therefore in Java, the if-statement at runtime would look like the following, which obviously evaluates to false:



if (1 == 2) {

}


In C, it is simply undefined (see Antti's answer).






share|improve this answer






























    up vote
    3
    down vote













    In C, the behavior of i == (i = 2) is undefined because it attempts to both update an object and use that object’s value in a computation without an intervening sequence point. The result will vary based on the compiler, compiler settings, even the surrounding code.






    share|improve this answer





















    • what about Java?
      – The Great Duck
      23 hours ago











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53577739%2fwhat-is-the-result-of-i-i-2%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    41
    down vote













    The behaviour of a C program that executes the expression i == (i = 2) is undefined.



    It comes from C11 6.5p22:





    1. If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)




    The i on the left-hand side of == is a value computation on the value of scalar object i and the right-hand side i = 2 has a side effect of assigning the value 2 to i. The LHS and RHS of == are unsequenced w.r.t. each other. Hence the entire program is meaningless in C.



    Compile with gcc -Wall and GCC will spit out:



    unsequenced.c:5:16: warning: operation on ‘i’ may be undefined [-Wsequence-point]
    if(i == (i = 2)) {
    ~~~^~~~




    Unlike C, Java guarantees the evaluation order for operands (left-to-right), therefore



    haveNext = (prev == (prev = get()));


    is correct in Java. The value of LHS is determined strictly before the evaluation of the side effect on the RHS occurs.



    In C you have to write this as something like



    newPrev = get();
    haveNext = (prev == newPrev);
    prev = newPrev;





    share|improve this answer























    • Why is i on the LHS a value computation? Is it specified somewhere in the Standard?
      – Some Name
      yesterday






    • 1




      @SomeName of course it is specified somewhere in the standard. :D
      – Antti Haapala
      yesterday










    • So how about reference? :) The only I could find was the value term defined in 3.19, but no value computation defined there.
      – Some Name
      yesterday










    • @SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. If i there was not a value of an expression then a[i++] = i; would be defined. Footnotes are not normative though.
      – Antti Haapala
      yesterday








    • 1




      @SomeName 5.1.2.3p2 says "value computation of lvalue expression", which is a case here, i is an lvalue and its value need to be computed, as it is not an operand. 6.3.2.1p2 says "converted to the value stored in the designated object", but this is the one referred to by 5.1.2.3p2
      – Antti Haapala
      yesterday















    up vote
    41
    down vote













    The behaviour of a C program that executes the expression i == (i = 2) is undefined.



    It comes from C11 6.5p22:





    1. If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)




    The i on the left-hand side of == is a value computation on the value of scalar object i and the right-hand side i = 2 has a side effect of assigning the value 2 to i. The LHS and RHS of == are unsequenced w.r.t. each other. Hence the entire program is meaningless in C.



    Compile with gcc -Wall and GCC will spit out:



    unsequenced.c:5:16: warning: operation on ‘i’ may be undefined [-Wsequence-point]
    if(i == (i = 2)) {
    ~~~^~~~




    Unlike C, Java guarantees the evaluation order for operands (left-to-right), therefore



    haveNext = (prev == (prev = get()));


    is correct in Java. The value of LHS is determined strictly before the evaluation of the side effect on the RHS occurs.



    In C you have to write this as something like



    newPrev = get();
    haveNext = (prev == newPrev);
    prev = newPrev;





    share|improve this answer























    • Why is i on the LHS a value computation? Is it specified somewhere in the Standard?
      – Some Name
      yesterday






    • 1




      @SomeName of course it is specified somewhere in the standard. :D
      – Antti Haapala
      yesterday










    • So how about reference? :) The only I could find was the value term defined in 3.19, but no value computation defined there.
      – Some Name
      yesterday










    • @SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. If i there was not a value of an expression then a[i++] = i; would be defined. Footnotes are not normative though.
      – Antti Haapala
      yesterday








    • 1




      @SomeName 5.1.2.3p2 says "value computation of lvalue expression", which is a case here, i is an lvalue and its value need to be computed, as it is not an operand. 6.3.2.1p2 says "converted to the value stored in the designated object", but this is the one referred to by 5.1.2.3p2
      – Antti Haapala
      yesterday













    up vote
    41
    down vote










    up vote
    41
    down vote









    The behaviour of a C program that executes the expression i == (i = 2) is undefined.



    It comes from C11 6.5p22:





    1. If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)




    The i on the left-hand side of == is a value computation on the value of scalar object i and the right-hand side i = 2 has a side effect of assigning the value 2 to i. The LHS and RHS of == are unsequenced w.r.t. each other. Hence the entire program is meaningless in C.



    Compile with gcc -Wall and GCC will spit out:



    unsequenced.c:5:16: warning: operation on ‘i’ may be undefined [-Wsequence-point]
    if(i == (i = 2)) {
    ~~~^~~~




    Unlike C, Java guarantees the evaluation order for operands (left-to-right), therefore



    haveNext = (prev == (prev = get()));


    is correct in Java. The value of LHS is determined strictly before the evaluation of the side effect on the RHS occurs.



    In C you have to write this as something like



    newPrev = get();
    haveNext = (prev == newPrev);
    prev = newPrev;





    share|improve this answer














    The behaviour of a C program that executes the expression i == (i = 2) is undefined.



    It comes from C11 6.5p22:





    1. If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)




    The i on the left-hand side of == is a value computation on the value of scalar object i and the right-hand side i = 2 has a side effect of assigning the value 2 to i. The LHS and RHS of == are unsequenced w.r.t. each other. Hence the entire program is meaningless in C.



    Compile with gcc -Wall and GCC will spit out:



    unsequenced.c:5:16: warning: operation on ‘i’ may be undefined [-Wsequence-point]
    if(i == (i = 2)) {
    ~~~^~~~




    Unlike C, Java guarantees the evaluation order for operands (left-to-right), therefore



    haveNext = (prev == (prev = get()));


    is correct in Java. The value of LHS is determined strictly before the evaluation of the side effect on the RHS occurs.



    In C you have to write this as something like



    newPrev = get();
    haveNext = (prev == newPrev);
    prev = newPrev;






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday

























    answered yesterday









    Antti Haapala

    79k16148191




    79k16148191












    • Why is i on the LHS a value computation? Is it specified somewhere in the Standard?
      – Some Name
      yesterday






    • 1




      @SomeName of course it is specified somewhere in the standard. :D
      – Antti Haapala
      yesterday










    • So how about reference? :) The only I could find was the value term defined in 3.19, but no value computation defined there.
      – Some Name
      yesterday










    • @SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. If i there was not a value of an expression then a[i++] = i; would be defined. Footnotes are not normative though.
      – Antti Haapala
      yesterday








    • 1




      @SomeName 5.1.2.3p2 says "value computation of lvalue expression", which is a case here, i is an lvalue and its value need to be computed, as it is not an operand. 6.3.2.1p2 says "converted to the value stored in the designated object", but this is the one referred to by 5.1.2.3p2
      – Antti Haapala
      yesterday


















    • Why is i on the LHS a value computation? Is it specified somewhere in the Standard?
      – Some Name
      yesterday






    • 1




      @SomeName of course it is specified somewhere in the standard. :D
      – Antti Haapala
      yesterday










    • So how about reference? :) The only I could find was the value term defined in 3.19, but no value computation defined there.
      – Some Name
      yesterday










    • @SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. If i there was not a value of an expression then a[i++] = i; would be defined. Footnotes are not normative though.
      – Antti Haapala
      yesterday








    • 1




      @SomeName 5.1.2.3p2 says "value computation of lvalue expression", which is a case here, i is an lvalue and its value need to be computed, as it is not an operand. 6.3.2.1p2 says "converted to the value stored in the designated object", but this is the one referred to by 5.1.2.3p2
      – Antti Haapala
      yesterday
















    Why is i on the LHS a value computation? Is it specified somewhere in the Standard?
    – Some Name
    yesterday




    Why is i on the LHS a value computation? Is it specified somewhere in the Standard?
    – Some Name
    yesterday




    1




    1




    @SomeName of course it is specified somewhere in the standard. :D
    – Antti Haapala
    yesterday




    @SomeName of course it is specified somewhere in the standard. :D
    – Antti Haapala
    yesterday












    So how about reference? :) The only I could find was the value term defined in 3.19, but no value computation defined there.
    – Some Name
    yesterday




    So how about reference? :) The only I could find was the value term defined in 3.19, but no value computation defined there.
    – Some Name
    yesterday












    @SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. If i there was not a value of an expression then a[i++] = i; would be defined. Footnotes are not normative though.
    – Antti Haapala
    yesterday






    @SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. If i there was not a value of an expression then a[i++] = i; would be defined. Footnotes are not normative though.
    – Antti Haapala
    yesterday






    1




    1




    @SomeName 5.1.2.3p2 says "value computation of lvalue expression", which is a case here, i is an lvalue and its value need to be computed, as it is not an operand. 6.3.2.1p2 says "converted to the value stored in the designated object", but this is the one referred to by 5.1.2.3p2
    – Antti Haapala
    yesterday




    @SomeName 5.1.2.3p2 says "value computation of lvalue expression", which is a case here, i is an lvalue and its value need to be computed, as it is not an operand. 6.3.2.1p2 says "converted to the value stored in the designated object", but this is the one referred to by 5.1.2.3p2
    – Antti Haapala
    yesterday












    up vote
    12
    down vote













    The Java Language Specification (§15.7) states:




    The Java programming language guarantees that the operands of operators appear
    to be evaluated in a specific evaluation order, namely, from left to right.




    The specification (§15.21.1) also states that:




    The value produced by the == operator is true if the value of the left-hand
    operand is equal to the value of the right-hand operand; otherwise, the result is
    false.




    Therefore in Java, the if-statement at runtime would look like the following, which obviously evaluates to false:



    if (1 == 2) {

    }


    In C, it is simply undefined (see Antti's answer).






    share|improve this answer



























      up vote
      12
      down vote













      The Java Language Specification (§15.7) states:




      The Java programming language guarantees that the operands of operators appear
      to be evaluated in a specific evaluation order, namely, from left to right.




      The specification (§15.21.1) also states that:




      The value produced by the == operator is true if the value of the left-hand
      operand is equal to the value of the right-hand operand; otherwise, the result is
      false.




      Therefore in Java, the if-statement at runtime would look like the following, which obviously evaluates to false:



      if (1 == 2) {

      }


      In C, it is simply undefined (see Antti's answer).






      share|improve this answer

























        up vote
        12
        down vote










        up vote
        12
        down vote









        The Java Language Specification (§15.7) states:




        The Java programming language guarantees that the operands of operators appear
        to be evaluated in a specific evaluation order, namely, from left to right.




        The specification (§15.21.1) also states that:




        The value produced by the == operator is true if the value of the left-hand
        operand is equal to the value of the right-hand operand; otherwise, the result is
        false.




        Therefore in Java, the if-statement at runtime would look like the following, which obviously evaluates to false:



        if (1 == 2) {

        }


        In C, it is simply undefined (see Antti's answer).






        share|improve this answer














        The Java Language Specification (§15.7) states:




        The Java programming language guarantees that the operands of operators appear
        to be evaluated in a specific evaluation order, namely, from left to right.




        The specification (§15.21.1) also states that:




        The value produced by the == operator is true if the value of the left-hand
        operand is equal to the value of the right-hand operand; otherwise, the result is
        false.




        Therefore in Java, the if-statement at runtime would look like the following, which obviously evaluates to false:



        if (1 == 2) {

        }


        In C, it is simply undefined (see Antti's answer).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        Jacob G.

        14.8k51962




        14.8k51962






















            up vote
            3
            down vote













            In C, the behavior of i == (i = 2) is undefined because it attempts to both update an object and use that object’s value in a computation without an intervening sequence point. The result will vary based on the compiler, compiler settings, even the surrounding code.






            share|improve this answer





















            • what about Java?
              – The Great Duck
              23 hours ago















            up vote
            3
            down vote













            In C, the behavior of i == (i = 2) is undefined because it attempts to both update an object and use that object’s value in a computation without an intervening sequence point. The result will vary based on the compiler, compiler settings, even the surrounding code.






            share|improve this answer





















            • what about Java?
              – The Great Duck
              23 hours ago













            up vote
            3
            down vote










            up vote
            3
            down vote









            In C, the behavior of i == (i = 2) is undefined because it attempts to both update an object and use that object’s value in a computation without an intervening sequence point. The result will vary based on the compiler, compiler settings, even the surrounding code.






            share|improve this answer












            In C, the behavior of i == (i = 2) is undefined because it attempts to both update an object and use that object’s value in a computation without an intervening sequence point. The result will vary based on the compiler, compiler settings, even the surrounding code.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            John Bode

            80.6k1375149




            80.6k1375149












            • what about Java?
              – The Great Duck
              23 hours ago


















            • what about Java?
              – The Great Duck
              23 hours ago
















            what about Java?
            – The Great Duck
            23 hours ago




            what about Java?
            – The Great Duck
            23 hours ago


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53577739%2fwhat-is-the-result-of-i-i-2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre