Strange Post-Increment/Assignment Behaviour

I have a problem understanding the code snippet below :
public class trythis {
     public static void main(String args[])
          int i = 0;
          i = i++; // ??
          System.out.println(i);
I thought that the output of this program should be 1, but when I ran the progam it gave the output 0.
I don't understand the reason. Can anyone help me understand the logic behind this?
Thanks.

OK so here is an elaborate explanation:
This is an assignment:
i = i++;
According to the Java Language Specification �15.26.1 the first thing that happens is that the left hand side is evaluated. It clearly evaluates to the variable i. The next thing that happens is that the right hand side (i++) is evaluated. So let's look at that...
This is a postfix increment expression:
i++
According to the Java Language Specification �15.14.1 the following happens: The variable is evaluated (this is i which is 0), 1 is added to the variable value (giving a value of 1) and the result is stored in the variable (making i=1). The value of the whole expression (i++) is the value of the variable before it was incremented. The variable i had the value 0 before it was incremented so the value of the right hand side is 0. Now back to the assignment...
The right hand side has now evaluated to 0. The value of the right hand side is then stored in the variable denoted by the left hand side (i). So after the assignment i has a value of 0.
Søren

Similar Messages

  • Pre/Post increment query

    I have a query regarding Pre/Post increment.
    The following code:
    int x = 0;
    int y = 0;
    x += y++;
    System.out.println("x = " + x);
    System.out.println("y = " + y);
    Produces the following output:
    x = 0
    y = 1
    (Value of y is assigned to x, y is then post incremented).
    This makes sense to me. However the following code,
    the value of x does not get incremented. Can someone
    explain this to me?
    The following code:
    int x = 0;
    x += x++;
    System.out.println("x = " + x);
    Produces the following output:
    x = 0
    (Value of x is assigned to x, then x does not get post incremented??).

