Solve it... public class test {public static void main(String[] args) { int i=5; i=i++; System.out.println("result::"+i); }}The result wil be 5..!! and not 6 why???

Questions by shs_1236   answers by shs_1236

Showing Answers 1 - 30 of 30 Answers

kiranmog

  • Jul 4th, 2006
 

Dear Friend,

              Simple and Nice question. I think our JVM first interpret the code line by line then after in the second iteration it will evaluate the expression.

From,

Urs

  Was this answer useful?  Yes

Mukund

  • Jul 6th, 2006
 

Because this is post increment operator which first assign value then increment in the value so here first it assign value of i before increment that is 5 and then increment so value of i will be 5.

  Was this answer useful?  Yes

Sash

  • Jul 8th, 2006
 

In this case the increment will happen after the assignment... this means the last change is the increment.....

so the value must actually be 6 ... right.. ?

  Was this answer useful?  Yes

Sash

  • Jul 8th, 2006
 

i=i++

can be taken up as i=(i=i+1)

here the value of i=5 initially .. then which one is it.. i=5 or i++ in the next line ..?

  Was this answer useful?  Yes

hi,

Ya i know that post increment i++ executes after assigning i=5; but ultimately the i++ should increment i to 6......

But the result is 5 , why??

I think its something related to memory allocation in java or if know the exact answer plz let me know....

thank u

  Was this answer useful?  Yes

Madanamohan

  • Jul 12th, 2006
 

i=5;

i = i++;// here again i is initialised to 5; (here the value of i++ is 5 because of post increment)

at the end of this statement the value of i becomes 5.

so it prints 5 instead of 6

  Was this answer useful?  Yes

brijesh

  • Jul 12th, 2006
 

If we are going to define a member as static that mean during any kind of execution the no. is still the same one.

i.e., no=5;

Ok bye

if i am wrong at any place then please reply back to me. 

  Was this answer useful?  Yes

ann

  • Jul 12th, 2006
 

Basically post incr is i++ (incremented value asssigned after statement is executed) and pre incr is ++i . so in this case it will print 5  as when  i= i++ is executed i is incremented to 6 in memory but not assigned to i so next line will print i as 5 and not 6 but if you do i=++i then 6 is printed.Please correct me if m wrong

Sash

  • Jul 12th, 2006
 

Where is the Static member here ... ?

  Was this answer useful?  Yes

amrendra

  • Jul 12th, 2006
 

sir it is really nice n tricky question . plz, send the answer with reason why it will give the result 5 instead of 6. i think it should give 6 . bye

  Was this answer useful?  Yes

shyam

  • Jul 13th, 2006
 

understand the post increment operator by trying this..

public class test {

public static void main(String[] args) throws Exception
{
int i=5;
  i=i++;
  int j;
for(j=1;j<6;j++){
System.out.println("result::"+i+"  J:"+j);
 Thread.sleep(1000);
}
System.out.println("result:  J:"+j);
}
}

if anything is wrong, please let me know.

  Was this answer useful?  Yes

Deepak

  • Jul 14th, 2006
 

 Hi,

I think someone give the good idea as our JVM first interpret the code line by line then after in the second iteration it will evaluate the expression.

Bye.

  Was this answer useful?  Yes

Laxman

  • Jul 20th, 2006
 


 asignment is the last statement which is executed in any expression as it have the least priority as compare to other operators

 (and it is right associative here it is not necessary to mention it because here is only one = assignment  operator but i mention it just for information)
In above example  unary postfix increment operator ++ is applied to i before the assignment so the last statement executed is assignment that is i=5
 

now even  if you r confused than think it in another way
b=3
a=5
b = a ++
 execution of statements
  b = a++
 1. b = 5++    // a is evaluated as 5
 2.            //then a is incremented in memory a=6
 3 then b is asign to by value 5
 now just put a in place of b
this is my first time i m giving any answer comment on this
 Laxman

  Was this answer useful?  Yes

snm

  • Jul 26th, 2006
 

Here, you are re-assigning the value to i before post increment. What I mean to say that suppose you take another int variable like "j", then assign i++ to j and print both values, you can understand the whole scenario

public class test {

public static void main(String[] args)
{
int i=5;

int j=0;

j=i++;
System.out.println("result - i ::"+i);

System.out.println("result - j ::"+j);
}
}

  Was this answer useful?  Yes

atiq

  • Aug 4th, 2006
 

Dear All,here i=i++; the i++ is post increment so at first the r-value of (i=5) is assigned to the l-value i. and then incrementing, but incremented i's value will not be stored to the memory cell. Therefore, the result is 5. and it is happened mostly for design issues of java compiler.

  Was this answer useful?  Yes

kumaril mishra

  • Aug 11th, 2006
 

the output is 5 because we are getting output of variable  int i,not i++;

so the answer will be 5.

  Was this answer useful?  Yes

jeffsong

  • Aug 17th, 2006
 

The statement i=i++ is executed in 3 steps:1. i=5 (the i to the right side of = ), system stores 5 somewhere in memory, let's say temp=5.2. i++ set i=6 in memory3. i (left one) is assigned with the stored value 5 (it is temp,not i).As I understand, i=i++, post operator does not mean that + is executed after ++. actually it means that = will still be executed the last, but it take the original variable value of the right hand side.public static void main(String[] args) { int i=5; int j =3; j=i++; System.out.println("result:I:"+i); System.out.println("result:J:"+j);}will outputresult:I:6result:J:5

  Was this answer useful?  Yes

Hi,In the above example if u have a close look at this statement, i = i++; Here we are doing post increment,so first it will assign this value to i and then it got incremented. Incremented value is not assign to any variable.So u will get output as 5 and not 6.if u change statement like i++;System.out.println(i) will give 6 instead of 5 because it is incremented and assigned to itself. thanks Moorthi M

  Was this answer useful?  Yes

PG west Coast

  • Nov 8th, 2006
 

No need to get crazy.i=i++ is like :i=i=i+1;which is :put i+1 in i and then i in i.Value does not change then.

  Was this answer useful?  Yes

raja

  • Nov 23rd, 2006
 

no it seems u r little bit regretted...if v pre increment ..According to you the value is remains 5 but it is giving 6 as i am executing...so whom to belive...watz say???

  Was this answer useful?  Yes

for4me

  • May 4th, 2008
 

It is same as System.out.println(i++);

public class test
{
public static void main(String[] args)
    {
        int i=5;
        System.out.println("result::"+i++);
    }
}

It prints 5  first then increments.

  Was this answer useful?  Yes

Dear Friend, this is post increment, that is first assign the value and then increment.
so in this case you first assign the value, so i become 5, now you increment which is of no use.
that's why the answer is 5;

But rather assigning a value if you directly write i++, then the value become 6;

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions