Integer vs int: with regard to memory
In general, the heap memory used by a Java object in Hotspot consists of:
- an object header, consisting of a few bytes of "housekeeping" information;
- memory for primitive fields, according to their size (int n->32 bits)
- memory for reference fields (4 bytes each) (Integer n ->32 bits)
- padding: potentially a few "wasted" unused bytes after the object data, to make every object start at an address that is a convenient multiple of bytes and reduce the number of bits required to represent a pointer to an object.
as per the suggestion of Mark Peters I would like add the link below
http://www.javamex.com/tutorials/memory/object_memory_usage.shtml
What is the difference between Integer and int in Java?
int
is a primitive type. Variables of type int
store the actual binary value for the integer you want to represent. int.parseInt("1")
doesn't make sense because int
is not a class and therefore doesn't have any methods.
Integer
is a class, no different from any other in the Java language. Variables of type Integer
store references to Integer
objects, just as with any other reference (object) type. Integer.parseInt("1")
is a call to the static method parseInt
from class Integer
(note that this method actually returns an int
and not an Integer
).
To be more specific, Integer
is a class with a single field of type int
. This class is used where you need an int
to be treated like any other object, such as in generic types or situations where you need nullability.
Note that every primitive type in Java has an equivalent wrapper class:
byte
hasByte
short
hasShort
int
hasInteger
long
hasLong
boolean
hasBoolean
char
hasCharacter
float
hasFloat
double
hasDouble
Wrapper classes inherit from Object class, and primitive don't. So it can be used in collections with Object reference or with Generics.
Since java 5 we have autoboxing, and the conversion between primitive and wrapper class is done automatically. Beware, however, as this can introduce subtle bugs and performance problems; being explicit about conversions never hurts.
What is the difference between an int and an Integer in Java and C#?
In Java, the 'int' type is a primitive, whereas the 'Integer' type is an object.
In C#, the 'int' type is the same as System.Int32
and is a value type (ie more like the java 'int'). An integer (just like any other value types) can be boxed ("wrapped") into an object.
The differences between objects and primitives are somewhat beyond the scope of this question, but to summarize:
Objects provide facilities for polymorphism, are passed by reference (or more accurately have references passed by value), and are allocated from the heap. Conversely, primitives are immutable types that are passed by value and are often allocated from the stack.
Java Primitive Types: int vs. Integer
Short answer: An int
is a number; an Integer
is a pointer that can reference an object that contains a number. Using Integer
for arithmetic involves more CPU cycles and consumes more memory. An int
is not an object and cannot passed to any method that requires objects (just like what you said about Generics).
Long vs Integer, long vs int, what to use and when?
Long
is the Object
form of long
, and Integer
is the object form of int
.
The long
uses 64 bits. The int
uses 32 bits, and so can only hold numbers up to ±2 billion (-231 to +231-1).
You should use long
and int
, except where you need to make use of methods inherited from Object
, such as hashcode
. Java.util.collections
methods usually use the boxed (Object
-wrapped) versions, because they need to work for any Object
, and a primitive type, like int
or long
, is not an Object
.
Another difference is that long
and int
are pass-by-value, whereas Long
and Integer
are pass-by-reference value, like all non-primitive Java types. So if it were possible to modify a Long
or Integer
(it's not, they're immutable without using JNI code), there would be another reason to use one over the other.
A final difference is that a Long
or Integer
could be null
.
ArrayListInteger vs ArrayListString - both storing values from 0 to 9 .. which takes more memory?
Here is a test result using this tool, it shows that ArrayList<Integer>
occupies less memory:
public static void main(String[] args) {
ArrayList<Integer> integerArrayList = new ArrayList<>();
ArrayList<String> stringArrayList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
integerArrayList.add(i);
stringArrayList.add(String.valueOf(i));
}
System.out.println(RamUsageEstimator.sizeOf(integerArrayList)); // 240
System.out.println(RamUsageEstimator.sizeOf(stringArrayList)); // 560
}
Memory allocation/reference comparison for ints and Integers
Because of boxing and unboxing in java
Converting an int to an Integer, a double to a Double, and so on. If
the conversion goes the other way, this is called unboxing.
Autoboxing/unboxing is a pure convenience feature that allows you to assign values of a primitive type a reference of a wrapper class and vice versa, with the compiler automatically adding the code to convert between the two.
Boxing and unBoxing and SEE HERE ALSO
Related Topics
Found Shared References to a Collection Org.Hibernate.Hibernateexception
Refreshing Static Content with Spring MVC and Boot
Making a Log4J Console Appender Use Different Colors for Different Threads
Arraylist Contains Case Sensitivity
Access to File Using Java with Samba Jcifs
Unsatisfieddependencyexception: Error Creating Bean with Name
Read Error Response Body in Java
Fastest Way to Iterate an Array in Java: Loop Variable VS Enhanced for Statement
How to Copy Hashmap (Not Shallow Copy) in Java
Java Raw Type and Generics Interaction
How to Get Annotations of a Member Variable
Is There a Way in Java to Convert an Integer to Its Ordinal Name
Parsing JSON Array Within JSON Object
How to Change Java Version Used by Tomcat
Java Try/Catch/Finally Best Practices While Acquiring/Closing Resources