The type of int. Class is ClassYou cannot handle primitive array types generically; so you either have to have your method just return type Object or you have to make separate methods for reference types and for primitive types.
The type of int. Class is Class, so genericArrayNewInstance() would be inferred to return a Integer. But the function actually creates an int, so it would have a class cast exception when it is returned.
Basically, the cast to T inside the function is not legitimate in this case, because int is not a T (primitives can't be used in type variables). You cannot handle primitive array types generically; so you either have to have your method just return type Object, or you have to make separate methods for reference types and for primitive types.
I could be wrong but I think that technically int doesn't have a class but when you use int. Class the compiler boxes int to Integer – matt be Mar 16 '10 at 10:37 3 @matt-b, int. Class is a real Class.
Check the javadoc for Class.isPrimitive. It's been around since 1.1. Well before boxing.Integer. Class!
= int.class.Integer. TYPE == int.class. – nicerobot Mar 16 '10 at 11:50 "The type of int.
Class is Class" -- I think herein lies the key – polygenelubricants Mar 18 '10 at 15:13.
A few points: primitives are autoboxed to their object counterparts (wrappers) when needed primitive arrays are objects, so they are not autoboxed. Generics cannot use primitives as type parameters For your examples, here are my assumptions: in 3 the autoboxing happens to the type parameter, but doesn't happen to the returned array in 4 the autoboxing happens to the type parameter, but doesn't happen to the method argument, so in fact an int is generated, but Integer is expected Autoboxing in the case of type parameter might not be exactly autoboxing, but is something with the same idea. Update: your 2nd example has nothing wrong.Int.
Class is a Class, so the compiler has no reason to reject it.
These are all good points but they don't answer my question. Also, there is no autoboxing in my example. If you print componentType in genericArrayNewInstance, it prints the passed .
Class objects as it should. – polygenelubricants Mar 18 '10 at 14:33 @polygenelubricants that's what I said - autoboxing doesn't happen to the method argumente - it has happened (well, it might not be exactly autoboxing, but something with similar idea) to the type parameter. The type parameter cannot be primitive, hence the wrapper is used.
– Bozho Mar 18 '10 at 14:53 "your 2nd example has nothing wrong" -- but my generic type specification mandates that T extends Number, where as int. Class extends null. See also update to Q.
– polygenelubricants Mar 18 '10 at 15:07 that fits exactly in my explanation about autoboxing type parameters but not autoboxing the method arguments. I still don't see a problem. – Bozho Mar 18 '10 at 15:49.
I agree with the original poster. This is crazy. Why can't I use primitive with generic?
This may not be compiler problem, but it's the language's issue. Simply wrong to skip primitive types from generic. For this: intArray = (int) Array.
NewInstance(int. Class, 0); int. Class is just a Class object.So it's ok to pass over.
"int" is a type, so it's not ok because it's clearly primitive. Not to say that's the "best" way to create the language, just to adhere to the language. This is so crazy that I can't create a wrapper for memory (array) allocation of primitives using generic.
If using objects, that is so bloat for a huge collections that is wasteful. People who created the Java language/machine clearly have a bit limit in their brain. They can do it wrong the 1st time, but fixing it takes a decade, and not doing it right.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.