• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Java small code confusion

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 !!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic