-1

I'm new to the coding can anyone explain to me why below code is giving output as '6'

class Test{
    public static void main(String[] args) {
        int i = 5;
        i = i++;
        i = ++i;
        System.out.print(i);
    }
}
user3359125
  • 141
  • 1
  • 1
  • 6

1 Answers1

1

i = i++; This line assign 5 to i again you can run this code to ensure about that:

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

For more information visit here

Mohsen
  • 4,536
  • 2
  • 27
  • 49