Posts

Showing posts with the label Type Conversion

Type Conversion and Casting in Java and Kotlin

Type conversion and casting define how one data type is converted into another. This topic is critical for interviews, JVM understanding, and bug-free coding , especially when working with primitives, wrappers, and mixed-type expressions. Table of Contents Type Conversion Basics Implicit Conversion Explicit Casting Kotlin Type Rules Java and Kotlin Interview Questions Conclusion 1. Type Conversion Basics Type conversion is the process of converting one data type into another. It usually happens when assigning values or performing operations. There are two main types: Implicit (Widening) – automatic Explicit (Narrowing) – manual casting 2. Implicit (Widening) Conversion Implicit conversion happens when a smaller data type is converted to a larger one. There is no data loss . Java Example int a = 10; long b = a; // int → long double c = b; // long → double Kotlin Example val a: Int = 10 val b: Long = a.toLong() val c:...