Working of post increment operator in Java

0 votes

I am really confused about how the post-increment operator in Java works. I was referring to a doc, where the author says:

j=j++ ;

is equivalent to:

temp=j;

j=j+1;

j=temp;

But, what I have learned is:

a=b++;

Which evaluates like:

a=b;

b=b+1;

So, following this logic:

j=j++;

Should be evaluated as:

j=j;

j=j+1;

So, where does this temp comes from? I am not able to understand this temp concept. Please do leave your explanation regarding this.

May 23, 2018 in Java by anto.trigg4
• 3,420 points
30 views

1 answer to this question.

0 votes

Well, I think the confusion is because you have changed the actual order of evaluation from increment and assign to assign and increment. So,

a=b++;

will be evaluated like:

temp=b;
b=b+1;     // increment
a=temp;    // then assign

And not as:

a=b;      // assign
b=b+1;    // then increment

Hope this makes things clear for you.

answered May 23, 2018 by geek.erkami
• 2,320 points

Related Questions In Java

0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,190 points
139 views
0 votes
2 answers

How to find out current working directory in Java?

You can also use java.nio.file.Path and java.nio.file.Paths. Path ...READ MORE

answered Jul 30, 2018 in Java by Sushmita
• 6,880 points
38 views
0 votes
2 answers

Counting no of Occurrence of a particular character inside a string in Java

We can find out the no. of ...READ MORE

answered Sep 7, 2018 in Java by Sushmita
• 6,880 points
250 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,050 points
462 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 8,061 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,090 points
1,967 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
5,125 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
492 views
0 votes
2 answers

Fetch list of in-between dates using Java

java.time Package The new java.time.package in Java 8 incorporates ...READ MORE

answered Aug 21 in Java by Sirajul
• 25,220 points
82 views
0 votes
2 answers

Listing all the subclasses of a specific class in Java

This is not possible to do using ...READ MORE

answered Aug 21 in Java by Sirajul
• 25,220 points
176 views