Aug 14

This post is also available in: German

Let’s go even deeper in the tricks of generics in methods and change the definitions therefore a little bit:

List <SalesPerson> a = new ArrayList <SalesPersonr>();
a.add(new
SalesPerson());
a.add(new
SalesPerson());

SalesPerson i [] = {new SalesPerson(), new SalesPerson ()};

The methods remain unchanged:

public static void checkStatus_array (Employee[] a){ … }
public static void checkStatus_list (List<Employee> a){ … }

Also here we are calling the appropriate functions :+

checkStatus_list(a); // CompilerError
checkStatus_array(b); // compiling and running without problems

In both cases “SalesPerson” is transfered and type “Employee” taken over. For Arrays it is working smoothly, but not for List; a cast does not take place. Therefore always the exact object-type need to be transfered.  In contradiction to assignments a explizit cast is not helpful in this case.

 

  • Print
  • del.icio.us
  • Facebook
  • Twitter
  • Google Bookmarks
  • Add to favorites
  • RSS
  • Blogosphere
  • email
  • PDF
  • Wikio
  • Technorati
  • Digg
  • DZone

Leave a Reply

preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs