Search code examples
javavariable-assignmentequalityconvention

Java assignment operator


The following blockquote is taken from http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-137265.html

Do not use the assignment operator in a place where it can be easily confused with the equality operator. Example:

if (c++ = d++) {        // AVOID! (Java disallows)
    ...
}

should be written as

if ((c++ = d++) != 0) {
    ...
}

I am confused by the line

if ((c++ = d++) != 0) {

Any clarification on this would be appreciated.


Solution

  • At the very beginning of the page I can see -

    This page is not being actively maintained. Links within the documentation may not work and the information itself may no longer be valid. The last revision to this document was made on April 20, 1999

        if ((c++ = d++) != 0) {
            //logic
        }
    

    Syntax is not even valid (Using Java6). It gives me

    The left-hand side of an assignment must be a variable
    

    You need to assign c++ to some variable.