• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference using "=" vs "+="  RSS feed

 
Silos Antonio
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
Could anyone help me understand the following:

int x=0;
x=x+1.5; this line fails to compile( type mismatch, cannot convert from double to int)

however if we change to x+=1.5; it will compile.

I do not understand it, given that x+=1.5 is equivalent to x=x+1.5;

 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is specified by the JLS:
JLS 15.26.2 wrote:
If the left-hand operand expression is not an array access expression, then:

...

  • Otherwise, the result of the binary operation is converted to the type of the left-hand variable, subjected to value set conversion (§5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable.

  •  
    Mike. J. Thompson
    Bartender
    Posts: 689
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The section that Jelle has quoted refers to compound assignment expressions (such as +=), and starts with the following which may help explain.

    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.


    So you can see x=x+1.5 is not equivalent to x+=1.5
     
    O Roozmand
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    = is just an assignment. a=b b is assigned to a

    you use += when you want to add the left side with another operand:

    a=a+b is the same as a+=b
    a=a-b is the same as a-=b

    ETC
     
    Mike. J. Thompson
    Bartender
    Posts: 689
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If a=a+b was the same as a+=b then Silos wouldn't have got inconsistent results when attempting to compile them. They may be conceptually equivalent, but there is an implicit cast when using the += operator (or any of the other compound assignment operators)
     
    Campbell Ritchie
    Marshal
    Posts: 56581
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to the Ranch O Roozmand
     
    datta Nadgir
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    there is an implicit type casting thats taking place at the left hand side of the operator for only one time. Example;
    x=x+1.5 // error
    x+=1.5+3.5; "+" in the right hand side adds up 1.5 and 3.5,, "+" doesnt work !!(mathematics), all it does is a type casting !!
    you check the difference between statements "x=3.5+4.5" x is an int and "x+=3.5+4.5" you will know it better , whats happening at the left hand side .
    and also analyse what would a compiler do in this case for better understanding.
    x is int;
    x=1.5+4.55555555555555555555555555555555555555555555;

    compiler would be so confused !! how many type castings !!.
    so implicit type casting is done at left hand side for only one time, ;


    hope you have got my point, thanks for posting this question, IT makes me think a llot !!!
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!