This question already has answers here:
What's the difference between i++ vs i=i+1 in an if statement? [duplicate]
(2 answers)
Closed 1 year ago.
can you do array[i]++?
Is it the same as array[i] = array[i] + 1?
If not, why not? Is it something to do with how primitives vs references are treated?
There's a slight difference: postfix vs prefix increment operator
If you use array[i]++, the old value will be used for the calculation and the value of i will be increased by 1 afterwards.
For array[i] = array[i] + 1, it's the opposite: i will first be incremented and only then the calculation will take place.
For more details, check this out.
Related
This question already has answers here:
Is ++x more efficient than x++ in Java?
(3 answers)
Difference between pre-increment and post-increment in a loop?
(22 answers)
Closed 4 years ago.
I've read recently that ++i is much more efficient in C++ than its counterpart i++ because the first (++i) does the increment and returns the incremented value, whereas the latter (i++) creates a copy of the value, return it and performs an increment.
Does the same happen in Java?
Both are not atomic operations composed of the multiple steps. Unlike in C++, these operators can't be overloaded. So there is no difference in Java in the matter of performance.
The only and only difference you should mind between x++ and ++x is that x++ returns the value before it's incremented. And ++x does the same but after the incrementation.
This answer provides a bytecode example.
This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 4 years ago.
capacity = (cap > 0) ? cap : CAPACITY;
I'm just looking through my lecture notes, and I can't figure out what this line of code does. Can someone help me?
It's called conditional expression and in your case it means that if cap is greater than 0, the capacity variable will get the value "cap", if false then it will get the value "CAPACITY".
This question already has answers here:
Byte incrementing operation in Java [duplicate]
(4 answers)
Closed 7 years ago.
When I start my code
for (byte i = 0; i < 1000; i++) {
System.out.print(i);
}
I get infinite loop. Why?
Simply because byte value starts to overflow after its max value i.e. 127.
The value of i will go up to 127 and then will overflow to -128 and then increment back to 127. This process will therefore never satisfy your for loop termination condition, and thus loop forever.
The maximum value of a byte is less than 1000.
This question already has answers here:
++someVariable vs. someVariable++ in JavaScript
(7 answers)
Closed 7 years ago.
I don't consider myself to be bad at programming, but there's been something troubling me since the past few days.
int counter = 3;
++counter;
Is the following code above the same as counter++;.
It is similar, but not the same.
In your expression it doesn't matter, but if you had something more complicated, like System.out.println(counter++), it would make a big difference.
For example:
int counter = 3;
System.out.println(counter++)
This will print 3, then increment counter to 4.
However, if you do
int counter = 3;
System.out.println(++counter)
it will print 4 because it increments prior to giving the value as a parameter to the print function.
It's a question of when the increment is performed, the prefix performs it before other operations, postfix performs it after. They have different precedences.
This question already has answers here:
Post-increment and pre-increment within a 'for' loop produce same output [duplicate]
(12 answers)
Semantics of pre- and postfix "++" operator in Java [duplicate]
(7 answers)
Closed 9 years ago.
In some code i've seen a for loop with a --i as third parameters
for(int i=array.length; i<0; --i)
Maybe someone can explain me the difference with i-- ?
i guess it's something like the moment when i is decremented ?
If, for example, i = 5:
--i decrements i by 1 then gives you the value of i (4).
i-- gives you the value of i (5) then decrements it by 1.
Both will give you the same result in a for loop.