aspose file tools*
The moose likes Mock Exam Errata and the fly likes JQ+    Arrays[0]++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "JQ+    Arrays[0]++" Watch "JQ+    Arrays[0]++" New topic
Author

JQ+ Arrays[0]++

Dominic Mack
Greenhorn

Joined: Sep 26, 2001
Posts: 17

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

Joined: Sep 22, 2001
Posts: 2579

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

Joined: Sep 26, 2001
Posts: 17
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

Joined: Sep 16, 2001
Posts: 139
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).]
 
Consider Paul's rocket mass heater.
 
subject: JQ+ Arrays[0]++