Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes ++ operator (C & Java) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "++ operator (C & Java)" Watch "++ operator (C & Java)" New topic
Author

++ operator (C & Java)

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
consider the folowing code:
int i = 0;
i = i++;
System.out.println(i);
when run, it outputs 0 to the console while the same code in C/C++ outputs 1
Shouldn�t this output be the same since I�m first atributing i�s current value to i and then incrementing i by one ??
Is it just me, am I missing something or is this weird?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
It should be 1 in both languages, as simple as that.
In the second line of your code, i is assigned a value of ( i+1 ).
By the time the third line executes, i is 1, no matter if you use a prefix or postfix ++ operator.
Maybe there's a typo in your code?
What causes confusion to most programmers is the difference between:
System.out.println ( i++ );
and
System.out.println ( ++i );
where the first outputs 0, and the second outputs 1 (if i = 0 at start).
This is because a postfix ++ operator first uses the value of i in the command (System.out.println), and then adds 1 to i.
In the second case, the prefix operator first adds 1 and then uses the value of i (incremented by then).
Hope this can clarify things.
Regards,
G�nther.
http://www.javacoding.net
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Actually the correct result is 0.
This has been asked (and answered) countless times in programmer certification. Try to do a search here for the previous threads on it. You find a ton.


Dave
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Oops ... my answer was a bit fast.
I overlooked one thing:
the line "i = i++;"
doesn't modify i, since "i++" evaluates to i, then i is incremented, and then the value of the evalutation (i++) is put into i.
This makes that in the end, i is 0 again.
G�nther.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
This link is to a past discussion on this very topic.
Good Luck,
-Dirk Schreckmann


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
And...
This conversation is also taking place today over in the Programmer Certification Study thread, which leads to this explanation from earlier this month.
Good Luck,
-Dirk Schreckmann
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ++ operator (C & Java)
 
Similar Threads
Calling a C++ executable from JSP?
two different results
A problem determining the length of an int variable converted to a string..........
"Sequential Logic" question
manpulating Unicode values..