Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
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
Forum:
Programmer Certification (OCPJP)
why doesnt this compile?
Ankur kothari
Ranch Hand
Posts: 531
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
public <T> List<T> meth(List<?> type) { System.out.println(type); // 1 return new ArrayList<String>(); // 2 }
return type is list..and am returning an arraylist?
this is from inquistion? is it related to <
String
>
rohan yadav
Ranch Hand
Posts: 156
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
List<> is superclass of ArrayList<>, hence we can return ArrayList<>. So thats why it compiles
Sage of The Monstrous Toad of Mount Myoboku
Ankur kothari
Ranch Hand
Posts: 531
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
it doesnt compile.......
rohan yadav
Ranch Hand
Posts: 156
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
may be this will help you
public <T> List<T> meth(List<?> type) { System.out.println(type); // 1 return (List<T>) new ArrayList<String>(); // 2 }
Sage of The Monstrous Toad of Mount Myoboku
Edwin Dalorzo
Ranch Hand
Posts: 961
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The code does not compile because the expected return type is List<T>, but you are returning List<String>.
You cannot guarantee that List<String> is of type List<T> at compile time, therefore, the compiler generates an error.
Ankur kothari
Ranch Hand
Posts: 531
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks Rohan and Edwin....understood the concept
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
List ? vs List T
Difference between the code snippets
how can I add some stuff in returned List?
K & B Mock exam Generics doubt
Genericss
More...