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 );
}

Similar Messages

  • 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.

  • 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

  • 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

  • Difference between Pre and Post Incrementing a variable

    I have a problem understanding the nuances between i++ and ++i.
    I have run several loops and changed the position of the "++". I noticed that sometimes the out put differs and sometimes the output remains the same.
    Is there a rule of thumb as to whether a variable show be post or pre incremented?
    Thanks

    Thank you all.
    After I asked the question, I did a little research and found this (from JGuru shortcourse):
    Note that x++ and ++x are equivalent in standalone contexts where the only task is to increment a variable by one,that is, contexts where the ultimate evaluation is ignored:
    int x = 4;
    x++; // same effect as ++x
    System.out.println("x = " + x);
    This code produces the output:
    x = 5;
    The next part after this is:
    In the following context, the placement of the increment operator is important:
    int x = 4;
    int y = x++;
    int z = ++x;
    System.out.println(
    "x = " + x + " y = " + y + " z = " + z);
    This code produces the output:
    x = 6 y = 4 z = 6
    ======================================================================
    I thought the out put would be x = 4, y = 6, z = 4.
    I totally lost! How do I interpet this correctly?

  • Row-Based Only option for post processing operator

    Hi,
    Have a question on the role of the "Row-base Only" option for post processing operators. I am trying to understand how a post-processing operation can be done in a row-based mode. A coded PLSQL procedure in a post processing operator is expected to run only once. So I am not clear on how this option might affect the execution of the transformation. Any clarifications would be appreciated.
    Thanks,
    Mazen

    Hi Carsten,
    My question is more on what is the use of the "Row-based only" checkbox that shows up in the properties window for a post-processing operator. How does this checkbox affect the execution of the post-processing transformation?
    Regards,
    Mazen

  • How do I send a private message to someone who has posted a previous question or answer?

    I have a similar question to one which was posted earlier and I would like to contact the person who asked this question, to see if the advice received helped sort out his problem

    Molly K wrote:
    AdrianB,
    Unfortunately, our current system doesn't have private messaging functionality.
    If the thread was recent, the person you are looking for may still be subscribed to it. If they are, then you can post to the thread and a notification will be sent to them and they may respond to you.
    If the thread wasn't recent, then the notifications to it have probably all expired. The best thing to do in this case would be to post a new question and reference the old thread in your question. The original person may reply to it or other users may reply and let you know their opinion of the advice given.
    Thanks,
    Molly K.
    NI Web Support & Operations
    Hi Molly,
    What qualifies as "recent"? In other words, how long until subscriptions have expired?
    Thanks,
    Corey

  • The DATA CAP MEGA THREAD....post data cap questions or comments here.

    In the interest of keeping things orderly....This is the DATA CAP MEGA THREAD....post data cap questions or comments here.
    Please keep it civil.
    Comcast is testing usage plans (AKA "data caps") in certain markets.
    The markets that are currently testing usage plans are:
    Nashville, Tennessee market: 300 GB per month and additional gigabytes in increments/blocks ( e.g., $10.00 per 50 GB ). 
    Tucson, Arizona market: Economy Plus through Performance tiers receive 300 GB. Those customers subscribed to the Blast! Internet tier receive 350 GB; Extreme 50 customers receive 450 GB; Extreme 105 customers receive 600 GB. Additional gigabytes in increments/blocks of 50 GB for $10.00 each in the event the customer exceeds their included data amount. 
    Huntsville and Mobile, Alabama; Atlanta, Augusta and Savannah, Georgia; Central Kentucky; Maine; Jackson, Mississippi; Knoxville and Memphis, Tennessee and Charleston, South Carolina: 300 GB per month and additional gigabytes in increments/blocks ( e.g., $10.00 per 50 GB ) Economy Plus customers have the option of enrolling in the Flexible-Data plan.
    Fresno, California, Economy Plus customers also have the option of enrolling in the Flexible-Data plan.
    - If you live outside of these markets you ARE NOT currently subject to a data plan.
    - Comcast DOES NOT THROTTLE your speed if you exceed your usage limits.
    - You can check out the Data Usage Plan FAQ for more information.
     

    I just got a call today that I reached my 300GB limit for the month.  I called and got a pretty rude response from the security and data usage department.  The guy told me in so many words that if I do not like or agree with the policy that I should feel free to find another service provider.!!! I tried to explain that we watch Netflix and XFinity on-demand alot and I was told that that can not be anywhere close to the data usage. I checked my router and watching a "super HD, dolby 5.1" TV show on Netflix will average about 5-6 GB per hour (1.6MB/s) ... sp this means that I can only watch no more than 1-2 Super HD TV shows a day via Netflix before I run out of my data usage.    This seems a bit redicilous doesn't it? Maybe the TV ads about the higher speed than the competition should be accompanied with "as long as you don't use it too often"   Not a good experience ... 

  • J-CONTENTS & INCREMENT OPERATOR

    I have 2-questions:-
    1. What are the contents of Core Java & Adv. Java?
    Is swing part of Core Java OR Adv. Java?
    2. Let int a=1;
    after execution of statement a=a+++a+a++; value of a will be 5
    after execution of statement a=++a+a+a++; vaue of a will be 6
    after execution of statement a=a+a+++a++; value of a will be 4
    Please Explaine me the logic behind it.
    Thanks & Regards,
    GAJESH TRIPATHI
    09252708020

    gajesh wrote:
    I have 2-questions:-
    1. What are the contents of Core Java & Adv. Java?
    Is swing part of Core Java OR Adv. Java?Huh? There is no official "core" vs. "advanced" (which is what I assume you mean by "adv"). Swing is part of the core API.
    2. Let int a=1;
    after execution of statement a=a+++a+a++; value of a will be 5
    after execution of statement a=++a+a+a++; vaue of a will be 6
    after execution of statement a=a+a+++a++; value of a will be 4
    Please Explaine me the logic behind it.No. That's ridiculous code. You'll never see that for real.
    Instead just learn about the difference between pre-increment (++x) and post-increment (x++).
    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op1.html

  • "Row-based Only" checkbox for a post processing operating

    Hi,
    Is there documentation available on the "Row-based Only" checkbox for the post-processing operator? Any links to more information on this would be appreciated.
    Regards,
    Mazen

    Hi Carsten,
    My question is more on what is the use of the "Row-based only" checkbox that shows up in the properties window for a post-processing operator. How does this checkbox affect the execution of the post-processing transformation?
    Regards,
    Mazen

  • If you need help quickly... Guideline for posting an effective question.

    Whenever you have a question that you would like answered, do pose that question effectively.
    First, do not assume that because you have tried many, many times that your question is understood by someone else. What is obvious to you may not be for another. So please state in a very detailed and structured way what steps you have taken to cause your problem, how you can replicate this problem, under what circumstances on what hardware/software.
    The fact that you have nearly given up because of your problem does not mean that someone else can readily understand this problem. When you go to your doctor and say: "I have a pain in my stomach" he will ask you a lot of questions. Same applies here. Your doctor may need to know about any medication you have taken, your blood pressure, history, etc. If you want help, be as PRECISE and ACCURATE as possible.
    Second, this is a USER TO USER forum. So ranting will not help. What may help is a STRUCTURED approach to describing the problem you have.
    Third, do a thorough search of posts here, on other fora and with Google before repeating what has been asked a thousand times and has worn out the patience of regulars here, who have answered your question a dozen times already. Use the FAQ and the Wiki to find answers to questions that have been posed and answered repeatedly. Do not assume you are unique with your problem. Well, you are unique, the problem you have most likely is not.
    Fourth, realize that even though it may be highly frustrating to you to not be able to solve YOUR problem, by posting your question here, you are dependent on fellow editors to help you on their free time without compensation, so bear with them to help identify what EXACTLY is your problem. If you lack that decency, you may forfeit their help, so why did you come here?
    Fifth, consider your work flow in light of the purpose of Premiere, an editing package. If you have problems with rotoscoping, programming with APL, spreadsheets in general, word processing on a mobile phone, importing Panda bears into a local zoo, feeding nearly distinct whales in the Alps, or anything far removed from the purpose of PP, you may be better off in posting your question elsewhere.
    Last:
    Everybody has to learn, has the right to ask questions and be helped.
    If everybody has the decency to pose their urgent, life threatening or other questions in a detailed, structured and comprehensible way, it will vastly improve the chances of speedy and helpful replies, without the need for sarcasm, bundles of to-and-fro posts without meaningful help, just wasting bandwidth.
    I know I have been rightly accused of such behavior, which was the result of sheer frustration with absolutely meaningless and often pointless questions without details, structure and the like. Maybe this can be a general guideline for posters on how to ask questions, in addition to Eddie's links and How to ask questions the smart way
    The most frustrating issues one can encounter are hangs, BSOD's (blue screens of death), system errors and other cases of seemingly random erros that require rebooting, restarting or similar actions, only to be confronted with the next hang, BSOD or system crash.
    These errors are often difficult to locate, due to the large variety of systems, components and driver or software versions in use. This is often further aggravated by sheer lack of expertise, understanding of what needs to be communicated in order for others to help solve the issue at hand. And that list is very long.
    So, in order to make it a bit simpler for the average user who encounters these problems, follow the following steps when posting a problem:
    1. Clearly describe the main problem in the topic, but be brief. Not so brief as to only say: "Help!" or "URGENT" or another meaningless statement but get to the point, like: "PP crashes on opening, requiring a reboot" or "Using DL the system hangs upon return to PP". You get the point.
    2. Briefly describe your hardware, software, OS and versions in use, including disk setup, video card and driver versions, etc.
    3. Describe source material, including codec (not AVI, which is not a codec), project settings and other relevant info.
    4. Describe as accurately as possible what you did, what steps you took, what happened at what moment and what you did to try to resolve it.
    5. Go to Start/Run, type in DXDIAG, enter and save all information to a TXT file. Include a link to that file in your post.
    6. Go to Start/Run, type in MSINFO32 (or similar for 64 bit OS), enter and save the results as a .NFO file. Include a link to that file in your post.
    7. Download Process Explorer , and either post a screen shot or a link to the results.
    8. Make a screen shot of the results of SpeedFan or HW Monitor and include that in your post.
    When you perform these basic steps, you avoid endless questions about your system and people may be able to help you faster.
    If you feel up to it, take a look at The Case of the Unexplained to start your own troubleshooting.
    Hope this helps to post more meaningful questions and get an answer much quicker.

    Whenever you have a question that you would like answered, do pose that question effectively.
    First, do not assume that because you have tried many, many times that your question is understood by someone else. What is obvious to you may not be for another. So please state in a very detailed and structured way what steps you have taken to cause your problem, how you can replicate this problem, under what circumstances on what hardware/software.
    The fact that you have nearly given up because of your problem does not mean that someone else can readily understand this problem. When you go to your doctor and say: "I have a pain in my stomach" he will ask you a lot of questions. Same applies here. Your doctor may need to know about any medication you have taken, your blood pressure, history, etc. If you want help, be as PRECISE and ACCURATE as possible.
    Second, this is a USER TO USER forum. So ranting will not help. What may help is a STRUCTURED approach to describing the problem you have.
    Third, do a thorough search of posts here, on other fora and with Google before repeating what has been asked a thousand times and has worn out the patience of regulars here, who have answered your question a dozen times already. Use the FAQ and the Wiki to find answers to questions that have been posed and answered repeatedly. Do not assume you are unique with your problem. Well, you are unique, the problem you have most likely is not.
    Fourth, realize that even though it may be highly frustrating to you to not be able to solve YOUR problem, by posting your question here, you are dependent on fellow editors to help you on their free time without compensation, so bear with them to help identify what EXACTLY is your problem. If you lack that decency, you may forfeit their help, so why did you come here?
    Fifth, consider your work flow in light of the purpose of Premiere, an editing package. If you have problems with rotoscoping, programming with APL, spreadsheets in general, word processing on a mobile phone, importing Panda bears into a local zoo, feeding nearly distinct whales in the Alps, or anything far removed from the purpose of PP, you may be better off in posting your question elsewhere.
    Last:
    Everybody has to learn, has the right to ask questions and be helped.
    If everybody has the decency to pose their urgent, life threatening or other questions in a detailed, structured and comprehensible way, it will vastly improve the chances of speedy and helpful replies, without the need for sarcasm, bundles of to-and-fro posts without meaningful help, just wasting bandwidth.
    I know I have been rightly accused of such behavior, which was the result of sheer frustration with absolutely meaningless and often pointless questions without details, structure and the like. Maybe this can be a general guideline for posters on how to ask questions, in addition to Eddie's links and How to ask questions the smart way
    The most frustrating issues one can encounter are hangs, BSOD's (blue screens of death), system errors and other cases of seemingly random erros that require rebooting, restarting or similar actions, only to be confronted with the next hang, BSOD or system crash.
    These errors are often difficult to locate, due to the large variety of systems, components and driver or software versions in use. This is often further aggravated by sheer lack of expertise, understanding of what needs to be communicated in order for others to help solve the issue at hand. And that list is very long.
    So, in order to make it a bit simpler for the average user who encounters these problems, follow the following steps when posting a problem:
    1. Clearly describe the main problem in the topic, but be brief. Not so brief as to only say: "Help!" or "URGENT" or another meaningless statement but get to the point, like: "PP crashes on opening, requiring a reboot" or "Using DL the system hangs upon return to PP". You get the point.
    2. Briefly describe your hardware, software, OS and versions in use, including disk setup, video card and driver versions, etc.
    3. Describe source material, including codec (not AVI, which is not a codec), project settings and other relevant info.
    4. Describe as accurately as possible what you did, what steps you took, what happened at what moment and what you did to try to resolve it.
    5. Go to Start/Run, type in DXDIAG, enter and save all information to a TXT file. Include a link to that file in your post.
    6. Go to Start/Run, type in MSINFO32 (or similar for 64 bit OS), enter and save the results as a .NFO file. Include a link to that file in your post.
    7. Download Process Explorer , and either post a screen shot or a link to the results.
    8. Make a screen shot of the results of SpeedFan or HW Monitor and include that in your post.
    When you perform these basic steps, you avoid endless questions about your system and people may be able to help you faster.
    If you feel up to it, take a look at The Case of the Unexplained to start your own troubleshooting.
    Hope this helps to post more meaningful questions and get an answer much quicker.

  • In which Board we need to post cRIO related Questions?

    sir,
         In which board do i need to post cRIO related Questions and doubts regarding my application with cRIO .Please help me previously i have posted in  motion control drives as my application is related to motor control using cRIO.bu i am not getting any reply .i couldnt find any cRIO related board in select board option.
    Solved!
    Go to Solution.

    Hi illa,
    You can post your query under the Real Time Measurement and Control
    http://forums.ni.com/t5/Real-Time-Measurement-and/bd-p/280

  • New number of posts bug(?) + question; no #100, and total # of posts vary

    New number of posts bug(?) + question; no number 100, and total number of posts vary
    Intrigued by Tuttle's new attempt to join Niel's old habit to keep his own total number of posts count, I went and start counting my posts since the New Discussions. Because I saved my latest Old Discussions Post-and-Points info, I can see what are the previously-still-online ones (the pre- 14-Nov-2005 01:31 GMT ones), and I know exactly what was my "real" total number of posts before 14-Nov-2005 (2446).
    My New Discussions posts number however, is quite uncertain.
    Firstly, when I use the "My Posts" link on the right, then clicking on and on on the "Next" link,
    my "Viewing 91-100 of 586 -- Pages" shows 91-99 instead of 91-100
    (= should show 10 posts, shows 9 posts)
    then I go on clicking Next Next Next, and I see the same missing counts happen elsewhere:
    my posts number 100, 259, 260, 370, 390, 520, 558, 559, 560, 578, 579, and 580 are missing too.
    Note that I wouldn't bother doing all this, and I would have thought it's just very old posts that are automatically deleted like in the Old Discussions,
    BUT,
    the problem is that when I choose to see all my posts together on the same page
    (by using the start=0&range=600 suffix in the URL)
    I get a total number of posts that is different (570).
    A third way to quickly see all posts without having to load too big page
    (using the start=0&range=300 suffix in the URL then only one "Next" click)
    shows yet another, different again, number of total posts (573).
    And my "official" total number of posts is (586).
    Dear HOSTS,
    - If this can be used as a bug report to fix a counting problem, glad to help.
    - If there is no bug, I have two(+) questions then:
    1) Was my post number 100 deleted?
    (should be around 27-Nov-2005 18:00 GMT)
    Also:
    I thought the Total Posts number that we see in "My Public Profile" or under the avatar in each post (586), was only the number of posts still online.
    - if it is the case, then there are no automatically deleted old posts anymore?
    - if old posts are automatically deleted like before, this number should vary?
    - if it doesn't vary (it just grows by one per new post), AND at the same time old posts are actually automatically deleted,
    it has to show the "real" total number of posts then,
    (as it is now, a total starting from the Old-Discussions-still-online-posts, it makes no sense),
    that is,
    the total posts in the New Discussions PLUS the total posts since first registration in the Old Discussions.
    So,
    2) What does it show exactly?
    Thanks again for all the great work!
    Axl
    220? 221?

    Thank you Andreas,
    Your confirmation about the "post #100 bug" does help.
    "As for your other missing posts - I guess Tuttle's assessment that they might be deleted is probably right..."
    Yes, that makes sense and that's what I'm trying to figure in my OP, but then,
    after I posted my "#221/2667 post" (11-Dec-2005 21:51 GMT), my originally missing #
    259, 260, 370, 390, 520, 558, 559, 560, 578, 579, and 580
    should have become
    260, 261, 371, 391, 521, 559, 560, 561, 579, 580, and 581
    right? (forget the #100: this one we know for sure it's a bug)
    Or if the software counts them from the first (now that would have been a good start) instead of starting from the most recent,
    they should become
    258, 259, 369, 389, 519, 557, 558, 559, 577, 578, and 579
    Well, that's not what happened. Now the missing # are
    259, 260, 370, 390, 400, 520, 559, 560, 570, 579, and 580
    (in addition to the same #100) (#558 and 578 not missing anymore).
    As you can see, there is no logic:
    It doesn't work as a simple numerotation of StillOnlinePosts, no matter the order
    (otherwise there would simply be a straight count from 1 to 575 (=587-12).
    It looks indeed like it shows which old posts are being automatically deleted. That the numbers are quite the same would simply show that the old posts dates were so close, of course they are deleted (six months later) almost at the same time,
    BUT,
    if it was the case, there would never be LESS deleted posts between two StillOnlinePosts, like it happens for example between my StillOnlinePosts #557 and #561.
    Please note: of course I stick to the same method to make all my counts. Not manually modifying the URL, just using the default Next-Next-Next- clicking...
    Modifying URL would add yet more differences in the differences!..
    Dear Hosts,
    Too bad it doesn't help looking yourself at my "My Posts", as everything changes each time I'm adding a reply somewhere.
    Perhaps a good way to troubleshoot this would be to start from a very simple basis:
    --> "name" the posts with a number, starting from the oldest.
    This way, for example my "#1" post (11-Dec-2005 21:51 GMT in this thread) would be named "221" in the New Discussions, or "2667" if you start from my Registration date.
    At the moment, it is very difficult to figure what fault comes from the "#100 post bug" and what fault comes from another possible counting bug...
    Then the New Discussions "fluctuating" refreshing delay too, possibly messes the counts...
    Thanks!
    Axl
    223/2669

  • Can we send pre-test score (Pre-test question slide) and post test score (Question slide) both in lms from captivate?

    can we send pre-test score (Pre-test question slide) and post test score (Question slide) both in lms from captivate?

    Pretest score normally is not sent to the LMS. However it is stored in a system variable, you could use JS to send it.

  • 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!!

Maybe you are looking for