What is the difference between a++ and ++a in coding?

I hope you are quite familiar with the terms PRE-INCREMENT and POST-INCREMENT.

What is the term pre and post ???

It is just similar to the postpaid sim cards and prepaid sim cards.

In postpaid sim we will do all the actions(calls , texting , data consuming and all other actions) and finally we will pay the bill .

Whereas in prepaid, first we will pay the bill and then we perform our desire action.

This same logic is applied in this pre-increment and post-increment.

Pre-Increment is similar to prepaid sim and post-increment is similar to postpaid sim.

class Demo{

public static void main(String[] args){

int a=10; // a is assigned an value

int b = ++a; //b value is 11 as it is pre-incremented (Now a =11 and b=11)

int c = a++; // c value is also 11 and a value is 12

System.out.prinltn(“a=”+a+ “b=” +b+ “c=” +c);

}

}

Output

a=12 b=11 c=11

Both,

++a and a++ performs same operation when it is not assigned to any variable.

But when it is assigned to any value it shows it’s nature 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s