π Type Casting Kya Hai?
Ek data type ki value ko doosre data type mein convert karna.
int a = 100; double b = a; β int ko double mein daal diya = TYPE CASTING
Memory mein sizes:
Deep Explanation: Widening matlab chote container ki cheez bade container mein daalna β automatically hota hai, kuch likhna nahi padta.
π₯ Glass (int) β πͺ£ Bucket (double) β automatically fit ho jaata hai
int x = 100; double y = x; β compiler khud karta hai Java sochta hai: "int β double safe hai, karo"
β
Safe β Data loss NAHI hoga (mostly)
β
Automatic β Implicit kehte hain isliye
public class Widening1 { public static void main(String[] args) { byte b = 10; short s = b; // byte β short (automatic) int i = s; // short β int (automatic) long l = i; // int β long (automatic) float f = l; // long β float (automatic) double d = f; // float β double (automatic) System.out.println("byte value: " + b); System.out.println("short value: " + s); System.out.println("int value: " + i); System.out.println("long value: " + l); System.out.println("float value: " + f); System.out.println("double value: " + d); } }
public class Widening2 { public static void main(String[] args) { int marks = 95; double percentage = marks; // int β double automatic System.out.println("Marks (int): " + marks); System.out.println("Percentage (double): " + percentage); int a = 10; int bVal = 3; double result = a / bVal; // β pehle int/int = 3, phir 3.0 double result2 = (double) a / bVal; // β sahi tarika System.out.println("Wrong division: " + result); // 3.0 System.out.println("Correct division: " + result2); // 3.3333 } }
public class Widening3 { public static void main(String[] args) { // char β int widening (char ka ASCII value milta hai) char ch = 'A'; int ascii = ch; // char β int automatic, 'A' ka ASCII = 65 System.out.println("char value: " + ch); System.out.println("int value: " + ascii); // 65 char ch2 = 'a'; int ascii2 = ch2; System.out.println("char value: " + ch2); System.out.println("int value: " + ascii2); // 97 // char β double bhi possible double d = ch; System.out.println("double value: " + d); // 65.0 } }
public class Widening4 { public static void main(String[] args) { int price = 500; printPrice(price); // int diya, double maanga β auto widening long bigNumber = 1234567890L; printPrice(bigNumber); // long diya β auto widening } // method double leta hai static void printPrice(double amount) { System.out.println("Price: Rs." + amount); } }
public class Widening5 { public static void main(String[] args) { // Widening mein data loss NAHI hota (mostly) int x = 2147483647; // int ka max value long y = x; // safe - long mein fit ho jaayega System.out.println("int max: " + x); System.out.println("long val: " + y); // int β float mein THODA precision loss ho sakta hai int bigInt = 123456789; float f = bigInt; // widening but float mein precision kam hai System.out.println("int val: " + bigInt); System.out.println("float val: " + f); // thoda different! } }
Deep Explanation: Narrowing matlab bade container ki cheez chote container mein daalna β manually karna padta hai.
πͺ£ Bucket (double) β π₯ Glass (int) β manually karna padta hai, glass overflow ho sakta hai!
double d = 9.99; int i = (int) d; β (int) likhna padta hai explicitly
β οΈ Data Loss: double 9.99 β int 9 (.99 chala gaya! truncation)
β οΈ Overflow: int 300 β byte ? (byte max 127 hai, 300 fit nahi hoga!)
public class Narrowing1 { public static void main(String[] args) { double d = 9.99; int i = (int) d; // β explicit cast zaroori hai // decimal part CHALA JAATA hai (truncation, rounding nahi!) System.out.println("double value: " + d); // 9.99 System.out.println("int value: " + i); // 9 (not 10!) double d2 = 3.1; int i2 = (int) d2; System.out.println("double value: " + d2); // 3.1 System.out.println("int value: " + i2); // 3 } }
public class Narrowing2 { public static void main(String[] args) { // int β byte: byte ka range -128 to 127 int i1 = 100; byte b1 = (byte) i1; // 100 fits in byte System.out.println("int 100 β byte: " + b1); // 100 int i2 = 130; byte b2 = (byte) i2; // 130 > 127, overflow hoga! System.out.println("int 130 β byte: " + b2); // -126 (unexpected!) int i3 = 256; byte b3 = (byte) i3; System.out.println("int 256 β byte: " + b3); // 0 (256 % 256 = 0) int i4 = 257; byte b4 = (byte) i4; System.out.println("int 257 β byte: " + b4); // 1 } }
public class Narrowing3 { public static void main(String[] args) { // long β int β short β byte chain long l = 1000L; int i = (int) l; // long β int short s = (short) i; // int β short byte b = (byte) s; // short β byte System.out.println("long value: " + l); // 1000 System.out.println("int value: " + i); // 1000 System.out.println("short value: " + s); // 1000 System.out.println("byte value: " + b); // -24 (overflow!) // float β int float f = 45.7f; int fromFloat = (int) f; System.out.println("float " + f + " β int: " + fromFloat); // 45 } }
public class Narrowing4 { public static void main(String[] args) { // int β char (ASCII se character) int ascii = 65; char ch = (char) ascii; // 65 β 'A' System.out.println("int 65 β char: " + ch); // A int ascii2 = 97; char ch2 = (char) ascii2; // 97 β 'a' System.out.println("int 97 β char: " + ch2); // a // double β int β char chain double d = 66.9; int i = (int) d; // 66.9 β 66 char c = (char) i; // 66 β 'B' System.out.println("double 66.9 β int: " + i + " β char: " + c); } }
public class Narrowing5 { public static void main(String[] args) { // Real world example - temperature convert double tempCelsius = 36.6; int tempRounded = (int) tempCelsius; // decimal cut System.out.println("Exact Temp: " + tempCelsius + "Β°C"); System.out.println("Rounded Temp: " + tempRounded + "Β°C"); // Price calculation double totalBill = 1599.99; int billInt = (int) totalBill; System.out.println("Exact Bill: Rs." + totalBill); System.out.println("Truncated: Rs." + billInt); // Safe narrowing - check karo pehle double safeVal = 50.5; if (safeVal >= Byte.MIN_VALUE && safeVal <= Byte.MAX_VALUE) { byte safeByte = (byte) safeVal; System.out.println("Safe cast: " + safeByte); } else { System.out.println("Value out of byte range! Cannot cast safely."); } } }
Special Rule β Bahut log miss karte hain! β οΈ
β byte β char : implicit NAHI hoga (compile error) β char β byte : implicit NAHI hoga (compile error) β short β char : implicit NAHI hoga (compile error) β char β short : implicit NAHI hoga (compile error)
Kyun nahi hota?
byte β signed (-128 to 127) short β signed (-32768 to 32767) char β unsigned (0 to 65535) β DIFFERENT! negative nahi hota
Signed aur Unsigned ke beech automatic conversion UNSAFE hai β isliye Java ne explicitly likhna zaroori kiya.
// Fix: byte b = 65; char c = (char) b; // β explicit cast likhna padega
public class CharByteCast1 { public static void main(String[] args) { // byte β char: explicit cast zaroori byte b = 65; // char c = b; // β Compile Error! char c = (char) b; // β explicit cast System.out.println("byte 65 β char: " + c); // A // char β byte: explicit cast zaroori char ch = 'Z'; // byte bVal = ch; // β Compile Error! byte bVal = (byte) ch; // β explicit cast System.out.println("char Z β byte: " + bVal); // 90 } }
public class CharByteCast2 { public static void main(String[] args) { // short β char: explicit zaroori short s = 72; // char c = s; // β Compile Error! char c = (char) s; // β System.out.println("short 72 β char: " + c); // H // char β short: explicit zaroori char ch = 'M'; // short sVal = ch; // β Compile Error! short sVal = (short) ch; // β System.out.println("char M β short: " + sVal); // 77 } }
public class CharByteCast3 { public static void main(String[] args) { // char range: 0 to 65535 char maxChar = '\uFFFF'; // max char value int charAsInt = maxChar; System.out.println("Max char as int: " + charAsInt); // 65535 // Negative byte β char kya hoga? byte negByte = -1; char fromNeg = (char) negByte; // explicit cast int result = fromNeg; System.out.println("byte -1 β char β int: " + result); // 65535! // -1 ka binary = 11111111 11111111 β char mein 65535 ban gaya } }
public class CharByteCast4 { public static void main(String[] args) { // Saare conversions int ke through safe hote hain // char β int β byte (do step) char ch = 'A'; // 65 int i = ch; // char β int : β widening (OK) byte b = (byte) i; // int β byte : explicit cast System.out.println("char A β int: " + i + " β byte: " + b); // byte β int β char (do step) byte b2 = 66; int i2 = b2; // byte β int : β widening (OK) char c2 = (char) i2; // int β char : explicit cast System.out.println("byte 66 β int: " + i2 + " β char: " + c2); // Direct path: byte b3 = 67; char c3 = (char) b3; // bhi chalega explicit se System.out.println("Direct byte 67 β char: " + c3); } }
Deep Explanation: Expression mein casting bohot important hai β galat jagah cast karo toh galat answer aata hai.
// β GALAT int a = 5, b = 2; double result = a / b; // pehle int/int = 2, phir 2.0 // β SAHI double result = (double) a / b; // pehle 5.0/2, phir 2.5
β οΈ Rule: Cast jo pehle hoti hai woh calculation ko affect karti hai.
public class CastArithmetic1 { public static void main(String[] args) { int a = 7, b = 2; // β Wrong - int division pehle hoti hai double wrong = a / b; System.out.println("Wrong: " + wrong); // 3.0 // β Correct - pehle cast karo double correct = (double) a / b; System.out.println("Correct: " + correct); // 3.5 // β Dono cast karo double correct2 = (double) a / (double) b; System.out.println("Correct2: " + correct2); // 3.5 } }
public class CastArithmetic2 { public static void main(String[] args) { // Percentage calculate karna int scored = 450; int total = 600; // β Wrong way double wrongPercent = scored / total * 100; System.out.println("Wrong %: " + wrongPercent); // 0.0 // β Correct way double correctPercent = (double) scored / total * 100; System.out.println("Correct%: " + correctPercent); // 75.0 // Average int s1 = 80, s2 = 90, s3 = 75; double avg = (double)(s1 + s2 + s3) / 3; System.out.println("Average: " + avg); // 81.666... } }
| Type | Direction | How | Risk |
|---|---|---|---|
| Widening | Chota β Bada byteβshortβintβlongβfloatβdouble |
Automatic / Implicit | β Safe (mostly) |
| Narrowing | Bada β Chota | Manual β (type) likhna padta hai | β οΈ Data loss / Overflow |
| char/byte/short | Inke beech koi bhi | Explicit (type) MUST hai | β οΈ signed vs unsigned |
| Expression | β | (double)a/b β a/b wrong β | β οΈ Wrong result |
| Type | Size | Range |
|---|---|---|
| byte | 1 byte | -128 to 127 |
| short | 2 bytes | -32,768 to 32,767 |
| int | 4 bytes | -2.1B to 2.1B |
| long | 8 bytes | very large |
| float | 4 bytes | decimal (7 digits) |
| double | 8 bytes | decimal (15 digits) |
| char | 2 bytes | 0 to 65,535 (unsigned) |
| Error | Reason | Fix |
|---|---|---|
| possible lossy conversion | Bina cast ke narrowing | (type) explicitly likho |
| incompatible types | charβbyte/short without cast | Explicit (char) ya (byte) likho |
| Result 0.0 ya wrong | int/int division | (double) pehle cast karo |
| Overflow (-126 for 130) | Value range se bahar | Range check karo pehle |
Widening1 hai toh file Widening1.java hogi!
π Type Casting Kya Hai?
Ek data type ki value ko doosre data type mein convert karna.
int a = 100; double b = a; β int ko double mein daal diya = TYPE CASTING
Memory mein sizes:
Deep Explanation: Widening matlab chote container ki cheez bade container mein daalna β automatically hota hai, kuch likhna nahi padta.
π₯ Glass (int) β πͺ£ Bucket (double) β automatically fit ho jaata hai
int x = 100; double y = x; β compiler khud karta hai Java sochta hai: "int β double safe hai, karo"
β
Safe β Data loss NAHI hoga (mostly)
β
Automatic β Implicit kehte hain isliye
public class Widening1 { public static void main(String[] args) { byte b = 10; short s = b; // byte β short (automatic) int i = s; // short β int (automatic) long l = i; // int β long (automatic) float f = l; // long β float (automatic) double d = f; // float β double (automatic) System.out.println("byte value: " + b); System.out.println("short value: " + s); System.out.println("int value: " + i); System.out.println("long value: " + l); System.out.println("float value: " + f); System.out.println("double value: " + d); } }
public class Widening2 { public static void main(String[] args) { int marks = 95; double percentage = marks; // int β double automatic System.out.println("Marks (int): " + marks); System.out.println("Percentage (double): " + percentage); int a = 10; int bVal = 3; double result = a / bVal; // β pehle int/int = 3, phir 3.0 double result2 = (double) a / bVal; // β sahi tarika System.out.println("Wrong division: " + result); // 3.0 System.out.println("Correct division: " + result2); // 3.3333 } }
public class Widening3 { public static void main(String[] args) { // char β int widening (char ka ASCII value milta hai) char ch = 'A'; int ascii = ch; // char β int automatic, 'A' ka ASCII = 65 System.out.println("char value: " + ch); System.out.println("int value: " + ascii); // 65 char ch2 = 'a'; int ascii2 = ch2; System.out.println("char value: " + ch2); System.out.println("int value: " + ascii2); // 97 // char β double bhi possible double d = ch; System.out.println("double value: " + d); // 65.0 } }
public class Widening4 { public static void main(String[] args) { int price = 500; printPrice(price); // int diya, double maanga β auto widening long bigNumber = 1234567890L; printPrice(bigNumber); // long diya β auto widening } // method double leta hai static void printPrice(double amount) { System.out.println("Price: Rs." + amount); } }
public class Widening5 { public static void main(String[] args) { // Widening mein data loss NAHI hota (mostly) int x = 2147483647; // int ka max value long y = x; // safe - long mein fit ho jaayega System.out.println("int max: " + x); System.out.println("long val: " + y); // int β float mein THODA precision loss ho sakta hai int bigInt = 123456789; float f = bigInt; // widening but float mein precision kam hai System.out.println("int val: " + bigInt); System.out.println("float val: " + f); // thoda different! } }
Deep Explanation: Narrowing matlab bade container ki cheez chote container mein daalna β manually karna padta hai.
πͺ£ Bucket (double) β π₯ Glass (int) β manually karna padta hai, glass overflow ho sakta hai!
double d = 9.99; int i = (int) d; β (int) likhna padta hai explicitly
β οΈ Data Loss: double 9.99 β int 9 (.99 chala gaya! truncation)
β οΈ Overflow: int 300 β byte ? (byte max 127 hai, 300 fit nahi hoga!)
public class Narrowing1 { public static void main(String[] args) { double d = 9.99; int i = (int) d; // β explicit cast zaroori hai // decimal part CHALA JAATA hai (truncation, rounding nahi!) System.out.println("double value: " + d); // 9.99 System.out.println("int value: " + i); // 9 (not 10!) double d2 = 3.1; int i2 = (int) d2; System.out.println("double value: " + d2); // 3.1 System.out.println("int value: " + i2); // 3 } }
public class Narrowing2 { public static void main(String[] args) { // int β byte: byte ka range -128 to 127 int i1 = 100; byte b1 = (byte) i1; // 100 fits in byte System.out.println("int 100 β byte: " + b1); // 100 int i2 = 130; byte b2 = (byte) i2; // 130 > 127, overflow hoga! System.out.println("int 130 β byte: " + b2); // -126 (unexpected!) int i3 = 256; byte b3 = (byte) i3; System.out.println("int 256 β byte: " + b3); // 0 (256 % 256 = 0) int i4 = 257; byte b4 = (byte) i4; System.out.println("int 257 β byte: " + b4); // 1 } }
public class Narrowing3 { public static void main(String[] args) { // long β int β short β byte chain long l = 1000L; int i = (int) l; // long β int short s = (short) i; // int β short byte b = (byte) s; // short β byte System.out.println("long value: " + l); // 1000 System.out.println("int value: " + i); // 1000 System.out.println("short value: " + s); // 1000 System.out.println("byte value: " + b); // -24 (overflow!) // float β int float f = 45.7f; int fromFloat = (int) f; System.out.println("float " + f + " β int: " + fromFloat); // 45 } }
public class Narrowing4 { public static void main(String[] args) { // int β char (ASCII se character) int ascii = 65; char ch = (char) ascii; // 65 β 'A' System.out.println("int 65 β char: " + ch); // A int ascii2 = 97; char ch2 = (char) ascii2; // 97 β 'a' System.out.println("int 97 β char: " + ch2); // a // double β int β char chain double d = 66.9; int i = (int) d; // 66.9 β 66 char c = (char) i; // 66 β 'B' System.out.println("double 66.9 β int: " + i + " β char: " + c); } }
public class Narrowing5 { public static void main(String[] args) { // Real world example - temperature convert double tempCelsius = 36.6; int tempRounded = (int) tempCelsius; // decimal cut System.out.println("Exact Temp: " + tempCelsius + "Β°C"); System.out.println("Rounded Temp: " + tempRounded + "Β°C"); // Price calculation double totalBill = 1599.99; int billInt = (int) totalBill; System.out.println("Exact Bill: Rs." + totalBill); System.out.println("Truncated: Rs." + billInt); // Safe narrowing - check karo pehle double safeVal = 50.5; if (safeVal >= Byte.MIN_VALUE && safeVal <= Byte.MAX_VALUE) { byte safeByte = (byte) safeVal; System.out.println("Safe cast: " + safeByte); } else { System.out.println("Value out of byte range! Cannot cast safely."); } } }
Special Rule β Bahut log miss karte hain! β οΈ
β byte β char : implicit NAHI hoga (compile error) β char β byte : implicit NAHI hoga (compile error) β short β char : implicit NAHI hoga (compile error) β char β short : implicit NAHI hoga (compile error)
Kyun nahi hota?
byte β signed (-128 to 127) short β signed (-32768 to 32767) char β unsigned (0 to 65535) β DIFFERENT! negative nahi hota
Signed aur Unsigned ke beech automatic conversion UNSAFE hai β isliye Java ne explicitly likhna zaroori kiya.
// Fix: byte b = 65; char c = (char) b; // β explicit cast likhna padega
public class CharByteCast1 { public static void main(String[] args) { // byte β char: explicit cast zaroori byte b = 65; // char c = b; // β Compile Error! char c = (char) b; // β explicit cast System.out.println("byte 65 β char: " + c); // A // char β byte: explicit cast zaroori char ch = 'Z'; // byte bVal = ch; // β Compile Error! byte bVal = (byte) ch; // β explicit cast System.out.println("char Z β byte: " + bVal); // 90 } }
public class CharByteCast2 { public static void main(String[] args) { // short β char: explicit zaroori short s = 72; // char c = s; // β Compile Error! char c = (char) s; // β System.out.println("short 72 β char: " + c); // H // char β short: explicit zaroori char ch = 'M'; // short sVal = ch; // β Compile Error! short sVal = (short) ch; // β System.out.println("char M β short: " + sVal); // 77 } }
public class CharByteCast3 { public static void main(String[] args) { // char range: 0 to 65535 char maxChar = '\uFFFF'; // max char value int charAsInt = maxChar; System.out.println("Max char as int: " + charAsInt); // 65535 // Negative byte β char kya hoga? byte negByte = -1; char fromNeg = (char) negByte; // explicit cast int result = fromNeg; System.out.println("byte -1 β char β int: " + result); // 65535! // -1 ka binary = 11111111 11111111 β char mein 65535 ban gaya } }
public class CharByteCast4 { public static void main(String[] args) { // Saare conversions int ke through safe hote hain // char β int β byte (do step) char ch = 'A'; // 65 int i = ch; // char β int : β widening (OK) byte b = (byte) i; // int β byte : explicit cast System.out.println("char A β int: " + i + " β byte: " + b); // byte β int β char (do step) byte b2 = 66; int i2 = b2; // byte β int : β widening (OK) char c2 = (char) i2; // int β char : explicit cast System.out.println("byte 66 β int: " + i2 + " β char: " + c2); // Direct path: byte b3 = 67; char c3 = (char) b3; // bhi chalega explicit se System.out.println("Direct byte 67 β char: " + c3); } }
Deep Explanation: Expression mein casting bohot important hai β galat jagah cast karo toh galat answer aata hai.
// β GALAT int a = 5, b = 2; double result = a / b; // pehle int/int = 2, phir 2.0 // β SAHI double result = (double) a / b; // pehle 5.0/2, phir 2.5
β οΈ Rule: Cast jo pehle hoti hai woh calculation ko affect karti hai.
public class CastArithmetic1 { public static void main(String[] args) { int a = 7, b = 2; // β Wrong - int division pehle hoti hai double wrong = a / b; System.out.println("Wrong: " + wrong); // 3.0 // β Correct - pehle cast karo double correct = (double) a / b; System.out.println("Correct: " + correct); // 3.5 // β Dono cast karo double correct2 = (double) a / (double) b; System.out.println("Correct2: " + correct2); // 3.5 } }
public class CastArithmetic2 { public static void main(String[] args) { // Percentage calculate karna int scored = 450; int total = 600; // β Wrong way double wrongPercent = scored / total * 100; System.out.println("Wrong %: " + wrongPercent); // 0.0 // β Correct way double correctPercent = (double) scored / total * 100; System.out.println("Correct%: " + correctPercent); // 75.0 // Average int s1 = 80, s2 = 90, s3 = 75; double avg = (double)(s1 + s2 + s3) / 3; System.out.println("Average: " + avg); // 81.666... } }
| Type | Direction | How | Risk |
|---|---|---|---|
| Widening | Chota β Bada byteβshortβintβlongβfloatβdouble |
Automatic / Implicit | β Safe (mostly) |
| Narrowing | Bada β Chota | Manual β (type) likhna padta hai | β οΈ Data loss / Overflow |
| char/byte/short | Inke beech koi bhi | Explicit (type) MUST hai | β οΈ signed vs unsigned |
| Expression | β | (double)a/b β a/b wrong β | β οΈ Wrong result |
| Type | Size | Range |
|---|---|---|
| byte | 1 byte | -128 to 127 |
| short | 2 bytes | -32,768 to 32,767 |
| int | 4 bytes | -2.1B to 2.1B |
| long | 8 bytes | very large |
| float | 4 bytes | decimal (7 digits) |
| double | 8 bytes | decimal (15 digits) |
| char | 2 bytes | 0 to 65,535 (unsigned) |
| Error | Reason | Fix |
|---|---|---|
| possible lossy conversion | Bina cast ke narrowing | (type) explicitly likho |
| incompatible types | charβbyte/short without cast | Explicit (char) ya (byte) likho |
| Result 0.0 ya wrong | int/int division | (double) pehle cast karo |
| Overflow (-126 for 130) | Value range se bahar | Range check karo pehle |
Widening1 hai toh file Widening1.java hogi!