Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > A freshman's question

Reply
Thread Tools

A freshman's question

 
 
whl
Guest
Posts: n/a
 
      10-19-2011
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!
 
Reply With Quote
 
 
 
 
whl
Guest
Posts: n/a
 
      10-19-2011
On 10月19日, 上午11时48分, Patricia Shanahan <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Arved Sandstrom
Guest
Posts: n/a
 
      10-19-2011
On 11-10-19 04:54 AM, whl wrote:
> On 10月19日, 上午11时48分, Patricia Shanahan <(E-Mail Removed)> 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

 
Reply With Quote
 
whl
Guest
Posts: n/a
 
      10-19-2011
On Oct 19, 5:05 pm, Arved Sandstrom <(E-Mail Removed)>
wrote:
> On 11-10-19 04:54 AM, whl wrote:
>
>
>
>
>
>
>
>
>
> > On 10月19日, 上午11时48分, Patricia Shanahan <(E-Mail Removed)> 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.
 
Reply With Quote
 
whl
Guest
Posts: n/a
 
      10-19-2011
On Oct 19, 4:20 pm, Patricia Shanahan <(E-Mail Removed)> wrote:
> whl wrote:
> > On 10月19日, 上午11时48分, Patricia Shanahan <(E-Mail Removed)> 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?
 
Reply With Quote
 
whl
Guest
Posts: n/a
 
      10-19-2011
On Oct 19, 4:20 pm, Patricia Shanahan <(E-Mail Removed)> wrote:
> whl wrote:
> > On 10月19日, 上午11时48分, Patricia Shanahan <(E-Mail Removed)> 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?
 
Reply With Quote
 
Lars Enderin
Guest
Posts: n/a
 
      10-19-2011
2011-10-19 12:38, whl skrev:
> On Oct 19, 4:20 pm, Patricia Shanahan <(E-Mail Removed)> wrote:
>> whl wrote:
>>> On 10月19日, 上午11时48分, Patricia Shanahan <(E-Mail Removed)> 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.
 
Reply With Quote
 
Tim Slattery
Guest
Posts: n/a
 
      10-19-2011
Lars Enderin <(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed)
http://members.cox.net/slatteryt
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      10-19-2011
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-20-2011
On Tue, 18 Oct 2011 20:08:05 -0700 (PDT), whl
<(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments