i gets auto-boxed to an Integer when passed to getObjectSize.

So you get the value of the size of promoted variable, not the original primitive int.

To circumvent this behaviour, build overloads of getObjectSize for the primitive cases:

public static long getObjectSize(int o) {
    return Integer.SIZE / 8;
}

and so on.

Answer from Bathsheba on Stack Overflow
🌐
Quora
quora.com › What-is-the-size-of-an-integer-in-Java
What is the size of an integer in Java? - Quora
* A variable of the int primitive data type takes up 4 bytes (or 32 bits) * An Integer object takes up 16 bytes (or 512 bits) The Integer object is created by the Integer wrapper class and is used for convenience in some places.
🌐
CodeGym
codegym.cc › java blog › java classes › java.lang.integer class
Java.lang.Integer Class
February 20, 2025 - java.util.ArrayList@66d3c617d footprint: COUNT AVG SUM DESCRIPTION 1 4016 4016 [Ljava.lang.Object; 1000 16 16000 java.lang.Integer 1 24 24 java.util.ArrayList 1002 20040 (total) So, ArrayList<Integer> takes up 4 times more memory space.
🌐
W3Schools
w3schools.com › java › java_data_types.asp
Java Data Types
Java Examples Java Videos Java ... explained in the previous chapter, a variable in Java must be a specified data type: int myNum = 5; // Integer (whole number) float myFloatNum = 5.99f; // Floating point number char myLetter ...
🌐
BeginnersBook
beginnersbook.com › 2024 › 06 › how-to-find-size-of-an-int-in-java
How to Find Size of an int in Java
The size of an int is 32 bits and it is consistent across all platforms that support Java language. We can confirm the size of an int using a simple piece of code. public class IntSize { public static void main(String[] args) { System.out.println("Size of int: " + (Integer.SIZE / 8) + " bytes"); } }
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › datatypes.html
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.integer.size
Integer.Size Field (Java.Lang) | Microsoft Learn
[<Android.Runtime.Register("SIZE")>] val mutable Size : int · Value = 32 · Int32 · Attributes · RegisterAttribute · The number of bits used to represent an int value in two's complement binary form.
Find elsewhere
🌐
Quora
quora.com › What-is-the-size-of-int-in-a-Java-programming-language
What is the size of int in a Java programming language? - Quora
Answer (1 of 3): An integer datatype (int) occupies 4 bytes in JAVA. Whether you are using a 32 bit system or a 64 bit system the size of integer data type is fixed i.e; it is 4 bytes.
🌐
W3Schools
w3schools.com › java › java_data_types_numbers.asp
Java Numbers
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... Integer types stores whole numbers, positive or negative (such as 123 or -456), ...
🌐
Quora
quora.com › Q1-What-is-the-size-of-the-int-data-type-in-Java
Q1.What is the size of the int data type in Java? - Quora
Answer (1 of 7): The size of the int data type in java is 4. In java,it is a type of Integral data type. int data type has wide range of uses. The range of int data type in java is -2,147,483,648 to 2,147,483,648. The int data type in java is, ...
🌐
Dot Net Perls
dotnetperls.com › integer-java
Java - Integer Max, Min and Size - Dot Net Perls
This is an 8-byte integral number type, twice the size of an int.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-data-types
Java Data Types - GeeksforGeeks
A 64-bit signed integer used when int is not sufficient for large values. Syntax: long longVar; Size : 8 bytes (64 bits) Java ·
Published   November 7, 2016
🌐
Baeldung
baeldung.com › home › java › java numbers › number of digits in an integer in java
Number of Digits in an Integer in Java | Baeldung
September 10, 2017 - In this quick tutorial, we’ll explore different ways of getting the number of digits in an Integer in Java.
🌐
Codementor
codementor.io › community › primitive data types in java: understand the core essentials
Primitive Data Types In Java: Understand The Core Essentials | Codementor
March 22, 2024 - Add the Suffix "l" or "L" to an integer literal to turn it into a long. long myLong = 2147483648L; long myOtherLong = 2147483649l; For handling decimal numbers, Java offers two primitive data types: float and double.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 8 )
October 20, 2025 - Decodes a String into an Integer. Accepts decimal, hexadecimal, and octal numbers given by the following grammar: ... + DecimalNumeral, HexDigits, and OctalDigits are as defined in section 3.10.1 of The Java™ Language Specification, except that underscores are not accepted between digits.
🌐
Scaler
scaler.com › home › topics › biginteger in java
BigInteger in Java - Scaler Topics
May 4, 2023 - However, there is a limit to the number that these data types can store. For example, the int data type in java can store values from -2147483648 to 2147483647, while the long data type can store values from -9223372036854775808 to ...
🌐
Runestone Academy
runestone.academy › ns › books › published › apcsareview › VariableBasics › minAndMax.html
3.7. Integer Min and Max — AP CSA Java Review - Obsolete
The int type in Java can be used to represent any whole number from -2147483648 to 2147483647. Why those numbers? Integers in Java are represented in 2’s complement binary and each integer gets 32 bits of space. In 32 bits of space with one bit used to represent the sign you can represent ...