post-increment

Java increment and assignment operator

微笑、不失礼 提交于 2019-11-27 08:41:18
I am confused about the post ++ and pre ++ operator , for example in the following code int x = 10; x = x++; sysout(x); will print 10 ? It prints 10,but I expected it should print 11 but when I do x = ++x; instead of x = x++; it will print eleven as I expected , so why does x = x++; doesn't change the the value of x ? No, the printout of 10 is correct. The key to understanding the reason behind the result is the difference between pre-increment ++x and post-increment x++ compound assignments. When you use pre-increment, the value of the expression is taken after performing the increment. When

Why does post-increment work on wrapper classes

做~自己de王妃 提交于 2019-11-27 07:45:35
问题 I was doing a review of some code and came across an instance of someone post-incrementing a member variable that was a wrapper class around Integer. I tried it myself and was genuinely surprised that it works. Integer x = 0; System.out.print(x++ + ", "); System.out.print(x); This prints out 0, 1 , not 0, 0 as I would have expected. I've looked through the language specification and can't find anything covering this. Can anyone explain to me why this works and if it's safe across multiple

a += a++ * a++ * a++ in Java. How does it get evaluated?

戏子无情 提交于 2019-11-27 06:03:04
问题 I came across this problem in this website, and tried it in Eclipse but couldn't understand how exactly they are evaluated. int x = 3, y = 7, z = 4; x += x++ * x++ * x++; // gives x = 63 System.out.println(x); y = y * y++; System.out.println(y); // gives y = 49 z = z++ + z; System.out.println(z); // gives z = 9 According to a comment in the website, x += x++ * x++ * x++ resolves to x = x+((x+2)*(x+1)*x) which turns out to be true. I think I am missing something about this operator precedence.

Post Increment with respect to Sequence Points

女生的网名这么多〃 提交于 2019-11-27 04:57:15
问题 When does the post increment operator affect the increment? I have come across two opinions: 1) From http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_015.htm: POST means do the operation after any assignment operation. 2) Closer home, an answer on SO(albeit on C++) says: ... that delays the increment until the end of the expression (next sequence point). So does the post increment operation... A) wait until a sequence point is reached or B) happen post an assignment operator or C)

How are java increment statements evaluated in complex expressions

久未见 提交于 2019-11-27 04:52:36
问题 What is the output of the following code: int x = 2; x += x++ * x++ * x++; System.out.println(x); I understand that ++variableName is pre-increment operator and the value of variableName is incremented before it is used in the expression whereas variableName++ increments its value after the expression is executed. What I want to know is - how does this logic apply here? 回答1: Its easier to see the what is going on with x = 1 instead of 2. The output for x=1 is 7. The key to the understanding

Why doesn't changing the pre to the post increment at the iteration part of a for loop make a difference?

坚强是说给别人听的谎言 提交于 2019-11-27 04:21:35
Why does this int x = 2; for (int y =2; y>0;y--){ System.out.println(x + " "+ y + " "); x++; } prints the same as this? int x = 2; for (int y =2; y>0;--y){ System.out.println(x + " "+ y + " "); x++; } As far, as I understand a post-increment is first used "as it is" then incremented. Are pre-increment is first added and then used. Why this doesn't apply to the body of a for loop? The loop is equivalent to: int x = 2; { int y = 2; while (y > 0) { System.out.println(x + " "+ y + " "); x++; y--; // or --y; } } As you can see from reading that code, it doesn't matter whether you use the post or

Why is “while (i++ < n) {}” significantly slower than “while (++i < n) {}”

♀尐吖头ヾ 提交于 2019-11-27 04:14:10
问题 Apparently on my Windows 8 laptop with HotSpot JDK 1.7.0_45 (with all compiler/VM options set to default), the below loop final int n = Integer.MAX_VALUE; int i = 0; while (++i < n) { } is at least 2 orders of magnitude faster (~10 ms vs. ~5000 ms) than: final int n = Integer.MAX_VALUE; int i = 0; while (i++ < n) { } I happened to notice this problem while writing a loop to evaluate another irrelevant performance issue. And the difference between ++i < n and i++ < n was huge enough to

The difference between ++Var and Var++ [duplicate]

末鹿安然 提交于 2019-11-27 03:09:37
This question already has an answer here: How do the post increment (i++) and pre increment (++i) operators work in Java? 14 answers In programming, particularly in Java, what is the difference between: int var = 0; var++; and int var = 0; ++var; What repercussions would this have on a for loop? e.g. for (int i = 0; i < 10; i++) {} for (int i = 0; i < 10; ++i) {} Dónal tldr; Although both var++ and ++var increment the variable they are applied to, the result returned by var++ is the value of the variable before incrementing, whereas the result returned by ++var is the value of the variable

Why can't I do ++i++ in C-like languages?

為{幸葍}努か 提交于 2019-11-27 01:47:16
问题 Half jokingly half serious : Why can't I do ++i++ in C-like languages, specifically in C#? I'd expect it to increment the value, use that in my expression, then increment again. 回答1: Short answer: i++ is not an "lvalue", so can't be the subject of an assignment. 回答2: Though the short answer "it's not an lvalue" is correct , that's perhaps just begging the question. Why isn't it an lvalue ? Or, as we say in C#, a variable . The reason is because you cannot have your cake and eat it too . Work

Post increment operator not incrementing in for loop

柔情痞子 提交于 2019-11-26 22:39:18
I'm doing some research about Java and find this very confusing: for (int i = 0; i < 10; i = i++) { System.err.print("hoo... "); } This is never ending loop! Anybody has good explanation why such thing happens? for (int i = 0; i < 10; i = i++) { The above loop is essentially the same as: - for (int i = 0; i < 10; i = i) { the 3 rd part of your for statement - i = i++ , is evaluated as: - int oldValue = i; i = i + 1; i = oldValue; // 3rd Step You need to remove the assignment from there, to make it work: - for (int i = 0; i < 10; i++) { (On OP request from Comments) Behaviour of x = 1; x = x++