-2

I want to know that when i=10 and k=0 then and when i am trying to print the value of i when i am doing like this :

case 1 : i = i++;

It's printing i=10 where

case 2 : k = i++;

It's printing i = 11;

I want to know how exactly doing this operation? because in both cases i++ is doing and after then i am printing the value of i?

if in the second case i am getting i=11; then i should get 11 in case 1 also. Please someone explain me the process?

The Code is like this :

public class Case1{
public static void main(String[] a){
int i = 10;
i = i++;
System.out.println(i);
}
}

It's giving ans : 10;

and

public class Case1{
public static void main(String[] a){
int i = 10;
int k = 0;
k = i++;
System.out.println(i);
}
}

Here the ans is coming as 11.

Now please explain me both the cases like how it's doing in the first program i = 10 and in second program 11 as ans.

dsahoo188
  • 61
  • 1
  • 7

4 Answers4

4

The expression i++ increments the value of i by 1 and returns the value of i before the increment was done. So in case 1, i++ changes the value of i to 11 but returns the value 10, which you then assign back to i, effectively undoing the increment.

Case 2 is different because you're assigning the result of i++ to a different variable, so the increment is not undone. k holds the value 10 and i holds the value 11.

Joe Farrell
  • 3,502
  • 1
  • 15
  • 25
3

In the first case, the side effect of ++ is applied to i before the assignment is performed:

  • i++ is evaluated; the result is 10 (remember, it's a post-increment!)
  • i+1 is stored in i, which briefly becomes 11 (but nobody "sees" it, because it gets overwritten)
  • The result of the evaluation of pre-increment, i.e. 10, is written back to i

When you make an assignment to k, there is no double-writing to i:

  • i++ is evaluated; the result is 10
  • i+1 is stored in i, which becomes 11, and stays like that
  • The result of the evaluation, i.e. 10, is written to k, which becomes 10
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Your first code is similar to this:

public class Case1{
 public static void main(String[] a){
   int i = 10; // i becomes 10
   int tmp = i; // tmp becomes 10
   i = i + 1; // i becomes 11
   i = tmp; // i becomes tmp's value, which is 10
   System.out.println(i); // The result is 10
 }
}

and second code to this:

public class Case1{
 public static void main(String[] a){
   int i = 10;  // i becomes 10
   int k = 0;  // k becomes 0
   int tmp = i; // tmp becomes 10
   i = i + 1; // i becomes 11
   k = tmp; // k becomes tmp's value, which is 10
   System.out.println(i); // The result is 11 as you print the i. 
                          // If k was printed, it would be 10 like in example 1.
 }
}
Petros P
  • 99
  • 8
0

ok dude,first i wrote sample program below please check this and will explain you

import java.util.*; public class StackTest {

public static void main(String[] args) {
           Scanner sc =new Scanner(System.in);
           int num=sc.nextInt();
    int i=10;
    int k;
    switch(num){
    case 1:
        i=i++;
        System.out.println("case1:"+i);
        break;
    case 2:
         k=i++;
        System.out.println("case2:"+i);
        break;
        default:break;
    }

}

}

Here u doubt is first case u may pass num is 1 result is:10, second case u may pass num is 2 result is:11 right. ok i am going to give clarity on this. First case what happens means that 'i' value is not incremented just the 10 is assigned to same memory allocated variable like 'i' but in second case the incremented result 'i' value is assigned to some other new variable like k ,here result is will allocate to k and print 11

see this u can understand and get both cases 11 as outout

import java.util.*; public class StackTest1 {

public static void main(String[] args) {
           Scanner sc =new Scanner(System.in);
           int num=sc.nextInt();
    int i=10;
    int k;
    switch(num){
    case 1:
        k=i++;
        System.out.println("case1:"+i);
        break;
    case 2:
         k=i++;
        System.out.println("case2:"+i);
        break;
        default:break;
    }

}

} i think this answear is satisfy you.Thank you