i was wondering whats the difference between the twoint x = 0; x += .1; x = x + .1;
given that x is an integer type
ofcourse will be an error because .1 is a floating point numberx = x + .1
but how come withit doesnt give any error?x += .1
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
i was wondering whats the difference between the twoint x = 0; x += .1; x = x + .1;
given that x is an integer type
ofcourse will be an error because .1 is a floating point numberx = x + .1
but how come withit doesnt give any error?x += .1
From the Java language specifications:
Basically, you get a free implicit cast.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);
chronoz13 (May 10th, 2011)
To add to what helloworld said, this is one of those times that looking at the bytecode might help, as the cast is readily seen in all its details (using 'javap -c MyClass'): the integer is cast to a double, added to said double, then cast to back to an int. javap is not an often used tool, but is nice in situations such as this to answer particular questions regarding compiler mysteries such as this.
chronoz13 (May 10th, 2011)
hmmm in my program actually, the game program im writing, there is a method that keeps track of the movement of my animations which has a variable x..
public void theMethod() { x += .1; }
where x is a data member which is an integer type, this method is responsible for the speed of the objects movement(Animation)
i notice that.. when i tried to make it 1 (i.e x += 1) the movement of the object is the same (doest change) even if i make it .1(i.e x += .1) so basically , either .1 or 1 in that statement the movement of the object is the same, i even make a print statement that monitors the movement of that certain object , no matter how i tried to make it .1 or 1, the value is still updating by 1
does it mean that the .1 is being casted or converted to int? converted to 1?
That's equivalent to (very explicitly spelled out):
x = (int)(((double)x) + 1.0);
Likely, the number you're adding results in something slightly smaller than the next integer. So when it's casted back to an int, it gets truncated. See: What every Computer Scientist should know about floating point arithmetic
chronoz13 (May 11th, 2011)