Integer Class

Su Suriya Ravichandran Updated 14 Nov 2026
6 min read ·Lesson 105 of 306

java.lang.Integer is the wrapper class for the primitive type int. An Integer object holds a single int value, which lets whole numbers be used where Java requires objects: in collections, generics, and nullable fields. The class also provides constants and static utility methods for parsing text into numbers, converting numbers into text in different bases, comparing values, and working with individual bits. Long, Short, and Byte follow exactly the same design for their primitive types.

Why a Wrapper Class Is Needed

Primitives are not objects, and generic types accept only objects:

List<int> scores;          // compile error
List<Integer> scores;      // correct

Integer is also used when "no value" must be representable, such as a database column that allows NULL, since a primitive int always has a value.

Creating Integer Objects

Integer a = 42;                     // autoboxing: the compiler calls Integer.valueOf(42)
Integer b = Integer.valueOf(42);    // explicit
Integer c = Integer.valueOf("42");  // from text
int d = a;                          // unboxing: the compiler calls a.intValue()

The constructor new Integer(42) is deprecated for removal and should not be used. Integer.valueOf is preferred because it can return cached instances.

Autoboxing and Unboxing

The compiler converts between int and Integer automatically:

List<Integer> list = new ArrayList<>();
list.add(5);                // autoboxing: int → Integer
int first = list.get(0);    // unboxing: Integer → int
int sum = list.get(0) + 10; // unboxed for arithmetic

This convenience hides two traps.

Unboxing null throws

Integer n = null;
int x = n;                  // NullPointerException

The compiler inserts n.intValue(), which fails on null. This often surprises code reading from maps:

Map<String, Integer> stock = new HashMap<>();
int count = stock.get("pen");                 // NullPointerException: key absent
int safe = stock.getOrDefault("pen", 0);      // 0

== compares objects, not values

Integer a = 127, b = 127;
Integer c = 128, d = 128;

System.out.println(a == b);           // true
System.out.println(c == d);           // false
System.out.println(c.equals(d));      // true
System.out.println(c.intValue() == d);  // true: one side is a primitive, so both are compared as ints

Integer.valueOf caches instances for values from −128 to 127, so small values happen to share an object and == returns true. Larger values are separate objects, so == returns false even though the numbers are equal. Code that works in tests with small numbers can fail in production with larger ones.

Rule: compare Integer objects with equals, or unbox at least one side to int.

Constants

ConstantValue
Integer.MAX_VALUE2,147,483,647 (2³¹ − 1)
Integer.MIN_VALUE−2,147,483,648 (−2³¹)
Integer.SIZE32 (bits)
Integer.BYTES4

Parsing Text into Numbers

int a = Integer.parseInt("-42");          // -42 (primitive)
Integer b = Integer.valueOf("42");        // 42 (object)
int hex = Integer.parseInt("ff", 16);     // 255
int bin = Integer.parseInt("1010", 2);    // 10
long big = Long.parseLong("9000000000");  // values beyond int range need Long

parseInt returns an int; valueOf returns an Integer. Both accept an optional radix (base) from 2 to 36.

Invalid input throws NumberFormatException:

Integer.parseInt("12.5");      // NumberFormatException: For input string: "12.5"
Integer.parseInt(" 42");       // NumberFormatException: whitespace is not allowed
Integer.parseInt("");          // NumberFormatException
Integer.parseInt("3000000000");   // NumberFormatException: out of int range

Input from users, files, or HTTP requests should be stripped and parsed inside a try/catch, or validated first. The practical example below shows a reusable pattern.

Converting Numbers into Text

String s1 = Integer.toString(255);          // "255"
String s2 = String.valueOf(255);            // "255"
Integer.toBinaryString(10);                 // "1010"
Integer.toHexString(255);                   // "ff"
Integer.toOctalString(8);                   // "10"
Integer.toString(255, 36);                  // "73": base 36 uses digits and letters

toBinaryString and toHexString represent negative numbers in two's complement: Integer.toHexString(-1) is ffffffff.

For padded or grouped output, use String.format("%05d", n) or "%,d".

Comparing and Arithmetic Helpers

Integer.compare(3, 7);      // -1: negative, zero, or positive (safe, no overflow)
Integer.signum(-9);         // -1, 0, or 1
Integer.max(4, 9);          // 9
Integer.min(4, 9);          // 4
Integer.sum(2, 3);          // 5

Integer.compare is the correct way to compare two int values in a compareTo or Comparator; subtracting them can overflow. Integer::sum, Integer::max, and Integer::compare are also handy as method references, for example map.merge(key, 1, Integer::sum).

Bit Manipulation

