-
i = i++;
hi,
i wish to point here a strange expression:
i = 20;
i = i++;
System.out.println(i);
this results on i = 20
when the same is done with c++, the result is 21.
the computation in Java seems strange.
The post fix operator shud be evaluated after the assignment. But it seems
it is never incrememted at all.
Please suggest possible solutions.
-
Re: i = i++;
"SK" <skrishna@mail.com> wrote:
>
>hi,
>
>i wish to point here a strange expression:
>
>i = 20;
>i = i++;
>System.out.println(i);
>
>this results on i = 20
>
>when the same is done with c++, the result is 21.
>
>the computation in Java seems strange.
>The post fix operator shud be evaluated after the assignment. But it seems
>it is never incrememted at all.
>
>Please suggest possible solutions.
Hi,
i=i++ means
-
Re: i = i++;
The result is not "strange", it is what the Java specification says. And if
you write "i=20; i=++i;" then the result will be i==21, also as the Java
spec says.
Why on earth would you write something like "i=i++" when "i++" is sufficient
to increment i?
PC2
SK <skrishna@mail.com> wrote in message news:3a5193fb@news.devx.com...
>
> hi,
>
> i wish to point here a strange expression:
>
> i = 20;
> i = i++;
> System.out.println(i);
>
> this results on i = 20
>
> when the same is done with c++, the result is 21.
>
> the computation in Java seems strange.
> The post fix operator shud be evaluated after the assignment. But it seems
> it is never incrememted at all.
>
> Please suggest possible solutions.
>
-
Re: i = i++;
"SK" <skrishna@mail.com> wrote:
>
>hi,
>
>i wish to point here a strange expression:
>
>i = 20;
>i = i++;
>System.out.println(i);
>
>this results on i = 20
>
>when the same is done with c++, the result is 21.
>
>the computation in Java seems strange.
>The post fix operator shud be evaluated after the assignment. But it seems
>it is never incrememted at all.
>
>Please suggest possible solutions.
>
Hi SK,
my solution is:
(Microsoft VC++ 6.0 on NT4.0 Service Pack 4)
#include <iostream>
using namespace std;
void main (){
int i = 20;
i = i++;
cout << "i: " << i;
}
result:
i = 21; // it's OK
(Borland JBuilder 4.0 Foundation (JDK1.3))
public class Untitled1 {
public Untitled1() {
}
public static void main(java.lang.String[] args){
int i = 20;
int a = i++;
java.lang.System.out.print("i: ");
java.lang.System.out.print(i);
java.lang.System.out.println("a: ");
java.lang.System.out.print(a);
}
}
result:
i = 21; // it's OK
a = 20; // it's OK
Ithink that the java code:
i = i++;
don't do:
i = i;
i = i + 1;
because there is a bug in java compiler.
I think the problem is in CPU's register manage.
Bey.
-
Re: i = i++;
Clode Anty <clodeanty@hotmail.com> wrote in message
news:3a51fd8e$1@news.devx.com...
>
....
> Ithink that the java code:
> i = i++;
>
> don't do:
>
> i = i;
> i = i + 1;
>
> because there is a bug in java compiler.
>
It's not a bug, it's a feature. The Java language specification says that
"i++" returns the value of i before the increment takes place and that "++i"
returns the value of i after the increment takes place.
-
Re: i = i++;
But the problem is that the incrementation doesn't occur AT ALL. The java
spec says that i is returned, then i is incremented. There is obviously
a bug in the compiler.
-Patrick
"Paul Clapham" <pclapham@core-mark.com> wrote:
>
>Clode Anty <clodeanty@hotmail.com> wrote in message
>news:3a51fd8e$1@news.devx.com...
>>
>....
>> Ithink that the java code:
>> i = i++;
>>
>> don't do:
>>
>> i = i;
>> i = i + 1;
>>
>> because there is a bug in java compiler.
>>
>It's not a bug, it's a feature. The Java language specification says that
>"i++" returns the value of i before the increment takes place and that "++i"
>returns the value of i after the increment takes place.
>
>
-
Re: i = i++;
thankx for all the responses..
ok, can someone clarify :
what do '++x' and 'x++' actually return?
i mean, does '++x' return the incremented value or does it return the reference
to 'x' itself....
and similarily, 'x++', does it return the incremented value or the refernce
to the temporary mem loc where the value of x has been copied...
also, can someone actually elaborate how does prefix and postfix operators
function (i mean in the sense of implementation by the compiler..)
- thanking in advance..
"Patrick" <pmccon@emory.edu> wrote:
>
>But the problem is that the incrementation doesn't occur AT ALL. The java
>spec says that i is returned, then i is incremented. There is obviously
>a bug in the compiler.
>
>-Patrick
>
>"Paul Clapham" <pclapham@core-mark.com> wrote:
>>
>>Clode Anty <clodeanty@hotmail.com> wrote in message
>>news:3a51fd8e$1@news.devx.com...
>>>
>>....
>>> Ithink that the java code:
>>> i = i++;
>>>
>>> don't do:
>>>
>>> i = i;
>>> i = i + 1;
>>>
>>> because there is a bug in java compiler.
>>>
>>It's not a bug, it's a feature. The Java language specification says that
>>"i++" returns the value of i before the increment takes place and that
"++i"
>>returns the value of i after the increment takes place.
>>
>>
>
-
Re: i = i++;
Patrick <pmccon@emory.edu> wrote in message news:3a538b8c$1@news.devx.com...
>
> But the problem is that the incrementation doesn't occur AT ALL. The java
> spec says that i is returned, then i is incremented. There is obviously
> a bug in the compiler.
>
No. Here's what happens.
1) i++ is evaluated: i is incremented, and the value of i++ (contrary to
what you claim) is the value of i BEFORE the incrementation took place.
(Cut and pasted from the Java Language Specification: "The value of the
postfix increment expression is the value of the variable before the new
value is stored.")
2) That value is assigned to i.
So in fact the incrementation DOES occur, but it is thrown away by the
assignment statement. There is no bug in the compiler.
-
Re: i = i++;
I agree with Paul. In c++ the following is legal code.
++i = i;
since ++i is a l-value whereas i++ isn't. However in Java neither ++i, i++
are l-values, so i++ does indeed have value i which is then assigned to i.
In c++
int i = 20;
i = i++;
appears to be like writing
int i = 20;
(i = i)++;
which will make i = 21. This could be to do with the difference in l-values
between the languages.
-
Re: i = i++;
peter.seymour@simcorp.com (Peter Seymour) wrote:
>I agree with Paul. In c++ the following is legal code.
>
>++i = i;
>
>since ++i is a l-value whereas i++ isn't. However in Java neither ++i, i++
>are l-values, so i++ does indeed have value i which is then assigned to
i.
>
>In c++
>
>int i = 20;
>i = i++;
>
>appears to be like writing
>
>int i = 20;
>(i = i)++;
>
>which will make i = 21. This could be to do with the difference in l-values
>between the languages.
Actually, while I don't have an ANSI C++ or Java spec in front of me, I'll
bet that the output of this code in either language in unspecified. In other
words, the compiler can handle it either way and it's correct.
I know that C++ doesn't define the order of evaluation for prefix and
postfix operators within the argument list of a function call. I've seen
this
kind of example many times before:
int x = 20;
printf("%d %d %d", x++, x++, x++);
can produce either
20 20 20
or
20 21 22
or
22 21 20
or many other combinations. The only thing you can count on from
the code above is that x will be incremented 3 times, so its value
will be 23 after the printf statement. Other than that, it is completely
up to the compiler to decide when to do each of the increments.
As a side note, the same is true with the prefix operator: if you changed
the printf statement above to read 'printf("%d %d %d\n", ++x, ++x, ++x");',
the only difference would be that the values printed would be in the range
21-23 instead of 20-22.
Also, if you call a function for each argument, there's no guarantee of the
order that those functions are called:
printf("%d %d %d\n", f1(x), f2(x), f3(x));
The functions could be called in the order 'f1, f2, f3', or 'f3, f2, f1',
or,
'f2, f1, f3'. If you put the same printf statement twice in a row in your
program, the compiler could even evaluate them in a different order
each time.
I think this same logic applies to the "x = x++" example, too, and
I wouldn't be surprised if it's exactly the same in Java.
The bottom line is that, at least in C++, the order of evaluation of function
arguments is undefined. Even if you think you know how your compiler
handles this, you can't rely on it for other compilers, and in fact, you
can't
even rely on your own compiler to do it the same way every time (although
in reality, it probably will). I don't like to rely on "probably's" in my
code,
though.
To answer the original question, how do you fix the code "i = i++" (if that
was a serious problem as opposed to just a question about the language),
the answer is simple: just use 'i++' instead. The "i=" part serves no purpose.
- Paul
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks