我在阅读《关于软件的更多Joel》时,偶然看到Joel Spolsky说,有一种特殊类型的程序员知道Java/ c#(面向对象编程语言)中int和Integer的区别。
那么,有什么不同呢?
我在阅读《关于软件的更多Joel》时,偶然看到Joel Spolsky说,有一种特殊类型的程序员知道Java/ c#(面向对象编程语言)中int和Integer的区别。
那么,有什么不同呢?
当前回答
嗯,在Java中,int是一个原语,而Integer是一个对象。意思是,如果你创建了一个新的Integer:
Integer i = new Integer(6);
你可以在i上调用一些方法:
String s = i.toString();//sets s the string representation of i
而对于int类型:
int i = 6;
您不能在它上调用任何方法,因为它只是一个原语。所以:
String s = i.toString();//will not work!!!
会产生一个错误,因为int不是一个对象。
int是Java中为数不多的基本类型之一(还有char和其他一些基本类型)。我不是100%确定,但我认为Integer对象或多或少只是有一个int属性和一大堆方法与该属性进行交互(例如toString()方法)。因此Integer是处理int的一种奇特方式(就像String是处理一组字符的奇特方式一样)。
我知道Java不是C语言,但由于我从未用C语言编程,这是我能找到的最接近答案的方法。
整数对象javadoc
Integer Ojbect与int原语比较
其他回答
在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, double, long, byte, float, double, short, boolean, char - primitives。用于保存基本数据类型 由语言支持。类的基本类型不是 对象层次结构,并且它们不继承object。不能通过对方法的引用来传递。
Double、Float、Long、Integer、Short、Byte、Character和Boolean都是类型包装器,打包在java.lang中。所有数字类型包装器都定义了构造函数,允许从给定值或该值的字符串表示形式构造对象。 即使是最简单的计算,使用对象也会增加开销。
从JDK 5开始,Java包含了两个非常有用的特性:自动装箱和自动装箱。自动装箱/拆箱极大地简化和简化了必须将基本类型转换为对象的代码,反之亦然。
构造函数的例子:
Integer(int num)
Integer(String str) throws NumberFormatException
Double(double num)
Double(String str) throws NumberFormatException
装箱/拆箱的例子:
class ManualBoxing {
public static void main(String args[]) {
Integer objInt = new Integer(20); // Manually box the value 20.
int i = objInt.intValue(); // Manually unbox the value 20
System.out.println(i + " " + iOb); // displays 20 20
}
}
autoboxing/autounboxing的例子:
class AutoBoxing {
public static void main(String args[]) {
Integer objInt = 40; // autobox an int
int i = objInt ; // auto-unbox
System.out.println(i + " " + iOb); // displays 40 40
}
}
附注:赫伯特·斯希尔特的书被作为参考。
(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...}
Int用于声明原始变量
e.g. int i=10;
Integer用于创建类Integer的引用变量
Integer a = new Integer();
“int”是原始数据类型,是Java包装类中的“Integer”。“Integer”可以用作需要对象的方法的参数,而“int”可以用作需要整数值的方法的参数,可用于算术表达式。