☕ Java
Beginner
What is autoboxing and unboxing in Java?
Answer
Autoboxing is the automatic conversion that Java performs from a primitive type to its corresponding wrapper class. For example, assigning an int to an Integer variable: Integer x = 5; — the compiler automatically wraps 5 in an Integer object. Unboxing is the reverse: automatic conversion from a wrapper class to its primitive. For example: int y = x;. While convenient, autoboxing can cause performance issues and NullPointerExceptions if you accidentally unbox a null Integer. Be mindful of autoboxing in tight loops or large collections.
Previous
What is the difference between int and Integer in Java?
Next
What is the difference between == and .equals() in Java?