developers

    Forum Stats

  • 3,873,653 Users
  • 2,266,622 Discussions
  • 7,911,597 Comments

Discussions

Potential inconsistency between += and normal addition

5ecabd38-927b-406b-a504-fa88299f9ac3
edited Jan 22, 2018 11:38AM in Java Programming

One of my students submitted java code with the following code snippet:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

double[] arr = new double[10];

// arr initialized here

int sum = 0;

for(int i = 0; i < arr.length; i++) {

     sum += arr[i];

}

>>>>>>>>>>>>>>>>>>>>>>>>>

I saw this and was very surprised when it actually compiled.

The same loop written using standard addition produces a compile error (can't assign a double to an int.

for(int i = 0; i < arr.length; i++) {

     sum   = sum + arr[i];

}

Seems like this is an important inconsistency between +=  and the normal + operator.

Is it this considered a bug or a feature, and is it known by others?

rpc1

Answers

  • Unknown
    edited Jan 22, 2018 11:38AM
    One of my students submitted java code with the following code snippet:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>double[] arr = new double[10];// arr initialized hereint sum = 0;for(int i = 0; i < arr.length; i++) { sum += arr[i];}>>>>>>>>>>>>>>>>>>>>>>>>>I saw this and was very surprised when it actually compiled.The same loop written using standard addition produces a compile error (can't assign a double to an int.for(int i = 0; i < arr.length; i++) { sum = sum + arr[i];}Seems like this is an important inconsistency between += and the normal + operator.Is it this considered a bug or a feature, and is it known by others?

    There IS NO inconsistency - both operators are working EXACTLY as defined by The Java Language Specification.

    Try this and the 'problem' goes away

    sum   = (int) (sum + arr[i]);

    See how the right-side result is CAST to the appropriate datatype?

    That conforms to the definition/declaration for 'Compound Assignment Operators' as discussed in the doc.

    https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26.2

    15.26.2. Compound Assignment Operators

    A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once. For example, the following code is correct:short x = 3; x += 4.6; 

    and results in x having the value 7 because it is equivalent to:

    short x = 3; x = (short)(x + 4.6); 

    See how the definition INCLUDES '(T)' - 'where T is the type of E1'?

    The rest of that section explains how that construct is interpreted and the rules for execution.

    One of my students submitted java code with the following code snippet:

    Excellent opportunity to have a class discussion about how to find and use the documentation as a resource when they have questions about the language.

    How to FIND  and USE documentation (or other relevant material) is probably the MOST IMPORTANT lesson you could teach them.

    A relatively high percentage of questions in the various Oracle forums are rather easily answered by the Oracle documentation.

    Unfortunately reading the documentation seems to be the LAST thing people with a problem want to do. Instead they want someone else to do the work for them (not talking about you).

    Show your class how to do it - I used a SIMPLE web search for 'java language spec +=' and the second link was the one I provided. Then I found the section for '+='.

    double[] arr = new double[10];// arr initialized here

    Except there IS NO initialization of the array in the code that was posted.

    Another teaching opportunity (lucky you - as if you don't have enough of those!)

    Discuss 'initialization' with your students. And see if they can answer questions like these:

    1. Is 'arr' an object or a primitive? What is the difference?

    2. Is 'arr' in the above declariation NULL? Or has it been initialized by the compiler?

    3. If initialized how many elements does the array contain and what are their values?

    rpc1
This discussion has been closed.
developers