Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

question

Thread view: 
parwal.sandeep@gmail.com - 17 Feb 2006 05:07 GMT
hello grp !!!
  i've one question plz excuse if it is silly to ask !!

public static void main( String[] args )
{
    int i=10;
     i = i++;
     SOP (i);
}

why it is printing  10 instead of 11 ??
Lasse Reichstein Nielsen - 20 Feb 2006 22:38 GMT
>      int i=10;
>       i = i++;
...
> why it is printing  10 instead of 11 ??

Because that's what you ask it to do (I assume SOP is a shorthand
for System.out.print).

Execution of the assignment expression "i = i++" first evaluates the
right hand side (i++) and then assigns the value to the variable "i".

Evaluation of i++ is defined to
1) take the value of the variable "i".
2) increment the variable "i"
3) return the value from 1), i.e., the *original* value.

So, you assigment does the following:
- take the value of i (10)
- increment i (i is now 11)
- take the original value (10) and assign it to i (i is now 10)

Then you write i, which is, as expected, 10.

You probably want to change "i = i++" into either "i = i + 1" or "i++",
either will do what you expect (increment the value of "i" by 1).

/L
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.