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 6 of 6

Thread: question on calculating

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question question on calculating

    Hi everyone,

    I have this bit of code and I am really confused about the result:
    int i=5;
    	if (i++ == 5 || false){
    	    System.out.println("before: "+i);
     
    	    i += i++;
    	}
    	System.out.println(i);
    before prints 6
    but the final println gives 12. I don't get it. Why is it not 13?

    I'm sorry for the real newbie question but I really don't get the logic here.

    Thanks,
    mC.
    Last edited by copeg; August 9th, 2010 at 01:03 PM. Reason: Please use the code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: question on calculating

    The post fix ++ increments the variable AFTER the variable's current value has been used in the expression.
    i += i++; // add value of i to i then incr i by one

    As you can see this is very confusing. Anyone that codes this way should be fired for writing confusing code.

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: question on calculating

    Hi,

    thanks for your reply. I am still confused though.
    the value of i before the confusing expression is 6.
    So I would expect something then like
    i += i
    i = 6+6
    =12
    then the increment =13.
    But it's not, the return value is 12.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: question on calculating

    Yes its confusing. There is a timing of when this part happens and when that part happens.
    If you want to see the order of the steps, have the compiler output the code it generates. I forget the option.

  5. #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: question on calculating

    If you are unsure of the order of operations, use parenthesis to clarify what it is you intend to have happen.

    The best solution is to only use the ++/-- operators in the pre-fix notation, and to make sure that you don't force the compiler to have to use order of operations rules with these operators.

    Technically it doesn't matter pre-fix or post-fix if you use it in this fashion, but it's a good habit to use pre-fix because in other languages (for example, C/C++) there is the potential for differences in performance between using the pre-fix or post-fix notations (when the user decides they want to overload the ++/-- operators, but this is out of the scope of Java programming).

    So, for example in your above code:

    int i=5;
    ++i;
        if (i == 5 || false){
            System.out.println("before: "+i);
     
            i += i;
            ++i;
        }
        System.out.println(i);

    There is zero ambiguity here and it is very clear what is happening (it'll print out 6, then 13).

    The only way to know for sure what the program is doing is to look at the disassemble of the program. Likely what's happening is that i++ is writing 7 into the memory location where i is. However, when the += gets evaluated, it's using the value of 6 for i and i++, and then writes the result 12 into that same memory location, nuking any changes made by i++.
    Last edited by helloworld922; August 9th, 2010 at 04:49 PM.

  6. #6
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: question on calculating

    Well, the point is not how to make it better, as this was an exam question.

    Seeing the answers now, I feel it's a bit unfair to ask that in a MC test. Ah well...thank you very much for the explanation helloworld922.

Similar Threads

  1. KB/s download speed calculating
    By Koâk in forum Java Theory & Questions
    Replies: 2
    Last Post: December 16th, 2009, 03:05 PM