Coder Perfect

In Java and C#, what is the difference between an int and an Integer?


I was reading More Joel on Software when I came across Joel Spolsky talking about a certain type of programmer in Java/C# who knows the difference between an int and an Integer (Object-Oriented Programming Languages).

So, what is the distinction?

Asked by CodingWithoutComments

Solution #1

The type ‘int’ is a primitive in Java, whereas the type ‘Integer’ is an object.

The type ‘int’ is the same as System in C#. Int32 is a value type (similar to Java’s ‘int’). An integer can be wrapped (“boxed”) into an object, just like any other value type.

The distinctions between primitives and objects are a bit outside the scope of this topic, but to summarise:

Polymorphism is supported through objects, which are passed by reference (or, more precisely, have references passed by value) and allocated from the heap. Primitive types, on the other hand, are immutable types that are passed by value and frequently allocated from the stack.

Answered by Matt

Solution #2

An int is a primitive in Java, whereas an Integer is an Object. In other words, suppose you created a new Integer:

Integer i = new Integer(6);

On i, you might use the following method:

String s = i.toString();//sets s the string representation of i

With an int, however, the situation is reversed:

int i = 6;

Because it is a primitive, you can’t call any methods on it. So:

String s = i.toString();//will not work!!!

Because int is not an object, this would result in an error.

In Java, int is one of the few primitives. I’m not certain, but I believe the Integer object consists primarily of an int property and a number of methods for interacting with that value (such as the toString() function). Integer is a fancy technique of working with an integer.

I realize that Java isn’t the same as C, but because I’ve never programmed in C, this is the best I could do. I hope this information is useful.

Integer object javadoc

Comparison of integer Ojbect vs. int primitive

Answered by cmcculloh

Solution #3

I’ll add to the excellent responses above by discussing boxing and unpacking, as well as how this applies to Java (though C# has it as well). I’ll stick to Java jargon because I’m more familiar with it.

Integer is an object, whereas int is just a number (called the unboxed type) as described in the replies (which contains the number, hence a boxed type). In Java, this means that you can’t put int or other non-object types in collections, in addition to not being able to invoke methods on them (List, Map, etc.). To store them, you must first place them in the appropriate boxed kind.

Auto-boxing and auto-unpacking are features in Java 5 and later that allow boxing and unboxing to be done behind the scenes. Contrast and compare: Version 5 of Java:

Deque<Integer> queue;

void add(int n) {

int remove() {
    return queue.remove();

Java 1.4 or earlier (without generics):

Deque queue;

void add(int n) {

int remove() {
    return ((Integer) queue.remove()).intValue();

It’s worth noting that, despite the Java 5 version’s simplicity, both versions generate identical bytecode. Although auto-boxing and auto-unboxing are useful since they need less code, they occur behind the scenes and incur the same runtime costs, so you must be aware of their presence.

Hope this helps!

Answered by Chris Jester-Young

Solution #4

Because some of the other posts are slightly wrong in relation to C#, I’ll just publish here.

Integer is an alias for System. Int32. False: float is not a synonym for System. Float, but this time for System. Single

In the C# programming language, int is a reserved keyword that is an alias for System. The value type is int32.

However, float and float are not synonymous, as the correct system type for “float” is System. Single. Some kinds, such as this, have reserved keywords that don’t appear to immediately match the type names.

Except for defining enums, there is no difference in C# between “int” and “System.Int32”, or any of the other pairs of keywords/system types. You can define the storage capacity to utilize with enums, and you can only use the reserved keyword instead of the system runtime type name in this instance.

The context and how you utilize the int determine whether the value is stored on the stack, in memory, or as a referenced heap object.

In a method, this declaration:

int i;

defines the type System variable i. Depending on optimizations, an Int32 may reside in a register or on the stack. A member field is defined by the same declaration in a type (struct or class). A parameter is defined by the same declaration in a method argument list, with the same storage options as a local variable. (Note that if you add iterator methods to the mix, this paragraph will no longer be valid; these are two completely separate animals.)

You can use boxing to retrieve a heap object:

object o = i;

This will create a boxed copy of I on the heap’s contents. In IL, you may directly call heap object methods, but in C#, you must cast it back to an int, which creates a new copy. As a result, changing the object on the heap in C# requires generating a new boxed copy of a new int value. (Ugh, this paragraph isn’t really easy to read.)

Answered by Lasse V. Karlsen

Solution #5

When comparing Integer objects in Java 1.5 and autoboxing, there is a significant “quirk” that comes into play.

In Java, integer objects having values ranging from -128 to 127 are considered immutable (that is, for one particular integer value, say 23, all Integer objects instantiated through your program with the value 23 points to the exact same object).

This, for example, returns true:

Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2); //  true

While this gives a false result,

Integer i1 = new Integer(128);
Integer i2 = new Integer(128);
System.out.println(i1 == i2); //  false

y citation (does the variables point to the same object).

Depending on the JVM you’re using, this result may or may not be different. Integers (-128 to 127) must always box to the same wrapper object, according to the Java 1.5 specification autoboxing.

Is there a solution? =) When comparing Integer objects, the Integer.equals() function should always be used.

System.out.println(i1.equals(i2)); //  true

More information is available at For instance, have a look at

Answered by andynil

Post is based on