Java Modifying Elements in a foreach

You can’t do that in a foreach loop. for (int i=0; i<copyArray.length;i++) copyArray[i] /= 2; Else you are not assigning it back into the array. Integer objects are immutable by the way so can’t modify them (creating new ones though). Updated from comment: Beware though that there are a few things going on, autoboxing/unboxing for … Read more

Why doesn’t autoboxing overrule varargs when using method overloading in Java 7?

I guess it’s related to bug #6886431, which seems to be fixed in OpenJDK 7 as well. The problem is that JLS 15.12.2.5 Choosing the Most Specific Method says that one method is more specific than another one when types of formal parameters of the former are subtypes of formal parameters of the latter. Since … Read more

Does autoboxing call valueOf()?

I first tought your question was a dupe of What code does the compiler generate for autoboxing? However, after your comment on @ElliottFrisch I realized it was different : I know the compiler behaves that way. I’m trying to figure out whether that behavior is guaranteed. For other readers, assume that “behaves that way” means … Read more

How do I convert Double[] to double[]?

If you don’t mind using a 3rd party library, commons-lang has the ArrayUtils type with various methods for manipulation. Double[] doubles; … double[] d = ArrayUtils.toPrimitive(doubles); There is also the complementary method doubles = ArrayUtils.toObject(d); Edit: To answer the rest of the question. There will be some overhead to doing this, but unless the array … Read more