MethodExampleResult
bitCount(i)Integer.bitCount(255)8 (number of 1 bits)
highestOneBit(i)Integer.highestOneBit(100)64
numberOfTrailingZeros(i)Integer.numberOfTrailingZeros(8)3
numberOfLeadingZeros(i)Integer.numberOfLeadingZeros(1)31
reverse(i)Integer.reverse(1)-2147483648 (bit 0 moved to bit 31)
rotateLeft(i, n)Integer.rotateLeft(1, 4)16

These are used in hashing, compression, graphics, permission flags, and algorithms that work on powers of two.

Unsigned Operations

Java's int is always signed, but Integer can treat its 32 bits as an unsigned value from 0 to 4,294,967,295:

Integer.parseUnsignedInt("4294967295");    // -1 (the same bits, read as a signed int)
Integer.toUnsignedString(-1);              // "4294967295"
Integer.toUnsignedLong(-1);                // 4294967295L
Integer.divideUnsigned(-2, 2);             // 2147483647
Integer.compareUnsigned(-1, 1);            // positive: -1 is the largest unsigned value

This is useful when reading binary formats, network protocols, or hash values defined as unsigned 32-bit numbers.

Integer, Long, Short, and Byte

All four wrapper classes provide the same kinds of methods (parseXxx, valueOf, toString, compare, MIN_VALUE/MAX_VALUE, and, for Long, the same bit and unsigned operations). Choose the wrapper that matches the primitive you need. Long.valueOf, Short.valueOf, and Byte.valueOf also cache values from −128 to 127, so the same == warning applies to them.

Practical Example: Validating Quantity Input

A shopping cart receives quantities as text from a web form. The value must be a whole number from 1 to 100; anything else must be rejected without crashing the request.

import java.util.*;

public class IntC {
    static OptionalInt parseQuantity(String raw) {
        if (raw == null || raw.isBlank()) return OptionalInt.empty();
        try {
            int q = Integer.parseInt(raw.strip());
            return q > 0 && q <= 100 ? OptionalInt.of(q) : OptionalInt.empty();
        } catch (NumberFormatException e) {
            return OptionalInt.empty();
        }
    }

    public static void main(String[] args) {
        for (String s : List.of("3", " 25 ", "0", "abc", "250", "", "7.5")) {
            OptionalInt q = parseQuantity(s);
            System.out.printf("%-6s -> %s%n", "\"" + s + "\"", q.isPresent() ? q.getAsInt() : "invalid");
        }

        List<Integer> ids = new ArrayList<>(List.of(10, 20, 30));
        ids.remove(Integer.valueOf(20));
        System.out.println(ids);
    }
}

Output:

"3"    -> 3
" 25 " -> 25
"0"    -> invalid
"abc"  -> invalid
"250"  -> invalid
""     -> invalid
"7.5"  -> invalid
[10, 30]

parseQuantity handles every way the input can go wrong:

  • Missing or blank input is rejected before parsing is attempted.
  • Surrounding spaces are removed with strip(), because parseInt rejects whitespace.
  • Non-numeric text and decimals ("abc", "7.5") make parseInt throw NumberFormatException, which is caught and turned into "invalid."
  • Valid numbers out of range (0, 250) pass parsing but fail the range check.

Returning OptionalInt (a primitive-specialized Optional) makes it impossible for the caller to forget that the value may be missing, and avoids boxing. The last line shows why Integer.valueOf(20) is needed when removing a value from a List<Integer>: ids.remove(20) would call remove(int index) and throw IndexOutOfBoundsException.

0 Comments

Reviewed before they appear

No comments yet.

