java.lang.ClassCastException is a Runtime Exception in Java and hence the Java Compiler will not check for it.
Below is an example of code that generate a ClassCastException:
import java.util.ArrayList;
import java.util.List;
public class ClassCastExceptionExample {
public static void main(String[] args) {
List list = new ArrayList();
list.add(10);
list.add("Hello");
String str = (String) list.get(0); //ClassCastException
}
}
Here we have a list as a raw type so we can add Objects of any types without any issues, but it becomes really difficult to know what elements will the list return, this if you try to cast the element incorrectly we get a ClassCastExceptionExample at runtime.
Fix or ClassCastExceptionExample
It is always recommended to make use of Java Generics that were introduced with Java 1.5 instead of raw types. By using Generics you add type safety to your code and thus you do not need to do casting.
public static void main(String[] args) {
List<String> list = new ArrayList<>();
//list.add(10); //incompatible types: int cannot be converted to java.lang.String
list.add("Hello");
String str = (String) list.get(0);
}
Instead you will get a Complication Error saying "incompatible types: int cannot be converted to java.lang.String". Thus you can avoid runtime exceptions.

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]
- Change Title text for Android Activity using java code - Android
- How to make a div tag clickable - Html
- Skip Test Maven while creating package command - Java
- Windows 10 now has a new enhanced Calculator with a new icon - News
- SharePoint Server 2016 error Microsoft Office Online Server 2016 cant be installed on the same machine as a Microsoft SharePoint Server product - SharePoint
- How to make macOS Terminal window Transparent (or Opaque) - MacOS
- Check SSH/OpenSSH version Command - Linux
- List of All Apple Silicon ARM Based M1/M2 Mac [updated Oct 2022) - MacOS