đ§ 05. Surprised by Javaâs Precision: Type Conversion and Calculation Traps
After mastering primitive types, I moved on to "Type Conversion." It sounded complex at first, but the principle is surprisingly intuitive: Itâs all about the size of the "containers." 1. "A Large ...

Source: DEV Community
After mastering primitive types, I moved on to "Type Conversion." It sounded complex at first, but the principle is surprisingly intuitive: Itâs all about the size of the "containers." 1. "A Large Container Holds a Small One" (Promotion) Automatic Type Conversion (Promotion) occurs when a smaller data type is stored in a larger one. Java handles this seamlessly. Size Order: byte < short < int < long < float < double For example, if you put the integer 2 into a double variable, Java automatically saves it as 2.0. The Catch: byte or short cannot be automatically converted to char. Since char is an "unsigned" (positive-only) type, Javaâthe strict perfectionistârefuses to allow it unless itâs 100% certain. 2. "Force It to Fit" (Casting) Sometimes we need to squeeze data from a large container into a smaller one. This is Explicit Conversion (Casting). Itâs like telling Java, "I'll take responsibility, so trim this down and fit it in!" int intValue = 65; char charValue = (char