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.
🌐
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 ...
🌐
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.
🌐
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.
🌐
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"); } }
🌐
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), ...
🌐
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.
🌐
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, ...
🌐
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.
🌐
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
🌐
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 ...
🌐
javaspring
javaspring.net › blog › int-size-java
Understanding `int` Size in Java — javaspring.net
This blog post will delve deep into the fundamental concepts of int size in Java, its usage methods, common practices, and best practices. ... In Java, the int data type is a 32 - bit signed two's complement integer.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-get-size-minimum-and-maximum-value-of-data-types-in-java
How to Get Size, Minimum, and Maximum Value of Data Types in Java? - GeeksforGeeks
Value 1 Byte 8 -128 127 2 Short 16 -32768 32767 3 Integer 32 -2147483648 2147483647 4 Float 32 1.4E-45 3.4028235E38 5 Long 64 -9223372036854775808 9223372036854775807 6 Double 64 4.9E-324 1.7976931348623157E308 7 Character 16
Published   March 28, 2021