• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Benefits of Increment Operator

 
Leon Omk
Ranch Hand
Posts: 75
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel quiet uneasy with this "feature"(increment operator). i=i+1, that is all we need. And i++, ++i just mess Java up.

About benefits, well, I can't come up with a reasonable one. Anyone can help?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is easy to read, easy to write and if used properly easy to comprehend.
 
Nico Van Brandt
Ranch Hand
Posts: 70
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) It's easy to read and it's fast to write
b) Java has great flexability with the use of ++i and i++ since they are far from the same. If you don't know the difference you should look it up!
 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it only "messes Java up" if used without thought, like the following examples:
If you avoid using them within other statements, so they will only be their own statements, there is nothing wrong with that. If you do use them within other statements you have to know what you're doing.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15459
42
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The pre-increment and post-increment operators ++i and i++ are a feature that Java inherited from C and C++.

I guess these operators existed in C and C++ because some microprocessors have instructions for exactly these operations. A compiler has an easy job when it finds one of these operators in your source code, because it can then just convert the code to those machine language instructions directly. I think any modern C or C++ compiler nowadays would be smart enough to also optimize a statement like i = i + 1 to an efficient, single machine language instruction, but really old C compilers of the 1970's might not have such optimizations built-in.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you get used to it, i++ is easier to read that i = i + 1. It's especially useful I think, when iterating through a set (as in a for loop), where it matches the real meaning of what you're trying to do - "move on a step", rather than "add something to this number".

It's like having a couple of methods attached to your number: add(int i), and increment(). add(int) is more flexible, but increment() is more specific, and therefore more meaningful.

Personally, I'd try to avoid using i++ and ++i in any context where the difference matters. Even though I understand the difference I'd probably have to pause to work out what was happening - and the next person to use the code might be less familiar.
 
anirudh jagithyala
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree.

Further (Adding to the explanation which i came across in my early days when i was learnin C++)

if we observe


i=i+1;--> 2 operations i.e, mathematical addition operation and then assignment operation.

i++ or ++i ----> only one operation i.e, increment
I guess these operators existed in C and C++ because some microprocessors have instructions for exactly these operations. A compiler has an easy job when it finds one of these operators in your source code, because it can then just convert the code to those machine language instructions directly. I think any modern C or C++ compiler nowadays would be smart enough to also optimize a statement like i = i + 1 to an efficient, single machine language instruction, but really old C compilers of the 1970's might not have such optimizations built-in.




This number of operations also effect the processing time, processing efficiency and also the complexity,
We would be more clear if we think the same in view of cpu processing.

This is also one of the reason but is not the only reason.. as there may be many other factors effecting the processing capabilities....

 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anirudh jagithyala wrote:i++ or ++i ----> only one operation i.e, increment

Correct.

I've called "javap -c" on the class file generated for the following little piece of code. (The System.out.println statements are there to separate the statements.)
The output:
i = i + 1 needs 4 operations: iload_1, iconst_1, iadd and istore_1. The other three, i += 1, i++ and ++i all need just 1 operation, iinc 1, 1.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ++ and -- operators are useful when you are iterating arrays. You can add to the top of an array like this . . . which is how java.util.ArrayList both adds elements at the end of the list and keeps track of its size simultaneously.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:I think any modern C or C++ compiler nowadays would be smart enough to also optimize a statement like i = i + 1 to an efficient, single machine language instruction, but really old C compilers of the 1970's might not have such optimizations built-in.


... or javac, it seems!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15459
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. But I'm actually not surprised that javac doesn't optimize this.

On the JVM, javac does very little optimization - it leaves almost all optimization to the just-in-time compiler that converts bytecode to native machine code. The reason for that is that the JIT knows much more about the exact computer that the program it running on, so it can optimize much better than javac at compile time, and if javac would do too much optimization at compile time, it could even make the JIT's job harder.

You'd say that javac would be able to optimize simple things like this, but apparently it doesn't.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic