What is the distinction in between an int and also an Integer in Java and also C#?

I read More Joel on Software when I found Joel Spolsky claiming something concerning a certain sort of designer recognizing the distinction in between an int and also an Integer in Java/C # (Object Oriented Programming Languages ).

So, what is the distinction?

2019-05-07 13:23:10
Source Share
Answers: 9

Regarding Java 1.5 and also autoboxing there is a vital "trait" that involves play when contrasting Integer things.

In Java, Integer things with the values - 128 to 127 are unalterable (that is, for one certain integer value, claim 23, all Integer things instantiated via your program with the value 23 indicate the specific very same object).

Instance, this returns real :

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

While this returns incorrect :

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

The = = contrasts by reference (does the variables indicate the very same object).

This outcome might or might not vary relying on what JVM you are making use of. The requirements autoboxing for Java 1.5 calls for that integers (- 128 to 127) constantly box to the very same wrapper object.

A remedy? =) One need to constantly make use of the Integer.equals () method when contrasting Integer things.

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

More details at java.net Example at bexhuff.com

2019-05-10 13:42:08

One even more point that I do not see in previous solutions : In Java the primitive wrappers courses like Integer, Double, Float, Boolean ... and also String are suposed to be stable, to make sure that when you pass an instance of those courses the invoked method could not modify your information at all, in opositiĆ³n with a lot of various other courses, which inner information can be modified by its public approaches. To make sure that this courses just has 'getter' approaches, no 'setters', besides the erector.

In a java program String literals are saved in a different section of lot memory, just a instance for actual, to conserve memory recycling those circumstances

2019-05-10 09:22:07

In Java there are 2 standard key ins the JVM. 1) Primitive kinds and also 2) Reference Types. int is a primitive type and also Integer is a class type (which is sort of reference type).

Primitive values do not share state with various other primitive values. A variable whose type is a primitive type constantly holds a primitive value of that type.

int aNumber = 4;
int anotherNum = aNumber;
aNumber += 6;
System.out.println(anotherNum); // Prints 4

An object is a dynamically developed class instance or an array. The reference values (usually simply referrals) are reminders to these things and also an unique null reference, which describes no object. There might be several referrals to the very same object.

Integer aNumber = Integer.valueOf(4);
Integer anotherNumber = aNumber; // anotherNumber references the 
                                 // same object as aNumber

Also in Java every little thing is gone by value. With things the value that is passed is the reference to the object. So an additional distinction in between int and also Integer in java is just how they are come on method telephone calls. As an example in

public int add(int a, int b) {
    return a + b;
final int two = 2;
int sum = add(1, two);

The variable 2 is passed as the primitive integer type 2. Whereas in

public int add(Integer a, Integer b) {
    return a.intValue() + b.intValue();
final Integer two = Integer.valueOf(2);
int sum = add(Integer.valueOf(1), two);

The variable 2 is passed as a reference to an object that holds the integer value 2.

@WolfmanDragon : Pass by reference would certainly function thus :

public void increment(int x) {
  x = x + 1;
int a = 1;
// a is now 2

When increment is called it passes a reference (reminder) to variable a . And also the increment function straight changes variable a .

And also for object kinds it would certainly function as adheres to :

public void increment(Integer x) {
  x = Integer.valueOf(x.intValue() + 1);
Integer a = Integer.valueOf(1);
// a is now 2

Do you see the distinction currently?

2019-05-10 06:04:47

In Java, the 'int' type is a primitive, whereas the 'Integer' type is an object.

In C#, the 'int' type coincides as System.Int32 and also is a value type (ie extra like the java 'int'). An integer (similar to any kind of various other value kinds) can be boxed (" covered") right into an object.

The distinctions in between things and also primitives are rather past the extent of this inquiry, yet to sum up:

Objects give centers for polymorphism, are gone by reference (or extra properly have actually referrals gone by value), and also are alloted from the heap. Alternatively, primitives are unalterable kinds that are gone by value and also are usually alloted from the stack.

2019-05-09 08:20:32

Well, in Java an int is a primitive while an Integer is an Object. Definition, if you made a new Integer:

Integer i = new Integer(6);

You can call some method on i:

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

Whereas with an int:

int i = 6;

You can not call any kind of approaches on it, due to the fact that it is merely a primitive. So:

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

would certainly generate a mistake, due to the fact that int is not an object.

int is just one of minority primitives in Java (in addition to char and also a few other). I'm not 100% certain, yet I'm assuming that the Integer object essentially simply has an int building and also an entire number of approaches to connect with that said building (like the toString () method as an example). So Integer is an expensive means to collaborate with an int (Just as probably String is an expensive means to collaborate with a team of chars).

I recognize that Java isn't C, yet given that I've never ever set in C this is the closest I can involve the solution. Hope this aids!

Integer object javadoc

Integer Ojbect vs. int primitive comparison

2019-05-09 08:19:15

In C#, int is simply an pen name for System.Int32, string for System.String, double for System.Double and so on.

Directly I favor int, string, double, etc due to the fact that they do not call for a using System; declaration :) A foolish factor, I recognize ...

2019-05-09 08:16:26

I'll simply upload below given that several of the various other blog posts are a little imprecise in regard to C#.

Proper : int is a pen name for System.Int32.
Incorrect : float is not a pen name for System.Float, but also for System.Single

Basically, int is a scheduled search phrase in the C# shows language, and also is a pen name for the System.Int32 value type.

float and also Float is not the very same nonetheless, as the appropriate system type for "float" is System.Single. There are some kinds similar to this that has actually booked search phrases that does not appear to match the type names straight.

In C# there is no distinction in between "int" and also "System.Int32", or any one of the various other sets or keywords/system kinds, with the exception of when specifying enums. With enums you can define the storage space dimension to make use of and also in this instance you can just make use of the scheduled search phrase, and also not the system runtime type name.

Wether the value in the int will certainly be saved on the pile, in memory, or as a referenced lot object relies on the context and also just how you utilize it.

This declaration in a method:

int i;

specifies a variable i of type System.Int32, staying in a register or on the pile, relying on optimizations. The very same declaration in a type (struct or class) specifies a participant area. The very same declaration in a method argument checklist specifies a parameter, with the very same storage space alternatives when it comes to a neighborhood variable. (note that this paragraph is not legitimate if you start drawing iterator approaches right into the mix, these are various monsters completely)

To get a lot object, you can make use of boxing:

object o = i;

this will certainly create a boxed duplicate of the materials of i on the lot. In IL you can access approaches on the lot object straight, yet in C# you require to cast it back to an int, which will certainly create an additional duplicate. Hence, the object on the lot can not conveniently be transformed in C# without developing a new boxed duplicate of a new int value. (Ugh, this paragraph does not read all that conveniently.)

2019-05-09 05:52:26

I'll add to the superb solutions offered over, and also speak about boxing and also unboxing, and also just how this relates to Java (although C# has it also). I'll make use of simply Java terms, due to the fact that I am even more au fait with that said.

As the solutions stated, int is simply a number (called the unboxed type), whereas Integer is an object (which has the number, therefore a boxed type). In Java terms, that suggests (in addition to not having the ability to call approaches on int), you can not store int or various other non - object key ins collections (List, Map, etc). In order to store them, you have to first box them up in its equivalent boxed type.

Java 5 onwards have actually something called vehicle - boxing and also vehicle - unboxing which permit the boxing/unboxing to be done behind the scenes. Contrast and also comparison : Java 5 variation :

Deque<Integer> queue;

void add(int n) {

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

Java 1.4 or earlier (no generics either) :

Deque queue;

void add(int n) {

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

It has to be kept in mind that regardless of the brevity in the Java 5 variation, both variations create the same bytecode. Hence, although vehicle - boxing and also vehicle - unboxing is really hassle-free due to the fact that you write much less code, these procedures do take place behind the scenes, with the very same runtime prices, so you still need to recognize their presence.

Hope this aids!

2019-05-08 14:58:02

This has actually currently been addressed for Java, below's the C# solution :

" Integer" is not a legitimate type name in C# and also "int" is simply a pen name for System.Int32. Additionally, unlike in Java (or C++) there aren't any kind of unique primitive key ins C#, every instance of a key in C# (consisting of int) is an object. Below's some demonstrative code :

void DoStuff()
    System.Console.WriteLine( SomeMethod((int)5) );
    System.Console.WriteLine( GetTypeName<int>() );

string SomeMethod(object someParameter)
    return string.Format("Some text {0}", someParameter.ToString());

string GetTypeName<T>()
    return (typeof (T)).FullName;
2019-05-08 10:26:21