| Author |
Java small code confusion
|
Sangeeta Sharma
Greenhorn
Joined: Aug 03, 2005
Posts: 2
|
|
Hi Frns, I am writing a simple program and got confused with a small fragment of code.I am writing that code fragment pls clarify the confusion. int i=10; int k=0; i = ++i; i = i++; System.out.println(i); this will print value as 11 and if the same program is written as Int i=10; int k=0; k = ++i; k = i++; System.out.println(i); this will print i as 12 why is there disprecency in the answers
|
Thanks & Regards<br />Sangeeta
|
 |
Stuart Gray
Ranch Hand
Joined: Apr 21, 2005
Posts: 410
|
|
To really understand why you should realise that statements such as: i = ++i i = i++ are actually considered very bad form. The preincrement (++i) and postincrement (i++) operators do not need to be part of an assignment statement, and in fact using them as part of one can cause a lot of confusion. For example the statement i=i++ actually has the same result as i=i - which of course is a totally redundant statement. If you change the above two lines to: i++; ++i; or even simply: i += 2; you will hopefully see why there is a difference between the two peices of code. [EDIT: Incidentally, this is the second or third time this problem has come up in recent weeks. Is this just a common beginners mistake or is there actually an institution somewhere giving out this kind of code for assignments?] [ August 04, 2005: Message edited by: Stuart Gray ]
|
 |
Mahesh x Bogadi
Ranch Hand
Joined: Jul 06, 2004
Posts: 51
|
|
You need to understand the preincrement and postincrement first. * Preincrement does - first increment the value and then give the value. * Postincrement does - first return(get) the value and then increment. Hence the behavior. The assignment operation happens like : Say expression is var = (expression); *the expression is evaluated. Say (expression resulted in x) so var = x; *hence var has value x. If we apply the same to below. int i=10; int k=0; i = ++i; // ++i results in 11 so i = 11, hence value of i becomes 11 i = i++; // here the i++ result 11 though the value in i is incremented // to 12 then i = 11, hence the value of i becomes 11 System.out.println(i); this will print value as 11 Then below is easy. and if the same program is written as Int i=10; int k=0; k = ++i; k = i++; System.out.println(i); this will print i as 12 I hope this helps.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
Originally posted by Stuart Gray: Incidentally, this is the second or third time this problem has come up in recent weeks. Is this just a common beginners mistake or is there actually an institution somewhere giving out this kind of code for assignments?
SCJP study guides always have questions about the behavior of things like i = i++ + ++i; as though this were a common sort of programming conundrum. Unfortunately, studying for the SCJP is quite often a fledgling programmer's first exposure to Java.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Sherry Jacob
Ranch Hand
Joined: Jun 29, 2005
Posts: 128
|
|
Originally posted by Sangeeta Sharma: Hi Frns, I am writing a simple program and got confused with a small fragment of code.I am writing that code fragment pls clarify the confusion. int i=10; int k=0; i = ++i; i = i++; System.out.println(i); this will print value as 11 and if the same program is written as Int i=10; int k=0; k = ++i; k = i++; System.out.println(i); this will print i as 12 why is there disprecency in the answers
Hi Sangeeta, There is no discrepancy in the answers. The answers are absolutely correct. Why ? Here's why... When u say ++i, u can segregate this operation into 3 separate operations : i += 1; result = i; return result; Thus in the first program, when u say i = ++i, u get the value of i as : i += 1 //i.e..11; result = i //i.e..result = 11 return result; //i.e..return 11; This value returned (11) is then stored into i. So new value of i becomes 11. Now when u use the operation i++, u can consider 3 operations again: result = i; i += 1; return result; So in the next line of the first program, when u say i = i++, u get: result = i //i.e..result = 11 since i got the new value 11 from the previous step. i += 1 //i.e..i = 11+1 = 12. return result //i.e..return 11 Hope u understood that !! Now coming to the next program lines with variable 'k': Keeping the above 3-line operations in view, we have k = ++i; So, i += 1; //i.e..i=11 result = i; //i.e..result = 11 return result // return 11; So k = 11. and the next statement : k = i++ yields result = i; //i.e..result = 11 since i is 11 from previous step i += 1; //i.e..i = 12; return result; //i.e..return 11 this final value 11 is stored in k. Hence i=11 and k=11 from first step and i=12 and k=11 from second step. Hope this gets ur fundas absolutely crystal clear !! Cheers !!
|
<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
|
 |
 |
|
|
subject: Java small code confusion
|
|
|