Java
Java Introduction Java Features Java Syntax Java Comments Java Statements Java Output Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Numbers Java Math Java Booleans Java User Input Java Conditions Java If Else Java Switch Java While Loop Java For Loop Java Break Java Continue Java Arrays Java Methods Java Parameters Java Return Values Java Scope Java Recursion Install Java Install JDK Java IDEs Java JDK Java JRE Java JVM Java Compiler Java File Compile Java Run Java JShell Basics Java Packages Java OOP Java Classes Java Objects Class Methods Constructors Constructor Overload this Keyword static Keyword final Keyword Encapsulation Inheritance Polymorphism Abstraction Access Modifiers super Keyword Method Overload Method Override Instanceof Operator Nested Classes Inner Classes Anonymous Classes Enums Records Array Basics Array Length Loop Arrays Multidimensional Arrays Array Copy Array Sorting Array Search ArrayList LinkedList HashSet TreeSet HashMap TreeMap Queue Interface Deque Interface Stack Class Iterator List Interface Set Interface Map Interface Collections Class Comparable Comparator Generics Basics Generic Classes Generic Methods Wildcards String Basics String Length String Methods String Concatenation String Compare String Search String Replace String Split String Format StringBuilder StringBuffer Text Blocks Regular Expressions Number Types Integer Class Float Class Double Class BigInteger BigDecimal Math Class Random Numbers Number Format Date Basics LocalDate LocalTime LocalDateTime ZonedDateTime Date Formatting Date Parsing Period Class Duration Class Java Instant Class Java Exception Basics Java try...catch Java Multiple Catch Blocks Java finally Block Java throw Keyword Java throws Keyword Java Checked Exceptions Java Unchecked Exceptions Java Custom Exceptions Java try-with-resources Java Exception Chaining Java File Basics Java Create Files Java Read Files Java Write Files Java Delete Files Java File Paths Java File Class Java BufferedReader Java BufferedWriter Java FileInputStream Java FileOutputStream Java Object Streams Java Directory Handling Java NIO Basics Java Files API Java Input Java Scanner Class Java Console Input Java PrintStream Java InputStream Java OutputStream Java Reader Class Java Writer Class Java Buffered Streams Java Data Streams Java Generics Introduction Java Generic Types Java Generic Methods Java Bounded Types Java Wildcards Java Type Erasure Java Generic Interfaces Java Generic Collections Java Lambda Basics Java Lambda Syntax Java Functional Interfaces Java Method References Java Stream API Java Stream Creation Java Stream Filter Java Stream Map Java Stream Reduce Java Stream Sort Java Stream Collect Java Collectors Class Java Optional Class Java Parallel Streams Java Threads: Thread Basics Create a Thread in Java Java Thread Class Java Runnable Interface Java Thread States Java Thread Sleep Java Thread Join Java Thread Priority Java Synchronization Java Thread Safety Java Race Conditions Java Deadlocks Java Executor Service Java Thread Pools Java Callable Interface Java Future Interface Java CompletableFuture Java Concurrent Collections Java Atomic Classes Java Virtual Threads Java Networking Basics Java URL Class Java URI Class Java HttpClient Java HTTP Requests Java HTTP Responses Java Socket Basics Java ServerSocket Java DatagramSocket Java WebSocket Basics Java JDBC Basics Java JDBC Setup Java Database Connection Java JDBC Connection Interface Java JDBC Statement Java PreparedStatement Java ResultSet Java JDBC CRUD Operations Java JDBC Transactions Java JDBC Batch Update Java Connection Pooling with HikariCP Java JPA Basics: Entities, EntityManager and JPQL Java Hibernate Basics: SessionFactory, Session and HQL Package in Java: Basics Explained with Examples Create Package in Java: Step-by-Step Guide with Examples Import Package in Java: The import Statement Explained Access Control in Java: Packages, Classes and Members JAR File in Java: Create, Run and Inspect JAR Files Java Modules: Introduction to the Java Module System module-info in Java: Every Module Directive Explained Java Module Path: How --module-path Finds Your Modules Java Classpath: How -cp and CLASSPATH Find Your Classes Java Annotations: Basics of @ Metadata in Java Java Built-in Annotations: @Override, @Deprecated and More Java Custom Annotations: Create Your Own @interface Java Annotation Targets: @Target and ElementType Explained Java Reflection API: Inspect and Use Classes at Runtime Java Class Metadata: Names, Modifiers, Hierarchy and Generic Java Dynamic Objects: Create Objects Dynamically With Reflec Java Dynamic Proxy: Intercept Method Calls With Proxy and In Java Security Java Secure Coding Java Password Hashing Java Input Validation Java Cryptography Basics Java KeyStore Basics Java TLS Basics Java SecureRandom Java JUnit Basics Java JUnit Test Methods JUnit Assertions JUnit Test Fixtures JUnit Parameterized Tests Mockito in Java Java Mock Objects Java Integration Testing Java Code Coverage Maven in Java Create a Maven Project in Java Maven Dependencies Maven Plugins Gradle in Java Gradle Build Gradle Dependencies Build Executable JAR in Java Build WAR File in Java HTTP Basics in Java Java Servlet Basics Servlet Lifecycle in Java HttpServletRequest in Java HttpServletResponse in Java JSP in Java JSTL in JSP REST API in Java JSON in Java Java Web Application Spring Framework Basics Spring Boot Tutorial Spring MVC Spring Data JPA Spring Security Spring Boot REST API Jakarta EE Basics Java Deployment JAR Deployment Java WAR Deployment Run Java as a Service Docker Java: Dockerize a Java Application Java Environment Variables and Configuration Java Logging Basics Java Production Setup Java JVM Options Java JVM Monitoring Java Hello World App Java Calculator App Java Number Guess Game Java Student Manager Java Library System Java Bank App Java Todo App Java File Manager Java Quiz App Java REST API Project Java Chat Server Java Employee Manager Java E-commerce API
Ask about this post
AI Ask about this post

Ask questions about Integer Class and get answers drawn from it.

Signed-in readers only.