Reason for (post/pre) increment operator in Java or C#

Recently, I’ve stumbled across question about predicting the output of code which heavily uses post/pre increment operators on integers. I am experienced C programmer, so I felt like at home, but people made statements that Java just blindly copied that from C (or C++), and that this is an useless feature in Java.

That being said, I am not able to find good reason for that (especially with keeping in mind the difference betwen post/pre forms) in Java (or C#), because it’s not like you will manipulate arrays and use them as strings in Java. Also, it was long ago when I last looked into bytecode, so I don’t know if there’s a INC operation, but I don’t see any reason why this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>for(int i = 0; i < n; i += 1)
</code>
<code>for(int i = 0; i < n; i += 1) </code>
for(int i = 0; i < n; i += 1)

could be less effective than

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>for(int i = 0; i < n; i++)
</code>
<code>for(int i = 0; i < n; i++) </code>
for(int i = 0; i < n; i++)

Is there any particular part of the language where this is really useful, or is this just a feature to bring C programmers in town?

11

is this just a feature to bring C programmers in town

It is surely a feature “to bring C (or C++) programmers in town”, but using the word “just” here underestimates the value of such a similarity.

  • it makes code (or at least code snippets) easier to port from C (or C++) to Java or C#

  • i++ is less to type than i += 1, and x[i++]=0; is much more idiomatic than x[i]=0;i+=1;

And yes, code which heavily uses post/pre increment operators maybe hard to read and maintain, but for any code where these operators are misused, I would not expect a drastic increase in code quality even when the language would not provide these operators. That’s because if you have devs which don’t care for maintainability, they will always find ways to write hard-to-understand code.

Related: Is there any difference between the Java and C++ operators? From this answer one can deduce that any well-defined operator behaviour in C is similar in Java, and Java does only add some definitions for operator precedence where C has undefined behaviour. This does not look like coincidence, it seems to be pretty intentional.

5

I use sometimes the postfix increment in return statements. E.g. consider this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>//Java
public class ArrayIterator implements Iterator<T> {
private final T[] array;
private int index = 0;
public ArrayIterator(T ... array) {
this.array = array;
}
public T next {
if (! hasNext()) throw new NoSuchElementException();
return array[index++];
}
...
}
</code>
<code>//Java public class ArrayIterator implements Iterator<T> { private final T[] array; private int index = 0; public ArrayIterator(T ... array) { this.array = array; } public T next { if (! hasNext()) throw new NoSuchElementException(); return array[index++]; } ... } </code>
//Java
public class ArrayIterator implements Iterator<T> {
   private final T[] array;
   private int index = 0; 
   public ArrayIterator(T ... array) {
      this.array = array;
   }

   public T next {
      if (! hasNext()) throw new NoSuchElementException();
      return array[index++];   
   }
   ...
}

You can’t do this with index += 1. So at least the postfix version can save you now and then some lines.

That said, these operators are indeed not necessary, and may confuse people. I know Scala decided against these operators (you have only += and -=, as you suggested), and due to the more “high-level” approach I don’t really miss them there. I hope Java develops (more slowly) in the same direction.

However Java has a bunch of other operators you don’t see very often “in the wild” (did you ever use ^= or >>>= ?), and nobody cares.

1

I’ve built many languages over the years, mostly special-purpose.
What happens is you have a large audience, and they all have expectations.
In considering each feature, you have to weigh the benefit of the feature against the cost of possibly violating expectations.

One that I personally had to backpedal on was integer division, which normally truncates downward, right?
Like 3 / 2 = 1.5 = 1, right?
Well, way back in the early days of Fortran, somebody decided -3 / 2 = -1.5 = -1 (not -2).
In other words, if the dividend is negative and the divisor is positive, it should truncate up.
Note what this implies – in that case the remainder is negative.
That violates the usual assumption that remainder = modulus.
If you’re doing graphics, with integer coordinates, this can lead to all kinds of trouble.
SO, in the language I simply had integer division truncate downward all the time, and say so in the doc.

And SO, guess what happened? All h*** broke loose. The language had a bug in integer division!

It did no good to say the old way was broken.
It was easier to change it than to argue it.

So to answer your question, in languages like C# and Java, it’s simply easier to include the ++ operators (which I personally like) than to explain why not.

7

Yup, its nothing important in itself, its only benefit is the syntactic sugar that makes it easier to type.

The reason it is in C in the first place is solely because the PDP-11 that was the basis for C, way back in the old days, had a special instruction for incrementing by 1. In those days, using this instruction was important to get more speed out of the system, hence the language feature.

However, I think people like the command so much they would complain if it wasn’t there.

1

This is idiomatic syntax in C for many situations (such as the for loop you cited), so as others have said, it can help those transitioning to Java as a new language and makes it easier to port pre-existing C-style code. For those reasons, including it was a natural decision to make in Java’s early days to speed adoption of the new language. It makes less sense now, because the more compact code comes at the expense of requiring programmers to learn syntax that isn’t strictly necessary.

Those who like it find it both convenient and natural. Doing without it seems awkward. Using it gains no efficiency, it’s a matter of readability. If you think your programs are easier to read and maintain using these operators, use them.

The original sense in C was more than just “add 1” or “subtract 1”. It’s much more than just syntactic sugar or small performance gains. The operator in C means “increment (or decrement) to the next item”. If you have a pointer to something larger than one byte, and you step to the next address as in *pointer++, the actual increment might be by 4, or 8, or whatever is required. The compiler does the work for you, keeping track of the size of the data items and skipping the right number of bytes:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>int k;
int *kpointer = &k;
*kpointer++ = 7;
</code>
<code>int k; int *kpointer = &k; *kpointer++ = 7; </code>
int k;
int *kpointer = &k;
*kpointer++ = 7;

On my machine, this adds 4 to kpointer, it does not add 1. This is a real help in C, preventing errors and saving spurious invocations of sizeof(). There is no equivalent pointer arithmetic in Java, so this isn’t a consideration. There, it’s more about the power of expression than anything else.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật