• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JQ+ Arrays[0]++

 
Dominic Mack
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I thought arrays were objects and the ++ operator only worked with primatives... so why can you use the ++ operator on a array object element?
 
Manish Hatwalne
Ranch Hand
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Dominic Mack
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic