Mihir Kumar Shrestha’s Post

Interview Question: What is Autoboxing and Unboxing in Java? Autoboxing and Unboxing are concepts in Java that handle the conversion between primitive data types and their corresponding wrapper classes. Autoboxing is the automatic conversion of a primitive type into its wrapper object. Unboxing is the reverse process, where a wrapper object is converted back into a primitive type. Example: int a = 10; // Autoboxing Integer obj = a; // Unboxing int b = obj; System.out.println(a + " " + obj + " " + b); 👉 Here, Java automatically converts: int → Integer (Autoboxing) Integer → int (Unboxing) Usage in Collections: import java.util.ArrayList; ArrayList<Integer> list = new ArrayList<>(); list.add(10); // Autoboxing int value = list.get(0); // Unboxing 👉 Collections store objects, so autoboxing makes it seamless to use primitives. ⚠️ Important Edge Case: Integer obj = null; int x = obj; // Throws NullPointerException 👉 During unboxing, if the wrapper object is null, it results in a runtime error. #Java #InterviewQuestions #Programming #BackendDevelopment #SoftwareEngineering

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories