I was reading More Joel on Software when I came across Joel Spolsky saying something about a particular type of programmer knowing the difference between an int
and an Integer
in Java/C# (Object Oriented Programming Languages).
So, what is the difference?
In platforms like Java,
int
s are primitives whileInteger
is an object which holds a integer field. The important distinction is that primitives are always passed around by value and by definition are immutable.Any operation involving a primitive variable always returns a new value. On the other hand, objects are passed around by reference. One could argue that the point to the object (AKA the reference) is also being passed around by value, but the contents are not.
This has already been answered for Java, here's the C# answer:
"Integer" is not a valid type name in C# and "int" is just an alias for System.Int32. Also, unlike in Java (or C++) there aren't any special primitive types in C#, every instance of a type in C# (including int) is an object. Here's some demonstrative code:
In Java, the
int
type is a primitive data type, where as theInteger
type is an object.In C#, the
int
type is also a data type same asSystem.Int32
. Aninteger
(just like any other value types) can be boxed ("wrapped") into an object.I'll just post here since some of the other posts are slightly inaccurate in relation to C#.
Correct:
int
is an alias forSystem.Int32
.Wrong:
float
is not an alias forSystem.Float
, but forSystem.Single
Basically, int is a reserved keyword in the C# programming language, and is an alias for the
System.Int32
value type.float and Float is not the same however, as the right system type for ''
float
'' is System.Single. There are some types like this that has reserved keywords that doesn't seem to match the type names directly.In C# there is no difference between ''
int
'' and ''System.Int32
'', or any of the other pairs or keywords/system types, except for when defining enums. With enums you can specify the storage size to use and in this case you can only use the reserved keyword, and not the system runtime type name.Wether the value in the int will be stored on the stack, in memory, or as a referenced heap object depends on the context and how you use it.
This declaration in a method:
defines a variable
i
of typeSystem.Int32
, living in a register or on the stack, depending on optimizations. The same declaration in a type (struct or class) defines a member field. The same declaration in a method argument list defines a parameter, with the same storage options as for a local variable. (note that this paragraph is not valid if you start pulling iterator methods into the mix, these are different beasts altogether)To get a heap object, you can use boxing:
this will create a boxed copy of the contents of
i
on the heap. In IL you can access methods on the heap object directly, but in C# you need to cast it back to an int, which will create another copy. Thus, the object on the heap cannot easily be changed in C# without creating a new boxed copy of a new int value. (Ugh, this paragraph doesn't read all that easily.)An int variable holds a 32 bit signed integer value. An Integer (with capital I) holds a reference to an object of (class) type Integer, or to null.
Java automatically casts between the two; from Integer to int whenever the Integer object occurs as an argument to an int operator or is assigned to an int variable, or an int value is assigned to an Integer variable. This casting is called boxing/unboxing.
If an Integer variable referencing null is unboxed, explicitly or implicitly, a NullPointerException is thrown.
int is used to declare primitive variable
Integer is used to create reference variable of class Integer