我在阅读《关于软件的更多Joel》时,偶然看到Joel Spolsky说,有一种特殊类型的程序员知道Java/ c#(面向对象编程语言)中int和Integer的区别。

那么,有什么不同呢?


当前回答

Int用于声明原始变量

e.g. int i=10;

Integer用于创建类Integer的引用变量

Integer a = new Integer();

其他回答

在c#中,int只是System的别名。Int32,系统的字符串。字符串,double for System。双等等……

我个人更喜欢int, string, double等,因为他们不需要使用系统;这是个愚蠢的理由,我知道……

在Java中,JVM有两种基本类型。1)基本类型和2)引用类型。int是基本类型,Integer是类类型(一种引用类型)。

基元值不与其他基元值共享状态。类型为基本类型的变量总是保存该类型的基本值。

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

对象是动态创建的类实例或数组。引用值(通常只是引用)是指向这些对象的指针和一个特殊的空引用,它不引用任何对象。同一个对象可能有多个引用。

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

在Java中,所有东西都是通过值传递的。对于对象,传递的值是对象的引用。因此,java中int和Integer的另一个区别是它们在方法调用中传递的方式。例如在

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

变量2作为原始整数类型2传递。而在

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

变量two作为一个引用传递给一个保存整数值2的对象。


@WolfmanDragon: 通过引用传递将像这样工作:

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

当increment函数被调用时,它传递一个指向变量a的引用(指针),并且increment函数直接修改变量a。

对于对象类型,它的工作方式如下:

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

现在你看到区别了吗?

(Java版本) 简单来说,int是原始的(不能有空值),Integer是int的包装对象。

一个使用Integer和int的例子,当你想比较int变量时,它会抛出错误。

int a;
//assuming a value you are getting from data base which is null
if(a ==null) // this is wrong - cannot compare primitive to null
{
do something...}

Instead you will use,
Integer a;
//assuming a value you are getting from data base which is null
if(a ==null) // this is correct/legal
{ do something...}

关于Java 1.5和自动装箱,在比较Integer对象时有一个重要的“怪癖”。

在Java中,值为-128到127的Integer对象是不可变的(也就是说,对于一个特定的整数值,比如23,通过程序实例化的所有值为23的Integer对象都指向完全相同的对象)。

例如,返回true:

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

而这返回false:

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

==通过引用进行比较(变量是否指向同一个对象)。

根据所使用的JVM的不同,这个结果可能不同,也可能没有不同。Java 1.5的规范自动装箱要求整数(-128到127)始终装入同一个包装器对象。

一个解决方案吗?当比较Integer对象时,应该总是使用Integer.equals()方法。

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

更多信息请访问bexhuff.com

在Java中,int类型是基本数据类型,而Integer类型是对象。

在c#中,int类型也是一种数据类型,与System.Int32相同。整数(就像任何其他值类型一样)可以被装箱(“包装”)到一个对象中。