What is the difference between an int and an Integ

2018-12-31 16:30发布

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?

24条回答
十年一品温如言
2楼-- · 2018-12-31 16:49

"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.

查看更多
像晚风撩人
3楼-- · 2018-12-31 16:50

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 :

int x;
Integer y; 

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.

Integer.toString(x);

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).

查看更多
听够珍惜
4楼-- · 2018-12-31 16:51

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 on int. The methods in Integer 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 (like toString(), 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 for System.Int32.When using integer-related methods like int.Parse(), int.ToString() etc. Integer is compiled into the FCL System.Int32 struct calling the respective methods like Int32.Parse(), Int32.ToString().

查看更多
人间绝色
5楼-- · 2018-12-31 16:53

In C#, int is just an alias for System.Int32, string for System.String, double for System.Double etc...

Personally I prefer int, string, double, etc. because they don't require a using System; statement :) A silly reason, I know...

查看更多
泛滥B
6楼-- · 2018-12-31 16:53

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 generics Integer is not used but int is used. so there is so such difference there.

查看更多
素衣白纱
7楼-- · 2018-12-31 16:54

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.

查看更多
登录 后发表回答