File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Benefits of Increment Operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Benefits of Increment Operator" Watch "Benefits of Increment Operator" New topic
Author

Benefits of Increment Operator

Leon Omk
Ranch Hand

Joined: Aug 17, 2010
Posts: 75

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?


OK, so that other guy knows Java better than I do, but I bet he can't speak Wuhanese(a Chinese Dialect) like me.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

It is easy to read, easy to write and if used properly easy to comprehend.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Nico Van Brandt
Ranch Hand

Joined: Mar 31, 2011
Posts: 66

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!


Oracle Java SE6 Certified Programmer
Oracle Java EE5 Certified Web Component Developer
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4390
    
    8

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

Joined: Dec 07, 2010
Posts: 41
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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

Joined: Oct 13, 2005
Posts: 38844
    
  23
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

Joined: Jan 06, 2011
Posts: 441

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

Joined: Aug 16, 2005
Posts: 14150
    
  18

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Benefits of Increment Operator