0

Does creating an array of generic objects, in this fashion, has any downsides or performance issues?

QueryObjects instances[] = (QueryObjects<String,String,String>[]) new Object[10];

Edit: I was even thinking if I could instead rely on this:-

QueryObjects instances[] = new QueryObjects[10];

The primary reason for this being, I do not want to fix the generic to <String,String,String> because sometimes it can also be like <Integer,Integer,Integer> for some elements of array. So I would like to give it as a runtime choice. Since this is mostly for application's internal work and not for any client side input, perhaps I should not be facing the danger of wrong inputs.

Rajat Gupta
  • 23,343
  • 56
  • 165
  • 281

4 Answers4

2

Creating it in this way does have a downside: you now have what is really an array of any kind of Object disguised as an array of QueryObjects. If someone else has a reference to the same array (and know that it is an Object[]), they could put some Object into it, e.g. a String, and then the program will throw a ClassCastException when you try to access that String through your reference to the array. Solution: create it like this:

QueryObjects<String, String, String> instances[] = new QueryObjects[10];
Aasmund Eldhuset
  • 35,093
  • 4
  • 61
  • 79
  • 3
    -1 Can't do either of these in Java. Both give you a compile error: `Cannot create a generic array of QueryObjects` – Jonathon Faust Feb 24 '11 at 18:44
  • @Jonathon My bad - I've been programming too much C#. At any rate, it was sloppy to provide untested code; sorry and thanks for pointing it out. My updated solution _is_ tested... (My argument about `ClassCastException` is still correct, though; that I _did_ test...) – Aasmund Eldhuset Feb 24 '11 at 18:50
  • Vote reversed. That will work, assuming you don't mind unchecked warnings. Java generics are pretty bad :( – Jonathon Faust Feb 24 '11 at 18:52
  • @Jonathon Agreed. I'll take an "unchecked conversion" warning over a possible `ClassCastException` any day, though... ;-) (Of course, you always risk that exception when using generics in Java, but at least you need to work harder to produce it...) – Aasmund Eldhuset Feb 24 '11 at 18:54
2

Your code will generate a ClassCastException at run time.

You should create an array of type QueryObjects[] and cast it to QueryObjects<String, String, String>[], or in the future, if you need to create an array of a variable type (e.g., T[]), use reflection: How to create a generic array in Java?

Community
  • 1
  • 1
ide
  • 17,118
  • 4
  • 56
  • 101
2

the "righteous" way is

QueryObjects<?,?,?>[] instances = new QueryObjects<?,?,?>[10];

the raw way is just fine:

QueryObjects[] instances = new QueryObjects[10];

notice the type declaration:

Type[] var

not

Type var[]

I don't care why Java allows the 2nd syntax, it still doesn't make any sense.

irreputable
  • 42,827
  • 9
  • 59
  • 89
1

It begs the question "If you're using generics, why use an array instead of an a typed structure?"

Instead of trying to achieve QueryObjects[] and doing it poorly, why not create a type for it? Then you can add whatever methods you want. Yay object orientation!

class QuerySet<O extends QueryObjects<A,B,C>> {
    O[] objects;
}
corsiKa
  • 76,904
  • 22
  • 148
  • 194
  • As a side note, I might have a syntax error on the `>`. I don't have the time to test it for syntactical accuracy at the moment. – corsiKa Feb 24 '11 at 19:08
  • Thanks glocoder! I just tried it out..thid syntax seems to work `class QuerySet {` `O[] object;` `}` Can this fit with my needs that I mentioned in the edits.. like some elements of this array could be whereas other elements could be like . – Rajat Gupta Feb 24 '11 at 19:38
  • If the behavior is the same, then yes, it can. If the behavior is different depending on Integer or String, you shouldn't use a generic. Think about what generic means! – corsiKa Feb 24 '11 at 21:06