  Show the output of the following code: Explain brieflyint a = 6;int b = a++;System.out.println(a);System.out.println(b);a = 6;b = ++a;System.out.println(a);System.out.println(b);

Question

Show the output of the following code: Explain briefly

int a = 6;

int b = a++;

System.out.println(a);

System.out.println(b);

a = 6;

b = ++a;

System.out.println(a);

System.out.println(b);

Step 1

The Output of the given code is shown below along with the code. To run the given code, it is important to create a class first and then write the code.

Step 2

In the above code, two variables are declared a and b as integers. The value of a is initialized as 6 and for b, the value is given as a++. So, b= a++ is using the concept of post increment which will first store the value of a in b as 6 and then increment the value of a by 1 and the value of a will become 7. So, the new values of a and b will become 7 and 6 respectively.

Step 3

Now again, value of a is given as 6 and b is given as ++a. So, b=++a is using the concept of pre-increment which will first increment the value ...

Want to see the full answer?

See Solution

Want to see this answer and more?

Our solutions are written by experts, many with advanced degrees, and available 24/7

See Solution
Tagged in

Computer Engineering 