+ 27

Why using Raw types in java is not recommended?

Actually I get a warning when I use them. for example: List a = new ArrayList();

23rd May 2018, 6:13 PM
Amir
Amir - avatar
4 Respostas
+ 5
It's considered unsafe because literally any object can go in there, which can result in unexpected behavior to say the least, but since getting any object could be what you want, that's the reason why it's just a warning. Consider that you could specify the type of object you want, so that no one can provide anything outside that object, avoiding potential security issues as well. Hope this is answers Amir.
23rd May 2018, 8:20 PM
Roberto Guisarre
Roberto Guisarre - avatar
+ 6
Exactly as Roberto said. On the top of that i would ask: "why they have created generics?". :) think about it. Your question is basically before Java 5. Perfect answer would be to copy one page from DocsOracle, one about raw type itself.
24th May 2018, 11:07 PM
Jakub Dubański
Jakub Dubański - avatar
+ 2
This sounds like an arraylist defaulting into E which is the generic object type when it is not assigned to a type/class. Arrays are suppose to be defined such as STRING etc. When not defined anyone can put anything in/out and compromise the script.
25th May 2018, 8:02 AM
Apple Blossom
Apple Blossom - avatar
- 1
Hy
25th May 2018, 7:44 AM
gohar iqbal
gohar iqbal - avatar