    I code with the mind set of "If the code is not self explanatory or obvious, then
    it's too confusing and should be avoided".
    I am studying for the Java Programmer Certification exam, so this query was more
    to try and understand why the output did not match what I had expected.
    So
    x += x++;
    is the same as (where x' refers to the original value of x)
    x = (x) + (x++);
    which is then executed in the following order:
    x = x++; //(x = x + 1) post incrementation
    x = x' + x'; //(x = 0 + 0) evalutation of the assignment
    Thanks for all the responses!!

  • Post increment problem. please help to solve it

    public class test {
    public static void main(String args[])
         int i=10;
         i=i++;
         System.out.println(i); // value of i is 10 why
    }

    Mostafa.Hashem wrote:
    Absofuckinglutely NOT !
    1) Save current value of x. That is, 1. This is the value of the expression "x++".
    2) Increment x. x is now 2. We have now completed x++
    3) Assign value from step 1 into y.i wonder how small point like this takes all that time.
    Mr Jverd,
    the difference between prefix increment and the postfix increment is :
    IN PREFIX INCREMENT THE ASSIGNMENT OF EXPRESSION DONE FIRST THEN THE INCREMENT.
    IN POSTFIX INCREMENT THE INCREMENT DONE BEFORE THE ASSIGNMENT.Wrong.
    ++x and x++ are both expressions. They have a value and a side effect.
    In pre-increment (++x) the increment is done before the expression is evaluated. The value of the expression is the new value of x.
    In post-increment (x++) the increment is done after the expression is evaluated. The value of the epxression is the original value of x.
    In both cases
    y = ++x;
    y = x++;
    The right-hand side is evaulated completely before the assignment to y is done. In fact, in ALL java assignment statements, the RHS is evaluated completely before assigning anything to the LHS.
    y = ++x;
    is equivalent to
    x +=1;
    y = x;
    y = x++;
    is equivalent to
    tmp = x;
    x += 1;
    y = tmp;

  • Post increment question

    I was looking at this puzzle question:
    public class Assignment {
    public static void main(String[] a) throws Exception {
    int tricky = 0;
    for (int i = 0; i < 3; i++)
    tricky += tricky++;
    System.out.println(tricky);
    tricky is 0. How come the post increment in this case does nothing?

    I was looking at this puzzle question:
    public class Assignment {
    public static void main(String[] a) throws Exception
    int tricky = 0;
    for (int i = 0; i < 3; i++)
    tricky += tricky++;
    System.out.println(tricky);
    tricky is 0. How come the post increment in this case
    does nothing?
    http://forum.java.sun.com/thread.jspa?forumID=54&threadID=753569

  • Post Increment Operator Question

    I had written a small program to test the working of Post increment operator.
    public static void main(String[] args)
         int a = 4;
         a = a++;
         System.out.println ( a );
    I expected the output to be '5', but the output given is '4'.
    Can anyone tell me to how this works in Java ?

    There is a big difference between a++ and ++a.
    a++ means increment a AFTER you have performed the line.
    ++a mean increment a BEFORE you perform the line.
    class Test
    public static void main(String[] args)
    int a = 4;
    a = ++a; //returns a = 5
    // a = a++; //returns a = 4
    //a++; //returns a = 5
    //++a; //returns a = 5
    System.out.println ( a );
    }

  • Behaviour of post increment/decrement operator

    int i = 1;
    i = ++i;
    System.out.println( i ); //output is 2
    i = i++;
    System.out.println( i ); // output is 2, WHY?

    Well, it seems that you didn't get the point at all.
    I know how to do the increment operation. What I
    intended to know is the behavior in this scenario.
    And you got that in reply 2.I assumed the answer to be that. but i was only trying to be sure.
    I know that such codes r not in common use and they are meaningless too, but such codes do come up in various quizzes, etc. to test ur knowledge. we always learn WHAT not to do, but we seldom try to know WHY not to do it. in our endeavor to learn advanced topics we tend to ignore the basics.
    Coming back to the original code can someone tell me whether this is a well defined behavior unlike c/c++ where the behavior of such codes are undefined. as far as i know java is a strongly typed language, where ambiguities have been eliminated (believe i am not wrong on this). Can i expect the same results on all compilers??
    and i hope that the forum behaves in a sane manner in future. Instead of blatantly dismissing a post, let people give reasons for it.
    Thanks

  • Strange assign behaviour

    Hi,
    I have defined variable (inputVariable) based on complex message type.
    I need create copy of this variable for making changes on it.
    So I created new variable (inputCopyVariable) based on the same complex message type.
    In assign activity I assign
    <copy>
    <from variable="inputVariable"/>
    <to variable="inputCopyVariable"/>
    </copy>
    Then I assign new values to inputCopyVariable.
    <copy>
    <from variable="checkingVariable" part="checkingResponseElement" query="/ns3:checkingResponseElement/sum"/>
    <to variable="inputCopyVariable" part="payload" query="/client:inputRequest/client:price"/>
    </copy>
    I was shocked when I discovered that inputVariable was changed too.
    So I experiment and it is true, when I change any of this variables then BPEL makes a change to both variables.
    I need independent variables, how to do this.
    I can use transformation. There is everything all right.
    Thanks.

    I think it is not a problem of displaying the variables in the BPELConsole.
    I make experiment:
    simple process with two vriables created with same message type.
    1) variable1.note = 'new'
    2) variable2 = variable1
    3) variable1.note = 'change'
    4) variable2.text = 'text'
    5) <case condition="bpws:getVariableData('variable1','payload','/client:vars/client:note') = bpws:getVariableData('variable2','payload','/client:vars/client:note')">
    if condition is true then process return 1 else return 0
    Testing this I can see changing both variables and get comparing values by BPEL.
    Result is: changing variable1 cause change variable2 and process return 1 (values of vriables are same).

  • Clearing Vendor : Header data - Posting with assignment problem

    Hi,
    I would like to write a program or if possible would like to use a BAPI  to post Clearing vendor data in t-code F-44 with assignment option.
    I looked at to bapi BAPI_ACC_INVOICE_RECEIPT_POST and BAPI_ACC_DOCUMENT_POST, but both of them needs tax key and in F-44 we do not use it. as an other example we'd like to clear it with the acc. document and  Line Item of acc. doc .(BSEG-BUZEI) 
    Is there any idea about how to do this?
    Kind Regards.

    Hi,
    my suggested method :
    1) works
    2) is very simple
    sample:
    * 0 initialization
      xbkpf = bbkpf.
    *1 header - fill xbkpf
      MOVE: 'FB05' TO xbkpf-tcode.
      MOVE itab-bukrs TO xbkpf-bukrs.
      MOVE '90'  TO xbkpf-blart.
      CONCATENATE sy-datum+6(2) sy-datum+4(2) sy-datum(4)
                  INTO xbkpf-bldat.
      CONCATENATE sy-datum+6(2) sy-datum+4(2) sy-datum(4)
                  INTO xbkpf-budat.
      MOVE itab-waers TO xbkpf-waers.
      xbkpf-auglv = 'UMBUCHNG'.
      TRANSFER xbkpf TO file.
    *2 fill bselk
      xselk = bselk.
      xselk-agkon = itab-lifnr.
      xselk-agbuk = itab-bukrs.
      xselk-agkoa = 'K'.
      xselk-xnops = 'X'.
      TRANSFER xselk TO file_si.
    *3 fill bselp (clearing criterion)
        xselp = bselp.
        xselp-feldn_1 = 'SGTXT'.
        MOVE  itab-sgtxt TO xselp-slvon_1.
        TRANSFER xselp TO file_si.
    regards Andreas

  • How to get the source of a strange posted pic into my camera roll?

    I got a strange pic put into my camera roll, this pic is most likely put by an app which has an access to my photo gallery, all I want to know is how to get the source which put this image into my camera roll, I have a punch of apps that have a photo access grant and really don't know to disable all of these apps because of only one of them.
    p.s: the apps with photo access in my device (ProCam, CameraArtFXFree, Photo Editior-, Instagram, Poster++, Photo Vault, Facebook, Tango, Viber, Y! Messenger, Ipadio, Line, WhatsApp) And the only opened apps when this photo pushed to my Camera Roll were (Viber, Tango, Line, Whatsapp).
    Thanks in advance.

    >
    Nitesh Kumar wrote:
    > Hi,
    >
    > FM to get the program source code: RPY_PROGRAM_READ
    >
    > By using this FM you can get the program name(say report_name) and then you can use
    >
    > READ REPORT report_name INTO itab
    >
    > Thanks
    > Nitesh
    u dont need the last statement the FM itself returns an itab with code in it.

  • Approval Workflow Task assigned behaviour

    I have a strange issue that has started recently. On a approval workflow, if the users do not add anyone to the cc line on the task assignment, there is only one email generated to notify the recipient of the task . However, if they put someone on the
    cc line as well, a email will get generated for each person on the cc list that will also include all the other recipients. So there are 3-4 emails being generated.  What could cause this and how can I fix it?

    Hi,
    Let verify the following:
    Whether you mean that when you add people to the cc line, several emails will be sent to the same recipient. if it is, perhaps your cc object 
    is a group contains the recipient.
    Whether your approval workflow created with SharePoint Designer 2010 platform. Maybe
    the following checkbox is enabled "For each group entered, assign a task to every member of that group" in the workflow.
    Best Regards,
    Lisa Chen

  • Strange measurement input fields behaviour

    Hello,
    I have a Czech version of Illustrator CC on Windows 7 Pro.
    Some measurement input fields are not working as expected, for example:
    When creating a new document:
    Decimal marks in document dimensions have disappeared (instead 210.00 mm it shows 210 00 mm). It also disappears after I add it. Another strange thing is when I click in the input box, do nothing and click out the value changes to 5779 55 mm
    (Something similar is happening in my InDesign: I can't change an object's dimension expressed with a number with a decimal mark to another number with a decimal mark - only to an integer. I'm not working in Web mode.)
    Is this behaviour controlled by some settings I can't find or is this a bug?
    Every note leading to a solution much appreciated!

    maara,
    The decimal point is basically governed by your Regional/Language settings, in the Control Panel I believe.
    There may be some issues between . and , for decimal point (and reversely for thousands).
    But, especially if the behaviour has not always been there, you may try the list below.
    In any case you may try a chat or a support call, here or here,
    Creative Cloud support (all Creative Cloud customer service issues, chat open between 5AM and 7PM PST/PDT on workdays)
    http://helpx.adobe.com/x-productkb/global/service-ccm.html
    Adobe Support (phone),
    http://helpx.adobe.com/adobe-connect/adobe-connect-phone-numbers.html
    The following is a general list of things you may try when the issue is not in a specific file (you may have tried/done some of them already); 1) and 2) are the easy ones for temporary strangenesses, and 3) and 4) are specifically aimed at possibly corrupt preferences); 5) is a list in itself, and 6) is the last resort.
    If possible/applicable, you should save curent artwork first, of course.
    1) Close down Illy and open again;
    2) Restart the computer (you may do that up to 3 times);
    3) Close down Illy and press Ctrl+Alt+Shift/Cmd+Option+Shift during startup (easy but irreversible);
    4) Move the folder (follow the link with that name) with Illy closed (more tedious but also more thorough and reversible);
    5) Look through and try out the relevant among the Other options (follow the link with that name, Item 7) is a list of usual suspects among other applications that may disturb and confuse Illy, Item 15) applies to CC, CS6, and maybe CS5);
    Even more seriously, you may:
    6) Uninstall, run the Cleaner Tool (if you have CS3/CS4/CS5/CS6/CC), and reinstall.
    http://www.adobe.com/support/contact/cscleanertool.html

  • Anyone understand this strange LR3 side panel behaviour?

    In the last couple of days my laptop has developed a piece of strange behaviour related to the side panels.  I'm pretty sure it didn't start when I installed the LR3.5 update, but this may have been a contributing factor.
    Until recently I always worked in "solo" mode.  A couple of days ago, LR's behaviour changed so that opening or closing any of the side panels would cause the relevant side (left or right) to become totally unresponsive.  The rest of the program continued to work fine, and the equivalent side panel in other modules continued to work properly until a panel was opened or closed.
    In an attempt to understand what was going on, I turned "solo" mode off.  This led to some very interesting behaviour indeed:
    If I used the triangle to open or close a side panel, everything worked exactly as expected.
    If I clicked on the dark grey bar to open or close a side panel, it became stuck in one of the two states.
    When stuck in "open" state, clicking in the grey area simply caused the panel to close and then re-open, very rapidly.
    When stuck in "closed" state, something similar happened in that the panel opened and then re-closed, very rapidly.
    When in "stuck" state, clicking on the triangle had no effect on the displayed panel but it seemed to toggle the internal state between opened and closed.
    Once toggled, clicking on the grey bar opened the panel where it used to close it, and vice versa.
    When collapsed, the panel displays as pale grey rather than the more usual dark grey.  See below for an illustration.
    I have a desktop machine as well which doesn't show the behaviour.  I've tried reinstalling LR3.5, to no avail.  My guess is that we're dealing with inconsistent panel state information in the "preferences" file, but I don't know for sure.
    If anyone knows what is happening, or (more importantly) how to stop it happening, I'd be enormously grateful for some help. 
    Many thanks in advance,
    Ian Wilson
    Cambridge UK

    Further investigation yielded yet more interesting behaviour - and, it turns out, a pointer to the problem.
    I tried renaming the old "preferences" file, causing LR to create a new one.  The problem was unchanged, apart from losing my registration information... 
    I tried creating a new, virgin catalogue.  This behaved normally, so I thought I was onto something!
    I tried importing a folder of pictures into the new catalogue.  The old behaviour returned.
    I created a new virgin catalogue, which reverted to good behaviour.  I then imported one picture into the catalogue.  Behaviour BAD.
    I then tried removing the single picture from the catalogue and restarted LR.  Behaviour GOOD!
    Conclusion:  Lightroom works just fine for me providing I never put any pictures into my catalogue!! 
    This got me thinking that, perhaps, the issue was related to the rendering of images.  I tried installing the latest nVidia drivers for my laptop, but this had no effect whatsoever.  Given the issue clearly related to how images are rendered, however, I wondered whether the problem could relate to colour profiles.  BINGO!!  I'd recently upgraded my "i1xtreme" to X-Rite's new "i1publish".  The profiles created with the latest software are ".icm" rather than ".icc", although this shouldn't matter (and doesn't matter to PS and Bridge).  It seems to matter to LR, however, as reverting to my earlier ".icc" profiles made everything work again just fine. 
    There is obviously a bug somewhere, but it's not clear whether I should be chasing Adobe or X-Rite.  I'd welcome any comments or advice...
    Many thanks for the suggestions,
    Ian.

  • Post increment operator i++

    I can't seem to figure out why the following is outputting 2,2,3 and not 2,3,4. Having searched the Java Language Spec and reading ... (http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#39438)
    ... it is still not clear. The key line probably is "The value of the postfix increment expression is the value of the variable before the new value is stored." I still think that the second output should have been 3 and not 2. Could someone explain this to me? Thanks.
    Here is the code that I tried:
    int i;
    i = 2;
    System.out.println(i);
    i = i++; // the line which I don't understand
    System.out.println(i);
    i++;
    System.out.println(i);

    The key is to understand the difference between the
    value of the expression and the value of the
    variable.
    i = i++;What that particular bit of devil-spawn does is
    this:
    1) Get the current value of i. The value of the
    postfix increment expression is the value of the
    variable before the new value is stored. So we
    have to get the "before it's incremented" value. This
    is the value of the expression i++.
    2) Increment i. This doesn't alter the value of the
    expression that was obtained in step 1.
    3) Take the value of the expression that was
    retrieved in step 1 (the value of i before being
    incremented) and stuff it into i.
    If you had int i = 2;
    int j;
    j = i++; Would you expect j to be 2 or 3? It will be 2. And
    the same reason that j is 2 here makes i 2 if you put
    i on the left of the equals.When the expression is "i = i + 1" compiled, it should be divided into two sub expressions :
    1.) i = i; //As postfix defination says use value first and then increment
    2)i = i + 1//Increment i;
    so, accordingly, the value of i after first step should be 0 and then after second step it is 1(C,C++ behaviour).
    so, the statement i = i++ must result into the value 1 for i, not 0.
    even in C#.net the value of i is 0 after the execution of above statement.

  • Strange Custom Template Sections Behaviour

    I've created some custom template sections so I can insert my own various page types for a document I'm producing. The strange thing is Pages will insert an extra blank page (similar to the master but not exactly the same it's missing a text box) between sections when I add a section—but only some of the time!
    It will not do it when I initially add the section.
    It will only add in a 2nd page between when I add additional sections, be they the same type of section or another type of section.
    I cannot delete this 2nd page without deleting the 1st page of the section as well.
    When I try to capture just the 1st page to make another version of the custom template it still exhibits the above behaviours, inserting this "buffer" 2nd page in between sections.
    All the other custom sections I have created do not exhibit this behaviour.
    I've turned on invisibles and can't see anything that makes this section special or different.
    Any guesses as to why it inserts this extra page in between sections to make a 2 page section instead of staying as a 1 page section?
    Message was edited by: Nathan Muirhead

    Nathan
    You have some text being forced over to the next page, either because you hammered away at the spacebar/return key/tabs, or have some object wrap pushing returns over, or both:
    +Menu > View > Show Invisibles/ Show Layout+
    Once you have cleaned up your pages recapture them as sections and resave your template over the old one.
    Peter

  • Strange 6i's Compiler Behaviour

    Hi all,
    I am experiencing a strange behaviour on Form Builder (patch 5). Sometime when I fix/change something and recompile (using run button or file-admin-compile file), other part of the application (ussually report) will be broken.
    For example, before I change anything, it could run the report just fine (using run_product built in). Then I change something (NOT the run_product) like the name of the button and recompile. The next time I try to run the report, it sometime display the error message or display blank report.
    To fix it, I have to open the trigger that contain the run_product built in to see the PL/SQL code, press the compile button and then recompile the whole form again.
    Anybody else experiencing the same problem?
    Regards,
    Ari

    Hi,
    I got the same problem but not in run_report built-in, in the key-next-item.
    Sometimes when I'm navigate trough my items, my application close. I do same thing that you do, I recompile the trigger and the problem appears in another item.
    We didn't find a solution yet.
    bye Roxane
    null

Maybe you are looking for