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
🌐
W3Schools
w3schools.com › java › java_data_types_numbers.asp
Java Numbers
Java Examples Java Videos Java ... ... Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals....
🌐
W3Schools
w3schools.com › java › java_data_types.asp
Java Data Types
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 ... int myNum = 5; // Integer (whole number) float myFloatNum = 5.99f; // Floating point number char myLetter = 'D'; // Character boolean myBool = true; // Boolean String myText = "Hello"; // String
🌐
W3Schools
w3schools.com › java › ref_keyword_int.asp
Java int Keyword
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
BeginnersBook
beginnersbook.com › 2024 › 06 › how-to-find-size-of-an-int-in-java
How to Find Size of an int in Java
Integer.SIZE returns the number of bits required to represent an int. Since 8 bits equal to 1 bytes, dividing the retuned value by 8 converts the size from bits to bytes. ... This confirms that the size of int in Java is 4 bytes.
🌐
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.
🌐
Dot Net Perls
dotnetperls.com › integer-java
Java - Integer Max, Min and Size - Dot Net Perls
Info Type returns a class reference. This matches the shortened type, like int for Integer. Next Size returns the numbers of bits in the type.
Find elsewhere
🌐
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.
🌐
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.
🌐
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.
🌐
CodeGym
codegym.cc › java blog › java classes › java.lang.integer class
Java.lang.Integer Class
February 20, 2025 - OFF SZ TYPE DESCRIPTION VALUE 0 ... (alignment/padding gap) 16 4000 int [I.<elements> N/A Instance size: 4016 bytes Space losses: 4 bytes internal + 0 bytes external = 4 bytes total OK, that does sort of make sense, considering a ...
🌐
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
🌐
DataCamp
datacamp.com › doc › java › int
int Keyword in Java: Usage & Examples
Default Choice: Use int for integer values unless there is a specific need for a smaller or larger data type.
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
You can also create an array by specifying its size with new.
🌐
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.
🌐
W3Schools
w3schools.com › java › ref_string_length.asp
Java String length() Method
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 ... The length() method returns the length of a specified string. Note: The length of an empty string is 0. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
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, ...