A freshman's question

W

whl

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

whl

whl said:
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
 
A

Arved Sandstrom

whl said:
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
 
W

whl

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

thank you ,I will try more experiment.
 
W

whl

whl said:
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?
 
W

whl

whl said:
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?
 
L

Lars Enderin

2011-10-19 12:38, whl skrev:
whl said:
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.

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

Tim Slattery

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

Lew

whl said:
Patricia said:
whl said:
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.
 
R

Roedy Green

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

whl

whl said:
whl 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
 
W

whl

whl said:
Patricia said:
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.

thank you ,now ,I think this is a very simple question,I understand
all of your answer,maybe I put this complicated matters!
 
W

whl

2011-10-19 12:38, whl skrev:








whl 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 !
 
W

whl

No question.


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?

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

whl

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

Travers Naran

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

Gene Wirchenko

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

Lew

Wirchenko said:
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>
 
J

Joshua Cranmer

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top