Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > question

Reply
Thread Tools

question

 
 
parwal.sandeep@gmail.com
Guest
Posts: n/a
 
      02-17-2006
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 ??

 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      02-20-2006
writes:

> 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
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57