A freshman's question

Discussion in 'Java' started by whl, Oct 19, 2011.

  1. whl

    whl Guest

    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.

    I need your help!
     
    whl, Oct 19, 2011
    #1
    1. Advertising

  2. whl

    whl Guest

    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.

    whl
     
    whl, Oct 19, 2011
    #2
    1. Advertising

  3. On 11-10-19 04:54 AM, 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.
    >
    > 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
     
    Arved Sandstrom, Oct 19, 2011
    #3
  4. whl

    whl Guest

    On Oct 19, 5:05 pm, Arved Sandstrom <>
    wrote:
    > On 11-10-19 04:54 AM, 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.

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


    thank you ,I will try more experiment.
     
    whl, Oct 19, 2011
    #4
  5. whl

    whl Guest

    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?
     
    whl, Oct 19, 2011
    #5
  6. whl

    whl Guest

    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 explain in patience,maybe my English is very
    terrible,I don't understand all of your meaning . your meaning is when
    the variable i is assigned ,the value of i on the left hand side is
    0,then don't execute the i++ , execute the next statement and print
    the value?
     
    whl, Oct 19, 2011
    #6
  7. whl

    Lars Enderin Guest

    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.
     
    Lars Enderin, Oct 19, 2011
    #7
  8. whl

    Tim Slattery Guest

    Lars Enderin <> wrote:


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


    No question.

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


    Lessee....I think this is the sequence:

    1. evaluate i, that yields 0
    2. increment i, so that i is 1 for a second.
    3. Now assign the value you got from evaluating i in step 1 to i. So i
    is reset to 0.

    Does that sound right?

    --
    Tim Slattery

    http://members.cox.net/slatteryt
     
    Tim Slattery, Oct 19, 2011
    #8
  9. whl

    Lew Guest

    whl wrote:
    > Patricia Shanahan wrote:
    >> whl wrote:
    >>> Patricia Shanahan wrote:
    >> >> whl wrote:
    >>>>> ======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.
    >>>
    >>> 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.

    >
    > 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?


    No.

    The "i++" *does* execute. ("print" has nothing to do with this yet.)

    What is the value of 'i++'?

    Let's just look at two lines of code.

    int i = 0;
    int x = i++;

    What is the value of 'x' after its initialization?

    Zero!

    Why?

    Because the value of a post-increment expression - that means the '++' is to the right of the variable - is the value of the variable before the increment.

    This is very, very basic.

    The value of post-increment (and of post-decrement) is the variable's value *before* the operation.

    Before.

    Not after.

    The pre-increment version, where the operator is to the left of the variable, is the value after the increment.

    So in this snippet:

    int i = 0, j = 0;
    int x = i++;
    int y = ++j;

    the value of 'x' will become zero, and the value of 'y' will become one.

    --
    Lew
     
    Lew, Oct 19, 2011
    #9
  10. whl

    Roedy Green Guest

    On Tue, 18 Oct 2011 20:08:05 -0700 (PDT), whl
    <> wrote, quoted or indirectly quoted someone
    who said :

    > I don't kown the
    >statement i=i++ operation sequence.


    This sort of code is not found in real code. It pushes the edges of
    the definition of the language.

    You would see
    i++
    or
    i+=2
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    It should not be considered an error when the user starts something
    already started or stops something already stopped. This applies
    to browsers, services, editors... It is inexcusable to
    punish the user by requiring some elaborate sequence to atone,
    e.g. open the task editor, find and kill some processes.
     
    Roedy Green, Oct 20, 2011
    #10
  11. whl

    whl Guest

    On 10ÔÂ20ÈÕ, ÉÏÎç1ʱ58·Ö, Patricia Shanahan <> wrote:
    > whl wrote:
    > > 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 explain in patience,maybe my English is very
    > > terrible,I don't understand all of your meaning . your meaning is when
    > > the variable i is assigned ,the value of i on the left hand side is
    > > 0,then don't execute the i++ , execute the next statement and print
    > > the value?

    >
    > No, the i++ does get executed, but so does the "=".
    >
    > The whole i=i++; statement is more or less equivalent to:
    >
    > // Evaluate i++
    > temp = i;
    > i = i + 1;
    >
    > // Assign the value of i++ to i
    > i = temp;
    >
    > Patricia



    Thank you very much ,through this example I understand!
    whl
     
    whl, Oct 20, 2011
    #11
  12. whl

    whl Guest

    On 10ÔÂ20ÈÕ, ÉÏÎç12ʱ58·Ö, Lew <>wrote:
    > whl wrote:
    > > Patricia Shanahan wrote:
    > >> whl wrote:
    > >>> Patricia Shanahan wrote:
    > >> >> whl wrote:
    > >>>>> ======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.

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

    >
    > > 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?

    >
    > No.
    >
    > The "i++" *does* execute. ("print" has nothing to do with this yet.)
    >
    > What is the value of 'i++'?
    >
    > Let's just look at two lines of code.
    >
    > int i = 0;
    > int x = i++;
    >
    > What is the value of 'x' after its initialization?
    >
    > Zero!
    >
    > Why?
    >
    > Because the value of a post-increment expression - that means the '++' isto the right of the variable - is the value of the variable before the increment.
    >
    > This is very, very basic.
    >
    > The value of post-increment (and of post-decrement) is the variable's value *before* the operation.
    >
    > Before.
    >
    > Not after.
    >
    > The pre-increment version, where the operator is to the left of the variable, is the value after the increment.
    >
    > So in this snippet:
    >
    > int i = 0, j = 0;
    > int x = i++;
    > int y = ++j;
    >
    > the value of 'x' will become zero, and the value of 'y' will become one.
    >
    > --
    > Lew


    thank you ,now ,I think this is a very simple question,I understand
    all of your answer,maybe I put this complicated matters!
     
    whl, Oct 20, 2011
    #12
  13. whl

    whl Guest

    On 10ÔÂ19ÈÕ, ÏÂÎç11ʱ48·Ö, Lars Enderin <> wrote:
    > 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.



    thank you ,I have understand ,I will hard work !
     
    whl, Oct 20, 2011
    #13
  14. whl

    whl Guest

    On 10ÔÂ20ÈÕ, ÉÏÎç12ʱ24·Ö, Tim Slattery <> wrote:
    > Lars Enderin <> wrote:
    > >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.

    >
    > No question.
    >
    > >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.

    >
    > Lessee....I think this is the sequence:
    >
    > 1. evaluate i, that yields 0
    > 2. increment i, so that i is 1 for a second.
    > 3. Now assign the value you got from evaluating i in step 1 to i. So i
    > is reset to 0.
    >
    > Does that sound right?
    >
    > --
    > Tim Slattery
    > ://members.cox.net/slatteryt


    thank you ,I have understand!this is a very simple question ,maybe I
    put this complicated matters! I often make some simple questions to
    complex!
     
    whl, Oct 20, 2011
    #14
  15. whl

    whl Guest

    On 10ÔÂ20ÈÕ, ÏÂÎç2ʱ56·Ö, Roedy Green <>
    wrote:
    > On Tue, 18 Oct 2011 20:08:05 -0700 (PDT), whl
    > <> wrote, quoted or indirectly quoted someone
    > who said :
    >
    > > I don't kown the
    > >statement i=i++ operation sequence.

    >
    > This sort of code is not found in real code. It pushes the edges of
    > the definition of the language.
    >
    > You would see
    > i++
    > or
    > i+=2
    > --
    > Roedy Green Canadian Mind Productshttp://mindprod.com
    > It should not be considered an error when the user starts something
    > already started or stops something already stopped. This applies
    > to browsers, services, editors... It is inexcusable to
    > punish the user by requiring some elaborate sequence to atone,
    > e.g. open the task editor, find and kill some processes.


    thank you ,I see!
     
    whl, Oct 20, 2011
    #15
  16. On 18/10/2011 8:48 PM, Patricia Shanahan wrote:
    >
    > 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.


    This is off topic, but I just recently learned that in C++, the result
    of i=i++ is officially undefined. It's interesting a question related
    to what I learned comes up again in a completely different forum.
     
    Travers Naran, Oct 20, 2011
    #16
  17. On Thu, 20 Oct 2011 07:30:31 -0700, Travers Naran <>
    wrote:

    [snip]

    >This is off topic, but I just recently learned that in C++, the result
    >of i=i++ is officially undefined. It's interesting a question related
    >to what I learned comes up again in a completely different forum.


    That comes from C. I have a question for anyone thinking it
    valid Java. What is it supposed to do of use? (If nothing, why even
    use it?) I suspect the whole thing got started by someone not
    understanding that ++ causes an assignment to occur.

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Oct 20, 2011
    #17
  18. whl

    Lew Guest

    Wirchenko wrote:
    > That comes from C. I have a question for anyone thinking it

    [ i = i++; ]
    > valid Java. What is it supposed to do of use? (If nothing, why even
    > use it?) I suspect the whole thing got started by someone not
    > understanding that ++ causes an assignment to occur.


    I have a question for anyone thinking it is not valid Java.

    Why haven't you read the Java Language Specification on this matter?
    <http://java.sun.com/docs/books/jls/third_edition/html/expressions.html>

    --
    Lew
     
    Lew, Oct 20, 2011
    #18
  19. On 10/20/2011 12:08 PM, Gene Wirchenko wrote:
    > On Thu, 20 Oct 2011 07:30:31 -0700, Travers Naran<>
    > wrote:
    >
    > [snip]
    >
    >> This is off topic, but I just recently learned that in C++, the
    >> result of i=i++ is officially undefined. It's interesting a
    >> question related to what I learned comes up again in a completely
    >> different forum.

    >
    > That comes from C. I have a question for anyone thinking it valid
    > Java. What is it supposed to do of use? (If nothing, why even use
    > it?) I suspect the whole thing got started by someone not
    > understanding that ++ causes an assignment to occur.


    I'm guessing that the largest reason for this issue lies in the fact
    that ++/-- is largely tied to increment/decrement addressing modes (so
    *p++ would translate to "Load from p, auto-increment the pointer") [1].
    In cases where it would compile to an autoincrement address mode, the
    effect would take place immediately after the access, much as it does in
    Java. However, on machines without this mode, it probably made more
    sense to translate it into "*p; p += 1;". This is, I believe, the reason
    why the C committee made |i=i++| invalid.

    In contrast, Java sought to leave nothing undefined in its semantics, so
    it has to pick a particular point in time at which the ++ takes place.
    If you're not tied to any overt architecture, the time that makes the
    most sense is to do it is immediately after getting the value (even
    before its use!). That means that the following function has a
    well-defined output:

    int testWhen() {
    int i = 0;
    try {
    int j = 5 / i++;
    } catch (Exception e) { return i; }
    return i;
    }

    It is 1: the increment happens before the division.

    Note that the only point of |i=i++| is to point out that how and when
    the increment occurs is not fully specified in C, while it is in Java.

    [1] Apparently, this isn't why they were originally developed, according
    to <http://cm.bell-labs.com/cm/cs/who/dmr/chist.html>. Kind of... the
    PDP-7 had some memory slots which autoincremented on load, which may
    have been the spark that caused them to be introduced in the language.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Oct 20, 2011
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    598
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    664
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    951
    Scott Allen
    Oct 6, 2005
  4. danilo of TUP

    some questions from a freshman

    danilo of TUP, Feb 19, 2006, in forum: C Programming
    Replies:
    1
    Views:
    325
    santosh
    Feb 19, 2006
  5. fangshuo

    freshman to webservice looking for help

    fangshuo, Jan 27, 2005, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    132
    fangshuo
    Jan 27, 2005
Loading...

Share This Page