PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

Autoboxing and Unboxing in Java

← All stacks

Theory

228/270

Autoboxing and Unboxing in Java

Autoboxing converts primitive → wrapper automatically: Integer n = 10;. Unboxing is wrapper → primitive: int x = n;. Java does this so you can put int into ArrayList<Integer>.

Useful, but null is dangerous. Integer n = null; int x = n; throws NullPointerException.

Don’t rely on == between two Integer objects for large numbers — cache only covers a small range. Use equals.

Let's take this on the board with one tiny Main class — no extra files. Autoboxing and Unboxing in Java — 10 is boxed into Integer, then unboxed back to int for math. Start from main, go line by line, and stop at each print. That output is the proof for Autoboxing and Unboxing.

Exam tip

Show Integer n = 5; int x = n;. Then warn: null unboxing NPE.

Example

// Wrapper / autoboxing
public class Main {
  public static void main(String[] args) {
    Integer a = 10; // autobox
    int b = a;       // unbox
    System.out.println(a + b);
  }
}

Autoboxing and Unboxing in Java — 10 is boxed into Integer, then unboxed back to int for math.

Short notes

  • DefAutoboxing int → Integer. Unboxing Integer → int.
  • UseArrayList<Integer> with add(5).
  • TrapUnboxing null → NPE. == on Integer can surprise you.
  • RememberPrefer equals for Integer content.

Questions

1

What is autoboxing?

2

What is the null trap?

Previous← Java AnnotationsNextJava Varargs →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.