Results 1 to 14 of 14
  1. #1
    is55 is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by is55 View Post
    what does "+=" mean in Java?
    It's the 'add assignment' operator; e.g.

    Java Code:
    x= 20;
    x+= 22; // x is 42 now
    There's also an operator -=, *=, /= etc. that subtract, multiply, divide etc. a value from a variable.

    kind regards,

    Jos

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    e.g x=20;
    x+=44;

    the above code says x is equal to 20. the next line of code says hey take x current value and add 44 so now x is 64.

    += means take the variable before + current value and add what is on the right of the equals sign to the current value of what is before the + sign.
    it essentially could be written as x=x+44; which could be x=20+44;

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In other words both x += 20 and x = x + 20 are exactly the same.

    Read the following small article, then you can have a better idea about that.

    How to Use Assignment Operators in Java | eHow.com

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Eranga View Post
    In other words both x += 20 and x = x + 20 are exactly the same.
    No they are not; compare this:

    Java Code:
    byte x= 0;
    int y= 3;
    x+= y;
    with this one:

    Java Code:
    byte x= 0;
    int y= 3;
    x= x+y;
    kind regards,

    Jos

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    True. But I mentioned it because I don't think that Op really touch on with different data types. If he still confused with compound assignment, I don't want to talk about that.

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    I dont understand the difference josAh it still seems like 0+3 to me?

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Java Code:
    byte a = 0;
    int b = 3;
    b = (int)a + b;
    System.out.println(b);
    
    byte x = 0;
    int y = 3;
    x += y;
    System.out.println(x);
    That prints 3 and 3. However, if you do not cast a to int (or do a=a+b instead), it will provide an error due to precision. Truly a byte can't have 3 added to it, cause it's... a byte. :P

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by Zack View Post
    Java Code:
    byte a = 0;
    int b = 3;
    b = (int)a + b;
    System.out.println(b);
    
    byte x = 0;
    int y = 3;
    x += y;
    System.out.println(x);
    That prints 3 and 3. However, if you do not cast a to int (or do a=a+b instead), it will provide an error due to precision. Truly a byte can't have 3 added to it, cause it's... a byte. :P
    of course yea, thanks got it now

  10. #10
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    However to be fair I think Eranga was assuming the same data types in her answer to the OP

  11. #11
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Yes. Assuming same data types it is always the case.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    I dont understand the difference josAh it still seems like 0+3 to me?
    If you've run the code segment at least once, you can see the difference. ;)

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Zack View Post
    Yes. Assuming same data types it is always the case.
    Yep, I assumed that. My point is when answering to a question, only related with the original as much as possible. Because I hope person who post the question will workaround with all the related.

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Zack View Post
    Java Code:
    byte a = 0;
    int b = 3;
    b = (int)a + b;
    System.out.println(b);
    
    byte x = 0;
    int y = 3;
    x += y;
    System.out.println(x);
    That prints 3 and 3. However, if you do not cast a to int (or do a=a+b instead), it will provide an error due to precision. Truly a byte can't have 3 added to it, cause it's... a byte. :P
    That's a very weak reasoning ;-) In the expression 'l op= r' the righthand operand 'r' is converted to the type of the left hand operand 'l' and the operator 'op' is performed on 'l' and 'r'. The result is stored in 'l'.

    In the expression 'l= l op r' both operands are converted to the widest type before the operator 'op' is performed. If the type of the result is wider than the type of 'l' the compiler protests about it because there could be possible loss of precision.

    So in my example 'x+= y' the value of 'y' is converted to byte type while in the expression 'x= x+y' both 'x' and 'y' are converted to ints because 'y' is of type int.

    kind regards,

    Jos

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. Replies: 10
    Last Post: 01-29-2009, 08:00 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •