The main reason we have Generics is Java is for Type Safety. As a result of type safety, we do not need to type-cast collection objects that are using using generic types.
Now look at the below code carefully,
1 package org.code2care.generics;
2
3 import java.util.ArrayList;
4
5 public class GenericExample {
6
7 public static void main(String[] args) {
8
9 ArrayList<Object> objectList = new ArrayList<>();
10 objectList.add(10);
11 objectList.add(10.20);
12 objectList.add("Sam");
13
14 Integer s = objectList.get(0);
15
16
17 }
18 }
At line number 9 we are creating an ArrayList of generic type Object. In the subsequent lines we are adding data to the list of type Integer, Double and String, there is no issue there.
But at line 14, when we try to get the element from the ArrayList we get a compliation error: "incompatible types: java.lang.Object cannot be converted to java.lang.Integer"
Why! This is like catch-22, you are using generics to avoid ClassCastExceptions during runtime and yet you are trying to hold objects of Object type that can cause a runtime exception. The compiler will not let you do this as it is designed to be type-safe.
Example: ArrayList<Object> and ArrayList<Integer> are two distinct types, even though Integer is a subtype of Object.
Have Questions? Post them here!
- Error: Can not find the tag library descriptor for
- Create a Database Table using JDBC PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- Java Jackson with Maven Example
- [fix] Java JDBC ConnectException: Connection refused
- Spring Boot: Transactions Management with JDBCTemplate Example
- Java Get Current Date for a Specific Time Zone
- What are the 8 Primitive Data Types in Java
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Maven Eclipse (M2e) No archetypes currently available
- How to Sort a LinkedList in Java
- [Fatal Error] XML The markup in the document following the root element must be well-formed.
- Split a String in Java with Examples
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- Truncate table using Java JDBC Example
- Java: Generate random numbers within a range
- Parse XML file in Java using DOM Parser
- How to get Client IP address using Java Code Example
- JDBCTemplate Querying Examples with Spring Boot 3
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- String Boot + Redis - SET and GET String Commands Examples
- Setting up Spring Boot 3 + Maven + MySQL + JDBC Example
- Spring Boot: JdbcTemplate Update Query With Parameters Example
- Java Split String by Spaces
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Fix Microsoft Windows Update Error 80072EFE - Microsoft
- Notepad++ do not show CRLF characters - NotepadPlusPlus
- How to install Python Specific version (3.8, 3.9 or 3.10) using Brew - Python
- Python Comments Multiple Lines - Python
- Make Notepad++ the default App for .txt file extensions - NotepadPlusPlus
- Uber and UberEats App down - issues with network, login, navigation, fare price, orders - News
- How to Display Excel Spreadsheet on SharePoint site - SharePoint
- Change default language highlighting in Notepad++ - NotepadPlusPlus