Java की Wrapper Classes
In this page:
Integer object = Integer.valueOf(primitiveValue);
int primitive = object.intValue();
Integer.parseInt(string);
What are Wrapper Classes
Integer, Double, और Boolean जैसी Wrapper classes Java के primitive types के object equivalents प्रदान करती हैं, primitives को कहीं भी उपयोग होने देते हुए जहां एक Object ज़रूरी हो — जैसे generic collections के अंदर।
उदाहरण: What are Wrapper Classes
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>(); // Integer wraps int
numbers.add(5);
System.out.println(numbers.get(0));
}
}
Login to try C/C++/Java/PHP code in the editor
Understanding Autoboxing
Autoboxing Java का किसी primitive को इसके wrapper type में अपने आप convert करना है जब एक object expected हो, जैसे एक int को बिना किसी explicit cast के सीधे एक Integer variable को assign करना।
उदाहरण: Understanding Autoboxing
public class Main {
public static void main(String[] args) {
Integer boxed = 5; // int automatically becomes Integer
System.out.println(boxed);
}
}
Login to try C/C++/Java/PHP code in the editor
Understanding Unboxing
Unboxing इसका उल्टा है: Java अपने आप एक wrapper object से primitive value extract करता है जब एक primitive expected हो, जैसे एक arithmetic expression में एक Integer उपयोग करना।
उदाहरण: Understanding Unboxing
public class Main {
public static void main(String[] args) {
Integer boxed = 10;
int primitive = boxed + 5; // Integer automatically becomes int
System.out.println(primitive);
}
}
Login to try C/C++/Java/PHP code in the editor
Parsing Numeric Strings
Integer.parseInt() और Double.parseDouble() जैसे methods एक String को इसकी corresponding primitive value में convert करते हैं, अगर string एक valid number न हो तो NumberFormatException throw करते हुए।
उदाहरण: Parsing Numeric 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 Integer.parseInt("42")
int num = Integer.parseInt("42");
// Declare d and set it to Double.parseDouble("3.14")
double d = Double.parseDouble("3.14");
// Print a line to the console
System.out.println(num + " " + d);
}
}
Login to try C/C++/Java/PHP code in the editor
Wrapper Constants and Utility Methods
Wrapper classes उपयोगी constants और utilities भी expose करती हैं, जैसे largest representable int के लिए Integer.MAX_VALUE, और </> के लिए एक null-safe comparison alternative के रूप में Integer.compare()।
उदाहरण: Wrapper Constants and Utility Methods
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(Integer.MAX_VALUE);
// Print a line to the console
System.out.println(Integer.compare(3, 7));
}
}
Login to try C/C++/Java/PHP code in the editor
- दो
Integerobjects को==से compare करना, जो सिर्फ छोटी cached values के लिए काम करता है;equals()उपयोग करें। - एक
nullwrapper को unboxing करना, जोNullPointerExceptionthrow करता है। - एक non-numeric string को
Integer.parseInt()से parse करना, जोNumberFormatExceptionthrow करता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: