• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method with Generic Argument

 
Sudharsan Ashwin
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can anyone explain..How is it that passthebuck(listall) is compiling fine? Though in the method definiton it is mentioned as List<String> ?
Just tried a sample program
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

It is because mixing of legacy and non-legacy code is allowed. Why do you think such method should work


In order to avoid breaking the legacy code because of generics, the mixing of legacy and non-legacy can be combined.
Did your program give any warnings?

Hope this helps,
 
Ulrich Vormbrock
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sudharsan,

it's right, you are mixing legacy- with non-legacy code.
Besides, it's up to you to make sure that later, you prevent operations such as "assigning a String to an int", etc.

At runtime, type-erasure comes into play, as you can simply verify decompiling (with cavaj) your class file:


As you can see, the generic in line 22 (<String>) has disappeared!

When I try to compile your class file with "-Xlint" (for example within the vi-Editor), I get the following output:

$ javac -Xlint GenericTypes.java
GenericTypes.java:8: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List listall.add(new String("Hi"));
^
GenericTypes.java:9: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List listall.add(45);
^
GenericTypes.java:10: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.List listall.add(56.90);
^
GenericTypes.java:13: warning: [unchecked] unchecked conversion
found : java.util.List
required: java.util.List<java.lang.String>
passthebuck(listall);
^
4 warnings


Hope this helps!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic