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?
"int" is primitive data-type and "Integer" in Wrapper Class in Java. "Integer" can be used as an argument to a method which requires an object, where as "int" can be used as an argument to a method which requires an integer value, that can be used for arithmetic expression.
have you ever programmed before then (int) is one of the primitive types you can set for your variables (just like char, float, ...).
but Integer is a wrapper class that you can use it to do some functions on an int variable (e.g convert it to string or vise versa,...) , but keep note that methods in the wrapper classes are static so you can use them anytime without creating an instance of Integer class. as a recap :
x and y are both variables of type int but y is wrapped by an Integer class and has several methods that you use,but i case you need to call some functions of Integer wrapper class you can do it simply.
but be aware that both x and y are corect but if you want to use them just as a primitive type, use the simple form (used for defining x).
In both languages (Java and C#)
int
is 4-byte signed integer.Unlike Java, C# Provides both signed and unsigned integer values. As Java and C# are object object-oriented, some operations in these languages do not map directly onto instructions provided by the run time and so needs to be defined as part of an object of some type.
C# provides
System.Int32
which is a value type using a part of memory that belongs to the reference type on the heap.java provides
java.lang.Integer
which is a reference type operating onint
. The methods inInteger
can't be compiled directly to run time instructions.So we box an int value to convert it into an instance of Integer and use the methods which expects instance of some type (liketoString()
,parseInt()
,valueOf()
etc).In C# variable int refers to
System.Int32.Any
4-byte value in memory can be interpreted as a primitive int, that can be manipulated by instance of System.Int32.So int is an alias forSystem.Int32.When
using integer-related methods likeint.Parse()
,int.ToString()
etc. Integer is compiled into the FCLSystem.Int32
struct calling the respective methods likeInt32.Parse()
,Int32.ToString()
.In C#, int is just an alias for
System.Int32
, string forSystem.String
, double forSystem.Double
etc...Personally I prefer int, string, double, etc. because they don't require a
using System;
statement :) A silly reason, I know...In java as per my knowledge if you learner then, when you write int a; then in java generic it will compile code like
Integer a = new Integer()
. So,as per genericsInteger
is not used butint
is used. so there is so such difference there.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.