pre-increment

Is ++x more efficient than x++ in Java?

无人久伴 提交于 2019-11-27 15:02:41
During a programming class, the professor was teaching us about x++ and ++x , with x being an integer. He said that in the scenario we are able to just put either x++ or ++x , ++x is more efficient (by little, but still, in theory, more efficient nonetheless). But I forgot why . Anyone knows? This was with Java. zneak It's not more efficient in Java. It can be more efficient in languages where the increment/decrement operators can be overloaded, but otherwise the performance is exactly the same. The difference between x++ and ++x is that x++ returns the value of x before it was incremented,

Is the pre-increment operator thread-safe?

江枫思渺然 提交于 2019-11-27 15:00:42
I'm making a program in java that races a few cars against each other. Each car is a separate thread. When cars complete the race, each one all calls this method. I've tested the method at varying timer speeds, and it seems to work fine. But I do realize that each thread is accessing the variable carsComplete, sometimes at the exact same time (at least at the scope the date command is giving me). So my question is: is this method thread-safe? public static String completeRace() { Date accessDate = new Date(); System.out.println("Cars Complete: " + carsComplete + " Accessed at " + accessDate

Expressions \"j = ++(i | i); and j = ++(i & i); should be a lvalue error?

筅森魡賤 提交于 2019-11-27 12:18:05
I was expecting that in my following code: #include<stdio.h> int main(){ int i = 10; int j = 10; j = ++(i | i); printf("%d %d\n", j, i); j = ++(i & i); printf("%d %d\n", j, i); return 1; } expressions j = ++(i | i); and j = ++(i & i); will produce lvalue errors as below: x.c: In function ‘main’: x.c:6: error: lvalue required as increment operand x.c:9: error: lvalue required as increment operand But I surprised that above code compiled successfully, as below: ~$ gcc x.c -Wall ~$ ./a.out 11 11 12 12 Check the above code working correctly. While other operators produce error (as I understand).

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 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

Java: pre-,postfix operator precedences

我们两清 提交于 2019-11-26 21:32:53
问题 I have two similar questions about operator precedences in Java. First one: int X = 10; System.out.println(X++ * ++X * X++); //it prints 1440 According to Oracle tutorial: postfix (expr++, expr--) operators have higher precedence than prefix (++expr, --expr) So, I suppose that evaluation order: 1) first postfix operator: X++ 1.a) X++ "replaced" by 10 1.b) X incremented by one: 10+1=11 At this step it should look like: System.out.println(10 * ++X * X++), X = 11; 2) second POSTfix operator: X++

Pre- & Post Increment in C#

我与影子孤独终老i 提交于 2019-11-26 20:27:23
I am a little confused about how the C# compiler handles pre- and post increments and decrements. When I code the following: int x = 4; x = x++ + ++x; x will have the value 10 afterwards. I think this is because the pre-increment sets x to 5 , which makes it 5+5 which evaluates to 10 . Then the post-increment will update x to 6 , but this value will not be used because then 10 will be assigned to x . But when I code: int x = 4; x = x-- - --x; then x will be 2 afterwards. Can anyone explain why this is the case? Sebastian Piu x-- will be 4, but will be 3 at the moment of --x , so it will end

Multiple preincrement operations on a variable in C++(C ?)

孤街浪徒 提交于 2019-11-26 20:21:29
Why does the following compile in C++? int phew = 53; ++++++++++phew ; The same code fails in C, why? Prasoon Saurav That is because in C++ pre-increment operator returns an lvalue and it requires its operand to be an lvalue . ++++++++++phew ; in interpreted as ++(++(++(++(++phew)))) However your code invokes Undefined Behaviour because you are trying to modify the value of phew more than once between two sequence points . In C , pre-increment operator returns an rvalue and requires its operand to be an lvalue . So your code doesn't compile in C mode. Note: The two defect reports DR#637 and DR