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:
- 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){ ... }
- When you need to get class literals
Class
- clazz = List.class;
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 null. So, 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!
ReplyDeleteWe are dealing in wide range of spy cameras of topmost quality on lowest prices. Contact us at +91 9999332499 Spy Camera in Delhi
Thanks for sharing such a amazing post
ReplyDeleteSearch and Rescue Equipment
Amazing post..Thanks for sharing.
ReplyDeleteBuy property online india
Click here Happy New Year 2k21 Offers grab Opportunity with 25% Off ALL Products
ReplyDeletebuy Xanax online overnight
buy zopiclone online overnight
Thanks for this post!
ReplyDeleteBuy Mtp Kit online to end early pregnancy.
Pretty nice post. I just stumbled upon your blog and wished to say that I have really enjoyed surfing around your blog posts.
ReplyDeleteAfter all I will be subscribing to your rss feed and I hope you write again soon!
buy zopiclone 7.5 mg tablet
Pretty nice post. I just stumbled upon your blog and wished to say that I have really enjoyed surfing around your blog posts.
ReplyDeleteAfter 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>