Results 1 to 14 of 14
Thread: what does "+=" mean in Java?
- 08-25-2010, 03:47 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 1
- Rep Power
- 0
- 08-25-2010, 03:49 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
- 08-27-2010, 03:02 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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;
- 08-27-2010, 05:21 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
- 08-27-2010, 07:29 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
- 08-27-2010, 07:35 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 08-29-2010, 03:02 AM #7
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
I dont understand the difference josAh it still seems like 0+3 to me?
- 08-29-2010, 03:59 AM #8
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. :PJava 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);
- 08-29-2010, 04:02 AM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
- 08-29-2010, 04:03 AM #10
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
However to be fair I think Eranga was assuming the same data types in her answer to the OP
- 08-29-2010, 04:25 AM #11
- 08-29-2010, 04:42 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 08-29-2010, 04:44 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 08-29-2010, 08:28 AM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
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
-
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
[SOLVED] pls help :S . "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerE
By ara in forum New To JavaReplies: 10Last Post: 01-29-2009, 08:00 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks