|
| 1 | +/***************************************************************************** |
| 2 | + * PROJECT: Java-101 Comprehensive Programming Course |
| 3 | + * MODULE: Introduction to Java Fundamentals |
| 4 | + * LESSON: 1.4 - Type Conversion & Casting |
| 5 | + * |
| 6 | + * AUTHOR: Dr. Saad Laouadi, Ph.D. |
| 7 | + * CREATED: March 2025 |
| 8 | + * |
| 9 | + * Copyright © 2025 Dr. Saad Laouadi. All Rights Reserved. |
| 10 | + * See LICENSE file for complete terms. |
| 11 | + *****************************************************************************/ |
| 12 | + |
| 13 | +public class TypeConversion { |
| 14 | + // Method to repeat a character n times for creating banners |
| 15 | + public static String repeatChar(char ch, int n) { |
| 16 | + StringBuilder sb = new StringBuilder(n); |
| 17 | + for (int i = 0; i < n; i++) { |
| 18 | + sb.append(ch); |
| 19 | + } |
| 20 | + return sb.toString(); |
| 21 | + } |
| 22 | + |
| 23 | + public static void main(String[] args) { |
| 24 | + String banner = repeatChar('=', 60); |
| 25 | + System.out.println(banner); |
| 26 | + System.out.println(" Type Conversion and Casting in Java "); |
| 27 | + System.out.println(banner); |
| 28 | + |
| 29 | + // 1. Implicit Conversion (Widening) |
| 30 | + System.out.println("1. IMPLICIT CONVERSION (Widening)"); |
| 31 | + System.out.println(" Automatic conversion from smaller to larger data types"); |
| 32 | + System.out.println(" byte → short → int → long → float → double"); |
| 33 | + |
| 34 | + byte byteValue = 100; |
| 35 | + short shortValue = byteValue; // Implicit conversion from byte to short |
| 36 | + int intValue = shortValue; // Implicit conversion from short to int |
| 37 | + long longValue = intValue; // Implicit conversion from int to long |
| 38 | + float floatValue = longValue; // Implicit conversion from long to float |
| 39 | + double doubleValue = floatValue; // Implicit conversion from float to double |
| 40 | + |
| 41 | + System.out.println("\n Example: byte value " + byteValue + |
| 42 | + " is widened through the type hierarchy:"); |
| 43 | + System.out.println(" byte: " + byteValue); |
| 44 | + System.out.println(" short: " + shortValue); |
| 45 | + System.out.println(" int: " + intValue); |
| 46 | + System.out.println(" long: " + longValue); |
| 47 | + System.out.println(" float: " + floatValue); |
| 48 | + System.out.println(" double: " + doubleValue); |
| 49 | + |
| 50 | + System.out.println("\n Note: No explicit casting is required for widening."); |
| 51 | + |
| 52 | + // 2. Explicit Casting (Narrowing) |
| 53 | + System.out.println("\n2. EXPLICIT CASTING (Narrowing)"); |
| 54 | + System.out.println(" Conversion from larger to smaller data types"); |
| 55 | + System.out.println(" double → float → long → int → short → byte"); |
| 56 | + System.out.println(" Requires explicit casting and may lose information"); |
| 57 | + |
| 58 | + double largeDouble = 123456789.123456789; |
| 59 | + float largeFloat = (float) largeDouble; // Explicit cast, precision loss |
| 60 | + long largeLong = (long) largeFloat; // Explicit cast, decimal part loss |
| 61 | + int largeInt = (int) largeLong; // Explicit cast, possible value loss |
| 62 | + short largeShort = (short) largeInt; // Explicit cast, possible value loss |
| 63 | + byte largeByte = (byte) largeShort; // Explicit cast, possible value loss |
| 64 | + |
| 65 | + System.out.println("\n Example: double value " + largeDouble + |
| 66 | + " is narrowed through explicit casting:"); |
| 67 | + System.out.println(" double: " + largeDouble); |
| 68 | + System.out.println(" float (after cast): " + largeFloat + " (precision loss)"); |
| 69 | + System.out.println(" long (after cast): " + largeLong + " (decimal part lost)"); |
| 70 | + System.out.println(" int (after cast): " + largeInt); |
| 71 | + System.out.println(" short (after cast): " + largeShort + " (possible value truncation)"); |
| 72 | + System.out.println(" byte (after cast): " + largeByte + " (significant value loss)"); |
| 73 | + |
| 74 | + // 3. Potential Data Loss Scenarios |
| 75 | + System.out.println("\n3. POTENTIAL DATA LOSS SCENARIOS"); |
| 76 | + |
| 77 | + // Example 1: Overflow |
| 78 | + int bigNumber = 130; |
| 79 | + byte smallByte = (byte) bigNumber; |
| 80 | + System.out.println("\n Example 1 - Overflow:"); |
| 81 | + System.out.println(" int value: " + bigNumber); |
| 82 | + System.out.println(" byte value after cast: " + smallByte); |
| 83 | + System.out.println(" Explanation: 130 exceeds byte's max value (127), so it wraps around."); |
| 84 | + |
| 85 | + // Example 2: Precision Loss |
| 86 | + double preciseValue = 3.14159265359; |
| 87 | + float lessPresiceValue = (float) preciseValue; |
| 88 | + System.out.println("\n Example 2 - Precision Loss:"); |
| 89 | + System.out.println(" double value: " + preciseValue); |
| 90 | + System.out.println(" float value after cast: " + lessPresiceValue); |
| 91 | + System.out.println(" Explanation: float has fewer significant digits than double."); |
| 92 | + |
| 93 | + // Example 3: Truncation |
| 94 | + double valueWithDecimal = 9.9; |
| 95 | + int truncatedValue = (int) valueWithDecimal; |
| 96 | + System.out.println("\n Example 3 - Truncation:"); |
| 97 | + System.out.println(" double value: " + valueWithDecimal); |
| 98 | + System.out.println(" int value after cast: " + truncatedValue); |
| 99 | + System.out.println(" Explanation: Casting from floating point to integer truncates decimal part."); |
| 100 | + |
| 101 | + // 4. Converting Between Primitives and Wrapper Classes |
| 102 | + System.out.println("\n4. CONVERTING BETWEEN PRIMITIVES AND WRAPPER CLASSES"); |
| 103 | + |
| 104 | + // Primitive to Wrapper (Boxing) |
| 105 | + int primitiveInt = 42; |
| 106 | + Integer wrappedInt = Integer.valueOf(primitiveInt); // Explicit boxing |
| 107 | + Integer autoboxedInt = primitiveInt; // Autoboxing (Java does this automatically) |
| 108 | + |
| 109 | + System.out.println("\n 4.1 Primitive to Wrapper (Boxing):"); |
| 110 | + System.out.println(" Primitive int: " + primitiveInt); |
| 111 | + System.out.println(" Explicitly boxed: " + wrappedInt); |
| 112 | + System.out.println(" Autoboxed: " + autoboxedInt); |
| 113 | + |
| 114 | + // Wrapper to Primitive (Unboxing) |
| 115 | + Double wrappedDouble = Double.valueOf(3.14); |
| 116 | + double primitiveDouble = wrappedDouble.doubleValue(); // Explicit unboxing |
| 117 | + double autounboxedDouble = wrappedDouble; // Auto-unboxing |
| 118 | + |
| 119 | + System.out.println("\n 4.2 Wrapper to Primitive (Unboxing):"); |
| 120 | + System.out.println(" Wrapper Double: " + wrappedDouble); |
| 121 | + System.out.println(" Explicitly unboxed: " + primitiveDouble); |
| 122 | + System.out.println(" Auto-unboxed: " + autounboxedDouble); |
| 123 | + |
| 124 | + // String conversions |
| 125 | + System.out.println("\n 4.3 String Conversions:"); |
| 126 | + |
| 127 | + // String to primitive |
| 128 | + String numberString = "123"; |
| 129 | + int parsedInt = Integer.parseInt(numberString); |
| 130 | + double parsedDouble = Double.parseDouble("456.789"); |
| 131 | + |
| 132 | + System.out.println(" String to primitive:"); |
| 133 | + System.out.println(" String value: \"" + numberString + "\""); |
| 134 | + System.out.println(" Parsed int: " + parsedInt); |
| 135 | + System.out.println(" Parsed double: " + parsedDouble); |
| 136 | + |
| 137 | + // Primitive to String |
| 138 | + int number = 789; |
| 139 | + String convertedString = Integer.toString(number); |
| 140 | + String formattedFloat = String.format("%.2f", 3.14159); |
| 141 | + |
| 142 | + System.out.println("\n Primitive to String:"); |
| 143 | + System.out.println(" Int value: " + number); |
| 144 | + System.out.println(" Converted to String: \"" + convertedString + "\""); |
| 145 | + System.out.println(" Formatted float: \"" + formattedFloat + "\""); |
| 146 | + |
| 147 | + System.out.println(banner); |
| 148 | + } |
| 149 | +} |
0 commit comments