What will be the output if i write code just likeclass testincrement{public static void main(String args[]){int i=5;i=i++;System.out.println("i= "+i);}}and why

Showing Answers 1 - 4 of 4 Answers

Anand

  • Apr 7th, 2006
 

i=5 herebecause i =i++ is post increment.

  Was this answer useful?  Yes

Tirupathaiah

  • Apr 15th, 2006
 

initially i is 5

i=i++ //this statement indicates that first it assigns the value and then increments(post fix)

the value of i will be 6.

so output: i= 6

  Was this answer useful?  Yes

kiran banala

  • Apr 16th, 2006
 

class Testincrement{public static void main(String args[]){int i=5; // here "i" value will be "5 "i=i++; // here "i" value is 5=5++ i.e. 5System.out.println("i= "+i); // so value of i will be 5}}

  Was this answer useful?  Yes

narayanan

  • Oct 11th, 2006
 

take this example:

class test {

public static void main(String ar[])throws Exception {

int i,j,k;

i = 5;j = 4;

i = i++  +  j++;

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

j = i++  + j++;

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

k = i++  +  j++;

System.out.println("i = " + i + "...j = "+ j +"...k = " + k);  }   }

in this example i = 5, j = 4, k = 0

first expression i = i++ + j++.     in this exp first take i and j value so i = 5 + 4 i = 9 but not assign the result to i. because right side exp is not completed. next,the value of i and j is incremented and stored in i = 6 , j = 5. in this time right side exp fully solved. next the result 9 is stored in i. so last i = 9 and j = 5.

next expression j = i++ + j++.  this exp solved by the same method.  first        j = 9 + 5 ...  j = 14. next increment i,j values so i = 10 & j = 6.  next assign the result of exp to j. so, j = 14 and i = 10.

the last exp. k = i++  + j++.  first take i and j value. k = 14 + 10.... k = 24     but not assign it to k. next increment the i and j value. i = 11, j = 15.  next k = 24.

 

  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