Wednesday, November 13, 2019

Java List vs List<?> vs List<Object>

Before generics were introduced developers used lists as List which means you can store any type of objects including null inside it and when you read it you need to cast it
List list = new ArrayList();
list.add("String test");
list.add(new Date());

String s = (String) list.get(0);
From code above you can expect ClassCastException because if you try to assign 2nd item to String it will not succeed and you will get an exception during runtime. To prevent it you need to check if your type is correct and then cast as below

if (list.get(0) instanceof String) {
   String s = (String) list.get(0);
}
So, after Java 5.0 generics were introduced. However, still you can define the raw form of a list (List without diamond operator) which is not good practices and it still exists in Java because of backward compatibility. There are 2 places where you can(forced) to use it:

  1. When you check for a type using instanceof. Because instanceof doesn't work with diamond operators (during runtime those operators are not existing). 
    if (list instanceof List){
       ...
    }
    
  2. When you need to get class literals
    Class clazz = List.class;
    
So, except above 2 places never and never use a raw type of List and prefer generic version.
Now, let's assume that we have a method that consumes List and prints its values as an example. How your method should look like?
Version 1:
private void printMe1(List<Object> list){
   System.out.println(list);
}
Version 2:
private void printMe2(List<?> list){
   System.out.println(list);
}
If you choose the first version then you can't pass List<String> to that method because List<String> isn't a subclass of List<Object>. But, in case of 2nd option, you can do it because List<?> means List of Unknown. So here List<?> wins. 

The second case is, if you create your list as List<Object> then you can put any type of data in it (integer, string and etc.) of course which means you are not type-safe again. But in case of List<?> you should create your list with a parameterized type (e.g. List<Integer> list...) which means you are safe while putting or getting values. 

And additionally, inside your method, you can't put any data in it except nullSo, List<?> can only accept null value insertion which gives additional safety to you. e.g. below code won't compile:
private void printMe2(List<?> list){
   list.add("in method");
}
Again List<?> wins, prefer to use 2nd version :)

If you want to go deep and master Java including Generics and looking for a cheap + effective course I'd strongly recommend Java Programming Masterclass for Software Developers which is already taken by 300k+ students and has a great rating.

Thanks for reading!

7 comments:






  1. We are dealing in wide range of spy cameras of topmost quality on lowest prices. Contact us at +91 9999332499 Spy Camera in Delhi

    ReplyDelete
  2. Click here Happy New Year 2k21 Offers grab Opportunity with 25% Off ALL Products

    buy Xanax online overnight
    buy zopiclone online overnight

    ReplyDelete
  3. Thanks for this post!
    Buy Mtp Kit online to end early pregnancy.

    ReplyDelete
  4. Pretty nice post. I just stumbled upon your blog and wished to say that I have really enjoyed surfing around your blog posts.
    After all I will be subscribing to your rss feed and I hope you write again soon!
    buy zopiclone 7.5 mg tablet

    ReplyDelete
  5. Pretty nice post. I just stumbled upon your blog and wished to say that I have really enjoyed surfing around your blog posts.
    After all I will be subscribing to your rss feed and I hope you write again soon!<a href="https://newsmozi.com/>free guest post site list 2020-21</a>

    ReplyDelete