🌐
Baeldung
baeldung.com › home › java › core java › how to get the size of an object in java
How to Get the Size of an Object in Java | Baeldung
January 8, 2024 - For example, when we compare int primitive (which consumes only 4 bytes) to the Integer object which takes 16 bytes, we see that there is 300% memory overhead. One way to get an estimate of an object’s size in Java is to use getObjectSize(Object) ...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-array-size-explained-by-example
Java array size, length and loop examples
You can’t change the size of an array in Java once the array is initialized. A common example of the Java array length property being used in code is a program looping through all of the elements in an array.
People also ask

How does the size() method work in Java Lists?
When you call the size() method on a Java List, it does a little bit of internal magic. Rather than iterating through all elements, it directly accesses a variable or field that keeps track of the number of elements in the List, returning that value to you swiftly.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › java list size
Java List Size: Understanding and Utilizing Dynamic Data Structures
How often should I call the size() method?
The size() method is there for your use anytime you need to know the number of elements in the List. However, using it excessively or unnecessarily could have performance implications, so employ this tool wisely and only when needed.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › java list size
Java List Size: Understanding and Utilizing Dynamic Data Structures
Why is the time complexity of the size() method constant?
The efficiency of the size() method is a boon. It boasts a constant time complexity because it merely retrieves the stored size value. It doesn't need to traverse through the List's elements, ensuring a quick and efficient response regardless of the List's size.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › java list size
Java List Size: Understanding and Utilizing Dynamic Data Structures
🌐
Codecademy
codecademy.com › docs › java › arraylist › .size()
Java | ArrayList | .size() | Codecademy
January 27, 2023 - Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more. Beginner Friendly.Beginner Friendly17 hours17 hours · Here, a variable size of type int is initialized with the size of a list. The List interface is an ordered collection of elements and is implemented by various classes such as ArrayList, LinkedList, and Vector: ... The following example creates an ArrayList, uses .add() to append elements to it, and uses .size() to print out its size:
🌐
Scaler
scaler.com › home › topics › java list size()
Java List Size() - Scaler Topics
April 28, 2024 - Java List size() method returns the count of elements present in the ArrayList. For example, if an ArrayList has five string objects stored in it, the size method will return five.
🌐
GeeksforGeeks
geeksforgeeks.org › java › list-size-method-in-java-with-examples
List size() method in Java with Examples - GeeksforGeeks
July 11, 2025 - // Java program to Illustrate size() ... l.add(5); // Getting total size of list // using size() method int s = l.size(); System.out.println("Size : " + s); } }...
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-size-method-in-java-with-examples
ArrayList size() Method in Java with Examples - GeeksforGeeks
July 11, 2025 - // Java program to demonstrate ... n.add(23); // Getting and printing the // size of the ArrayList int s = n.size(); System.out.println("" + s); } } ... Returns Value: This method returns the number of elements in the ...
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › java list size
Java List Size: Understanding and Utilizing Dynamic Data Structures
3 weeks ago - In Java, the List interface provides the size() method to retrieve the number of elements in a List, while the length property is used to determine the array size Java. The size() method is specific to List implementations and provides a dynamic size that can change as elements are added or removed. On the other hand, the length property is a fixed value for arrays, representing the number of elements that can be stored in the array. Let's explore a few more examples to solidify our understanding of the Java List size() method.
Find elsewhere
🌐
W3Schools
w3schools.com › java › ref_arraylist_size.asp
Java ArrayList size() Method
HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference AngularJS Reference jQuery Reference · HTML Examples CSS Examples JavaScript Examples How To Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples jQuery Examples
🌐
Board Infinity
boardinfinity.com › blog › listsize-in-java
ListSize() in Java | Board Infinity
August 30, 2025 - Java's List size() function can be used to determine a list's size; it returns an integer that indicates the list's size without asking for any additional parameters. The return value of the size() method can change because ArrayList has a dynamic ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-use-list-size-method-in-java-with-examples
How to use List size() method in Java With Examples?
We use this method to retrieve ... list.add(30); System.out.println("The list elements are: " + list); //using size() method System.out.println("The size of list is: " + list.size()); } }...
🌐
TutorialsPoint
tutorialspoint.com › What-does-the-method-size-do-in-java
What does the method size() do in java?
import java.util.ArrayList; public ... arrlist.add(25); arrlist.add(22); for (Integer number : arrlist) { System.out.println("Number = " + number); } int retval = arrlist.size(); System.out.println("Size of list = " + retval); } } Number = 15 Number = 20 Number = 25 Number = 22 Size ...
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › size
Java ArrayList size() - Get List Size | Vultr Docs
November 13, 2024 - ... import java.util.ArrayList; ... list.add("Banana"); list.add("Cherry"); for (int i = 0; i < list.size(); i++) { System.out.println("Element at index " + i + ": " + list.get(i)); } } } Explain Code...
🌐
GeeksforGeeks
geeksforgeeks.org › java › set-size-method-in-java-with-example
Set size() method in Java with Example - GeeksforGeeks
July 26, 2024 - // Java code to illustrate Set.size() ... System.out.println("Set: " + set); // Displaying the size of the Set System.out.println("The size of the set is: " + set.size()); } }...
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › validation › constraints › Size.html
Size (Java(TM) EE 7 Specification APIs)
javax.validation.constraints · @Target(value={METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER}) @Retention(value=RUNTIME) @Documented @Constraint(validatedBy={}) public @interface Size · The annotated element size must be between the specified boundaries (included).
🌐
TutorialsPoint
tutorialspoint.com › home › java/util › java arraylist size
Java ArrayList Size
September 1, 2008 - Arraylist Size = 3 Arraylist Size ... to the ArrayList object using add() method calls per element. Size of the arraylist is printed using size() method....
Top answer
1 of 16
72

No. There is no such method in the standard Java SE class library.

The designers' view is that it is not needed in Java, since the language removes the need for an application1 to know about how much space needs to be reserved for a primitive value, an object or an array with a given number of elements.

You might think that a sizeof operator would be useful for people that need to know how much space their data structures take. However you can also get this information and more, simply and reliably using a Java memory profiler, so there is no need for a sizeof method.


Previous commenters made the point that sizeof(someType) would be more readable than 4. If you accept that readability argument, then the remedy is in your hands. Simply define a class like this ...

public class PrimitiveSizes {
    public static int sizeof(byte b) { return 1; } 
    public static int sizeof(short s) { return 2; }
    // etcetera
}

... and statically import it ...

import static PrimitiveSizes.*;

Or define some named constants; e.g.

public static final int SIZE_OF_INT = 4;

Or (Java 8 and later) use the Integer.BYTES constant, and so on.


Why haven't the Java designers implemented this in standard libraries? My guess is that:

  • they don't think there is a need for it,
  • they don't think there is sufficient demand for it, and
  • they don't think it is worth the effort.

There is also the issue that the next demand would be for a sizeof(Object o) method, which is fraught with technical difficulties.

The key word in the above is "they"!


1 - A programmer may need to know in order to design space efficient data structures. However, I can't imagine why that information would be needed in application code at runtime via a method call.

2 of 16
15

From the article in JavaWorld

A superficial answer is that Java does not provide anything like C's sizeof(). However, let's consider why a Java programmer might occasionally want it.

A C programmer manages most datastructure memory allocations himself, and sizeof() is indispensable for knowing memory block sizes to allocate. Additionally, C memory allocators like malloc() do almost nothing as far as object initialization is concerned: a programmer must set all object fields that are pointers to further objects. But when all is said and coded, C/C++ memory allocation is quite efficient.

By comparison, Java object allocation and construction are tied together (it is impossible to use an allocated but uninitialized object instance). If a Java class defines fields that are references to further objects, it is also common to set them at construction time. Allocating a Java object therefore frequently allocates numerous interconnected object instances: an object graph. Coupled with automatic garbage collection, this is all too convenient and can make you feel like you never have to worry about Java memory allocation details.

Of course, this works only for simple Java applications. Compared with C/C++, equivalent Java datastructures tend to occupy more physical memory. In enterprise software development, getting close to the maximum available virtual memory on today's 32-bit JVMs is a common scalability constraint. Thus, a Java programmer could benefit from sizeof() or something similar to keep an eye on whether his datastructures are getting too large or contain memory bottlenecks. Fortunately, Java reflection allows you to write such a tool quite easily.

Before proceeding, I will dispense with some frequent but incorrect answers to this article's question. Fallacy: Sizeof() is not needed because Java basic types' sizes are fixed

Yes, a Java int is 32 bits in all JVMs and on all platforms, but this is only a language specification requirement for the programmer-perceivable width of this data type. Such an int is essentially an abstract data type and can be backed up by, say, a 64-bit physical memory word on a 64-bit machine. The same goes for nonprimitive types: the Java language specification says nothing about how class fields should be aligned in physical memory or that an array of booleans couldn't be implemented as a compact bitvector inside the JVM. Fallacy: You can measure an object's size by serializing it into a byte stream and looking at the resulting stream length

The reason this does not work is because the serialization layout is only a remote reflection of the true in-memory layout. One easy way to see it is by looking at how Strings get serialized: in memory every char is at least 2 bytes, but in serialized form Strings are UTF-8 encoded and so any ASCII content takes half as much space

🌐
Java67
java67.com › 2016 › 07 › how-to-find-length-size-of-arraylist-in-java.html
How to find length/size of ArrayList in Java? Example | Java67
* */ public class ArrayListDemo{ public static void main(String[] args) { System.out.println("Welcome to Java Program to find the length of array list"); ArrayList<String> listOfBanks = new ArrayList<>(); int size = listOfBanks.size(); ...