Type Casting

TLDR: Type casting is the process of converting a variable from one data type to another, either explicitly or implicitly, depending on programming language rules. It ensures compatibility between different types while maintaining type safety. Type casting is widely used in programming to perform operations on variables or to fit them into a specific context, such as combining primitive and complex data types.

https://en.wikipedia.org/wiki/Type_conversion

In Java, type casting is categorized as implicit (widening) or explicit (narrowing). Implicit casting occurs automatically when converting a smaller data type, like `int`, to a larger type, like `double`. Explicit casting requires manual intervention, such as converting a `double` to an `int`, using syntax like `(int) 3.14`. Implicit casting is safe because it does not lose data, whereas explicit casting may lead to data loss or precision errors, requiring careful implementation.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Type casting is also applied to objects in object-oriented programming, particularly in Java when working with inheritance and polymorphism. For example, an `Animal` object can be cast to a `Dog` if the instance is indeed a `Dog`. However, such casts must be validated using `instanceof` to prevent runtime errors. Proper usage of type casting improves flexibility and allows for dynamic operations across diverse data types, enhancing functionality while preserving type integrity.

https://docs.oracle.com/javase/specs/jls/se20/html/jls-5.html