Blog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2025 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Java
Javamedium

Autoboxing/unboxing — what is it and what are common pitfalls?

Tags
#autoboxing#wrapper#npe
Back to categoryPractice quiz

Answer

Autoboxing converts primitives to wrappers (e.g., `int`→`Integer`) and unboxing does the reverse. Pitfalls: unboxing `null` causes NPE, `Integer` caching can confuse `==`, and extra allocations can hurt performance in hot code.

Integer x = null;
// int y = x; // NullPointerException due to unboxing

Integer a = 100;
Integer b = 100;
System.out.println(a == b); // true (Integer cache)

Integer c = 1000;
Integer d = 1000;
System.out.println(c == d); // false