Java में Type Casting
In this page:
targetType variable = (targetType) value; // explicit (narrowing)
largerType variable = smallerValue; // implicit (widening)
Widening Casting
Widening casting एक छोटे numeric type को अपने आप एक बड़े में convert करता है (जैसे int से long), क्योंकि conversion में कोई data खो नहीं सकता।
आम examples में byte से short, int से long, और float से double शामिल हैं, जिन सभी को Java बिना किसी special syntax के implicitly perform करता है।
उदाहरण: Widening Casting
public class Main {
public static void main(String[] args) {
int num = 10;
long widened = num; // automatic: no data lost
System.out.println(widened);
}
}
Login to try C/C++/Java/PHP code in the editor
Narrowing Casting
Narrowing casting एक बड़े numeric type को एक छोटे में convert करता है और इसे एक explicit cast चाहिए, क्योंकि data loss (जैसे एक double का decimal हिस्सा truncate करना) possible है।
Compiler explicit cast बिल्कुल इसलिए मजबूर करता है क्योंकि यह चाहता है developer consciously acknowledge करे कि कुछ precision खो सकती है।
उदाहरण: Narrowing Casting
public class Main {
public static void main(String[] args) {
double d = 9.78;
int narrowed = (int) d; // explicit cast required: decimal truncated
System.out.println(narrowed);
}
}
Login to try C/C++/Java/PHP code in the editor
Reference Upcasting
Upcasting एक subclass reference को इसके superclass type की तरह treat करता है, जो implicitly और सुरक्षित रूप से होता है क्योंकि हर subclass instance genuinely इसके supertype का भी एक instance है।
यह Java में polymorphism की नींव है, code को generically एक superclass type के साथ काम करने देते हुए जबकि अब भी इसकी किसी भी subclasses को रखते हुए।
उदाहरण: Reference Upcasting
class Animal {}
class Dog extends Animal {}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
Animal a = dog; // implicit and safe
System.out.println(a instanceof Dog);
}
}
Login to try C/C++/Java/PHP code in the editor
Reference Downcasting
Downcasting एक superclass reference को एक ज़्यादा specific subclass type की तरह treat करता है, एक explicit cast चाहते हुए और runtime पर एक ClassCastException का जोखिम उठाते हुए अगर object असल में उस subclass का instance न हो।
उदाहरण: Reference Downcasting
class Animal {}
class Dog extends Animal {}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
Dog dog = (Dog) a; // explicit, risks ClassCastException
System.out.println(dog);
}
}
Login to try C/C++/Java/PHP code in the editor
Converting Types to Strings
किसी भी value को एक String में convert करना आमतौर पर String.valueOf() या simple concatenation ("" + value) से किया जाता है, दोनों hood के नीचे value का अपना string representation logic call करते हुए।
उदाहरण: Converting Types to Strings
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare num and set it to 42
int num = 42;
// Declare s1 and set it to String.valueOf(num)
String s1 = String.valueOf(num);
String s2 = "" + num;
// Print a line to the console
System.out.println(s1 + " " + s2);
}
}
Login to try C/C++/Java/PHP code in the editor
- बिना cast के एक
doubleको एकintको assign करना, जो एक compile error है। - Data खोने वाले Narrowing casts, जैसे
(int) 3.93देना, और(byte) 200-56देना। - गलत class में downcasting करना, जो
ClassCastExceptionthrow करता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: