← Back to Core Java Course | Chapter 14: Advanced Topics | Lesson 2 of 6

Java में Type Casting

Type casting एक value को एक type से दूसरे में बदलता है, एक cup से juice को एक bottle में डालने जैसा। एक बड़े type में जाना safe है, जबकि एक छोटे में जाना कुछ information खो सकता है।
Syntax
java
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

java
public class Main {
	public static void main(String[] args) {
		int num = 10;
		long widened = num; // automatic: no data lost
		System.out.println(widened);
	}
}

Narrowing Casting

Narrowing casting एक बड़े numeric type को एक छोटे में convert करता है और इसे एक explicit cast चाहिए, क्योंकि data loss (जैसे एक double का decimal हिस्सा truncate करना) possible है।

Compiler explicit cast बिल्कुल इसलिए मजबूर करता है क्योंकि यह चाहता है developer consciously acknowledge करे कि कुछ precision खो सकती है।

उदाहरण: Narrowing Casting

java
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);
	}
}

Reference Upcasting

Upcasting एक subclass reference को इसके superclass type की तरह treat करता है, जो implicitly और सुरक्षित रूप से होता है क्योंकि हर subclass instance genuinely इसके supertype का भी एक instance है।

यह Java में polymorphism की नींव है, code को generically एक superclass type के साथ काम करने देते हुए जबकि अब भी इसकी किसी भी subclasses को रखते हुए।

उदाहरण: Reference Upcasting

java
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);
	}
}

Reference Downcasting

Downcasting एक superclass reference को एक ज़्यादा specific subclass type की तरह treat करता है, एक explicit cast चाहते हुए और runtime पर एक ClassCastException का जोखिम उठाते हुए अगर object असल में उस subclass का instance न हो।

उदाहरण: Reference Downcasting

java
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);
	}
}

Converting Types to Strings

किसी भी value को एक String में convert करना आमतौर पर String.valueOf() या simple concatenation ("" + value) से किया जाता है, दोनों hood के नीचे value का अपना string representation logic call करते हुए।

उदाहरण: Converting Types to Strings

java
// 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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बिना cast के एक double को एक int को assign करना, जो एक compile error है।
  2. Data खोने वाले Narrowing casts, जैसे (int) 3.9 3 देना, और (byte) 200 -56 देना।
  3. गलत class में downcasting करना, जो ClassCastException throw करता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.