Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #8981

Re: A freshman's question

From Arved Sandstrom <asandstrom3minus1@eastlink.ca>
Newsgroups comp.lang.java.programmer
Subject Re: A freshman's question
References <3242b80f-ee51-45f0-9e12-231482f61a97@r2g2000prh.googlegroups.com> <37KdnWZAYfIR1QPTnZ2dnUVZ_vWdnZ2d@earthlink.com> <02a66b06-651a-46c8-9a09-64442a73a959@s17g2000prm.googlegroups.com>
Message-ID <Vpwnq.6806$El.6414@newsfe19.iad> (permalink)
Organization Public Usenet Newsgroup Access
Date 2011-10-19 06:05 -0300

Show all headers | View raw


On 11-10-19 04:54 AM, whl wrote:
> On 10月19日, 上午11时48分, Patricia Shanahan <p...@acm.org> 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.
> 
> whl

WHL, try a little experiment. Leave ++ (post or pre) aside for a moment.
Write another method that accepts an int parameter. This method also
modifies the value (maybe just by adding 42 to it, or setting it to 0).

Call the method, passing in an int variable set to a particular value.
Then print out the value of the variable that you passed in as an argument.

Did you expect the value of that variable to change? Why?

AHS

-- 
I tend to watch a little TV... Court TV, once in a while. Some of the
cases I get interested in.
-- O. J. Simpson

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-18 20:08 -0700
  Re: A freshman's question Patricia Shanahan <pats@acm.org> - 2011-10-19 04:48 +0100
    Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-19 00:54 -0700
      Re: A freshman's question Patricia Shanahan <pats@acm.org> - 2011-10-19 09:20 +0100
        Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-19 03:38 -0700
          Re: A freshman's question Lars Enderin <lars.enderin@telia.com> - 2011-10-19 17:48 +0200
            Re: A freshman's question Tim Slattery <Slattery_T@bls.gov> - 2011-10-19 12:24 -0400
              Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-20 03:55 -0700
            Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-20 03:50 -0700
          Re: A freshman's question Lew <lewbloch@gmail.com> - 2011-10-19 09:58 -0700
            Re: A freshman's question Patricia Shanahan <pats@acm.org> - 2011-10-19 18:47 +0100
            Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-20 03:48 -0700
        Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-19 03:49 -0700
          Re: A freshman's question Patricia Shanahan <pats@acm.org> - 2011-10-19 18:58 +0100
            Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-20 02:13 -0700
      Re: A freshman's question Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-10-19 06:05 -0300
        Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-19 03:38 -0700
    Re: A freshman's question Travers Naran <tnaran@gmail.com> - 2011-10-20 07:30 -0700
      Re: A freshman's question Gene Wirchenko <genew@ocis.net> - 2011-10-20 10:08 -0700
        Re: A freshman's question Patricia Shanahan <pats@acm.org> - 2011-10-20 18:22 +0100
        Re: A freshman's question Lew <lewbloch@gmail.com> - 2011-10-20 13:59 -0700
        Re: A freshman's question Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-10-20 17:38 -0500
  Re: A freshman's question Roedy Green <see_website@mindprod.com.invalid> - 2011-10-19 23:56 -0700
    Re: A freshman's question whl <wanghailunmail@gmail.com> - 2011-10-20 03:55 -0700

csiph-web