• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubts in Generics: Why dont this throw exception?

 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Gen<G>
{
G g;
Gen(G g)
{
this.g =g;
}
public static void main(String[] args)
{
Gen<String> arr[] = new Gen[5]; //line 1
arr[0] = new Gen("Java"); //line 2
arr[1] = new Gen(1); //line 3
arr[2] = (Gen<String> new Gen(1); //line 4

for(Gen o:arr)
{
System.out.println(o);
}
}
}

I have confusion in above code.Line 1 says that arr[] is of type <String>.

Then how are you able to create a Gen object with value 1(line 3) which is an Integer.


ALSO if there is line of code , say

List ls=new ArrayList<Number>();
then 1)is above list ls typesafe?
2)can we add object of anytype to ls(like we used to do in 1.4)or are their any restrictions
[ December 21, 2007: Message edited by: Maan Shenoy ]

[ December 21, 2007: Message edited by: Maan Shenoy ]

[ December 22, 2007: Message edited by: Maan Shenoy ]

[ December 22, 2007: Message edited by: Maan Shenoy ]
[ December 22, 2007: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers!

The problem is that you cannot really make an array of generic types.
So you can not say:
Gen<String> arr[] = new Gen<String>[5]; // doesn't compile.


Therefore in your code (that compiles with warnings) you wrote:
Gen<String> arr[] = new Gen[5]; //line 1


The array variable is of type Gen<String>, but the element objects stored into the arrays are (and must be) raw types like if you said
Gen x = new Gen(1); // unparametrised.


These individual element variables aren't parameterized, but you can assign parameterized Gen-objects to these like
Gen x = new Gen(1);
x = new Gen("str");



So your lines 2-4 do compile.


The fact that your array is of type Gen<String> only makes safe that the elements in the array will be casted automatically so you can say in your main method:
String s = arr[0].g;

without casting.
If the array was not parameterized with String this would not work because then arr[any].g would be an Object.
However this behaviour doesn't help much because there can be any Gen<Thing> in the array as already shown.
If you do the same with arr[1].g you would get a class cast exception.


The baseline is just: don't use arrays together with generics.

Perhaps you could also have a look at this:
http://java.sun.com/docs/books/tutorial/extra/generics/fineprint.html

Yours,
Bu.
 
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic