Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Lars Enderin Newsgroups: comp.lang.java.programmer Subject: Re: A freshman's question Date: Wed, 19 Oct 2011 17:48:31 +0200 Organization: albasani.net Lines: 77 Message-ID: <4E9EF14F.1040904@telia.com> References: <3242b80f-ee51-45f0-9e12-231482f61a97@r2g2000prh.googlegroups.com> <37KdnWZAYfIR1QPTnZ2dnUVZ_vWdnZ2d@earthlink.com> <02a66b06-651a-46c8-9a09-64442a73a959@s17g2000prm.googlegroups.com> <4df1e05a-295c-4c60-9baf-0e69cd4ee507@r2g2000prh.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=GB2312 Content-Transfer-Encoding: 8bit X-Trace: news.albasani.net J8m0GRtrD2P3EpeVF/jhYSvg+8g+zBDmUDraDMdht1W99P1TgD3wdCGipo9bj3MTWCfV/BnJjMSyDRNd2avG53U6eDHNh9zS7GZjl7taRIqa5kfVSjBjl1yjDwfy3ycy NNTP-Posting-Date: Wed, 19 Oct 2011 15:48:35 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="lS/bnUFsA8NN5e7y/xcETwElTrh+jmSMJ+6zyQUuUUW3xcWV3bzZAuyoo4KjtG00AVAAGzHVPrnuGMudYZ9A4Ho+zCNUHvyUySvn0NtmZHEHySc8J6rsdvJXJUgqbMAt"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110922 Thunderbird/3.1.15 In-Reply-To: <4df1e05a-295c-4c60-9baf-0e69cd4ee507@r2g2000prh.googlegroups.com> Cancel-Lock: sha1:Hs/eoqEre7An3dDlmOhkC6TaRU8= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8999 2011-10-19 12:38, whl skrev: > On Oct 19, 4:20 pm, Patricia Shanahan wrote: >> whl wrote: >>> On 10月19日, 上午11时48分, Patricia Shanahan wrote: >>>> whl wrote: >>>>> Dear all >>>>> I am a freshman ,I from china, I have been learn java for >>>>> mouths,my English isn't very well ,may my question expression is not >>>>> very clear,but I hope I can learn with you together! thank you ! >>>>> . when I do some test ,I found the question ,It is the code >>>>> ======start========= >>>>> public class Inc{ >>>>> public static void main(String argv[]){ >>>>> Inc inc = new Inc(); >>>>> int i =0; >>>>> inc.fermin(i); >>>>> i = i++; >>>>> System.out.println(i); >>>>> } >>>>> void fermin(int i){ >>>>> i++; >>>>> } >>>>> } >>>>> ========end======== >>>>> I think the result is 1,but the real result is 0. I don't kown the >>>>> statement i=i++ operation sequence. In my opinion , variable i's >>>>> values is 0,then i++ ,the variable i's values is 1. They share a >>>>> common memory space,the variable i should change the values. >>>> The i=i++ operation sequence is: >> >>>> 1. Evaluate i++. It has the side effect of incrementing i to 1, but has >>>> as result the old value of i, 0. >> >>>> 2. Do the assignment. This sets the left hand side, i, equal to the >>>> result of the right hand side, 0. >> >>>> In theory, i does change to 1, but immediately changes back to 0. In >>>> practice, the change in i's value might get optimized out. The effect of >>>> i=i++ is to leave i unchanged. >> >>>> Patricia >> >>> so ,thank you for you answer my question ,I just don't know the >>> variable i is share the common memory space and when the left hand >>> side ,i ,equal to the result of the right hand side ,0,then ,i >>> increase to 1,so ,in the memory ,the variable i's value should be 1.if >>> change the expression i=i++ to i++,the result is 1. >> >> Look again at what I wrote. >> >> During step 1, at least in theory, i changes from 0 to 1. If the entire >> statement is "i++;" that is the value of i for later statements. The 0 >> result of evaluating i++ is not used. >> >> In the original case, step 2 makes i equal to the value of the right >> hand side, 0, and that is the value of i for later statements. >> >> Patricia > > Thank you for your explanation in patience, maybe my English is very > terrible,I don't understand all of your meaning. your meaning is when > the variable i on the right hand side is assigned 0,then ,the > statement "i++" don't execute,and start print? 1) i = 0; inc.fermin(i); The method fermin() is useless. It has no effect. The parameter is not the same i, just a copy of the actual value. The statement i++; in the method has no effect outside the method. So i remains == 0. 2) i = i++; This is an assignment. First, the expression i++ is evaluated. Its value is 0, the initial value of i. This 0 is then assigned to i, which thus remains == 0. You really have to go back to basics.