This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Originally posted by Dominic Mack: I thought arrays were objects and the ++ operator only worked with primatives... so why can you use the ++ operator on a array object element?
Array is an object, but 0<sup>th</sup> element of the array is an int, which is a primitive. The unary operator ++ works on this int value, which happens to be the 0<sup>th</sup> element of an array. HTH, - Manish
[This message has been edited by Manish Hatwalne (edited October 12, 2001).]
Joined: Sep 26, 2001
If the unary operator works on an int like you said, and go() returns an int, why can't I do this??? int i = this.go()++; [This message has been edited by Dominic Mack (edited October 12, 2001).]
postfix or prefix increment works for variables only. An element of array is evaluated to a "variable", while a call to a method is evaluated to what is returned. Since go() returns an int "value", you can't apply ++ to it. Here is a quote from JLS:
JLS 15.13 - The result of an array reference is a variable of type T, namely the variable within the array selected by the value of the index expression. JLS 15.14.1 - A postfix expression followed by a ++ operator is a postfix increment expression. The result of the postfix expression must be a variable of a numeric type, or a compile-time error occurs.
[This message has been edited by Nain Hwu (edited October 13, 2001).]