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.

Results 1 to 5 of 5

Thread: Increment Statements Difference

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Increment Statements Difference

    i was wondering whats the difference between the two
    int x = 0;
     
            x += .1;
     
            x = x + .1;

    given that x is an integer type

    x = x + .1
    ofcourse will be an error because .1 is a floating point number

    but how come with
    x += .1
    it doesnt give any error?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Increment Statements Difference

    From the Java language specifications:

    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);
    Basically, you get a free implicit cast.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (May 10th, 2011)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Increment Statements Difference

    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.

  5. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (May 10th, 2011)

  6. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Increment Statements Difference

    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?

  7. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Increment Statements Difference

    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

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (May 11th, 2011)

Similar Threads

  1. [SOLVED] Help with prefix and postfix(increment&decrement)
    By Lokesh in forum Object Oriented Programming
    Replies: 1
    Last Post: February 12th, 2011, 10:20 AM
  2. [SOLVED] Difference between == and equals.
    By goldest in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 18th, 2010, 03:15 PM
  3. Replies: 1
    Last Post: August 13th, 2010, 06:58 AM
  4. auto-increment of primary key
    By hundu in forum Enterprise JavaBeans
    Replies: 1
    Last Post: May 15th, 2010, 10:55 AM
  5. Need to know this difference
    By arvind in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2010, 06:24 AM