Java
Programming
Data Types
Integer
int

What is the difference between Integer and int in Java?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In Java, both int and Integer are used to represent numeric values. However, they belong to fundamentally different categories: int is a primitive data type while Integer is a class, part of Java’s wrapper class system provided in the java.lang package. Understanding the differences between these two is crucial for efficient Java programming, especially in contexts involving collections, serialization, or reflection.

Primitive Type: int

The int type is one of the eight primitive data types in Java. A primitive type is predefined by the language and named by a reserved keyword. int represents a 32-bit signed two's complement integer, which allows it to have values from -2,147,483,648 to 2,147,483,647.

Primitive types such as int are highly efficient. Variables of type int are stored directly in memory, and operations on these variables are generally the fastest possible, especially when compared to their wrapper counterparts.

Example of using int:

java
int number = 100;
int sum = number + 200;  // Direct arithmetic operation

Wrapper Class: Integer

On the other hand, Integer is a class in Java that encapsulates an int. Being a class, Integer provides several methods that can be used to manipulate and convert integers, as well as constants such as Integer.MAX_VALUE and Integer.MIN_VALUE. Moreover, Integer falls under the broader category of reference types, which means it can hold null, unlike primitive types that can only hold their respective values.

An essential aspect of Integer is its role in enabling int values to be used in Java’s generics-based collections like ArrayList or HashMap, which cannot hold primitives.

Example of using Integer:

java
Integer number = Integer.valueOf(100);
Integer sum = number + 200;  // Autoboxing followed by unboxing for the arithmetic operation

Autoboxing and Unboxing

Java 5 introduced autoboxing and unboxing, blurring the lines between int and Integer by automatically converting primitives to their corresponding wrapper types and vice versa when needed. This feature is particularly useful in collections and conditional expressions, contributing to cleaner, more readable code.

Example:

java
List<Integer> list = new ArrayList<>();
list.add(5);  // Autoboxing is happening here (int to Integer)
int first = list.get(0);  // Unboxing is happening here (Integer to int)

Performance Considerations

While Integer objects offer more flexibility than int, they come with a cost of additional memory and processing overhead. An Integer instance takes up more memory than an int, and the process of autoboxing and unboxing can add overhead during runtime, especially in scenarios involving extensive numerical computations or large collections of integers.

Null Safety

A significant difference is that Integer can handle null values, allowing it to represent the absence of a set value. This property can be particularly useful when dealing with databases or data parsing where values might be optional.

Summary

Here's a quick summary of key differences:

FeatureintInteger
TypePrimitiveObject (Wrapper Class)
Memory & PerformanceHighly efficientLess efficient; more memory overhead
Usage with CollectionsNot directly usableUsable as objects
NullabilityCannot be nullCan be null
Utility MethodsNoneMany (e.g., parseInt, compare)
Autoboxing/UnboxingDoes not applySupported (since Java 5)

Conclusion

Choosing between int and Integer in Java programming depends on the specific requirements and context of the application. For raw performance and when working with large arrays of numeric data, int is preferable. In contrast, Integer is indispensable for scenarios that require collections of integers, flexibility through methods, and the ability to deal with null values. Understanding these nuances ensures better, more effective Java coding.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.