我在阅读《关于软件的更多Joel》时,偶然看到Joel Spolsky说,有一种特殊类型的程序员知道Java/ c#(面向对象编程语言)中int和Integer的区别。
那么,有什么不同呢?
我在阅读《关于软件的更多Joel》时,偶然看到Joel Spolsky说,有一种特殊类型的程序员知道Java/ c#(面向对象编程语言)中int和Integer的区别。
那么,有什么不同呢?
当前回答
使用包装类的原因有很多:
我们得到了额外的行为(例如,我们可以使用方法) 我们可以存储空值,而在原语中则不能 集合支持存储对象,而不支持存储原语。
其他回答
int变量保存一个32位有符号整数值。Integer(大写I)保存对(类)类型Integer或null对象的引用。
Java自动在两者之间进行类型转换;当Integer对象作为int操作符的参数出现,或者被赋值给int变量,或者一个int值被赋值给Integer变量时,从Integer到int。这种类型转换称为装箱/解装箱。
如果一个引用null的Integer变量被显式或隐式地解盒,则抛出NullPointerException异常。
我将对上面给出的优秀答案进行补充,并讨论装箱和拆箱,以及如何将其应用于Java(尽管c#也有)。我将只使用Java术语,因为我对它更熟悉。
正如答案所提到的,int只是一个数字(称为未装箱类型),而Integer是一个对象(包含数字,因此是装箱类型)。在Java术语中,这意味着(除了不能在int上调用方法之外),您不能在集合(List, Map等)中存储int或其他非对象类型。为了存储它们,必须首先将它们装入相应的盒装类型中。
Java 5以后有了所谓的自动装箱和自动拆箱,允许装箱/拆箱在幕后完成。比较和对比:Java 5版本:
Deque<Integer> queue;
void add(int n) {
queue.add(n);
}
int remove() {
return queue.remove();
}
Java 1.4或更早版本(也没有泛型):
Deque queue;
void add(int n) {
queue.add(Integer.valueOf(n));
}
int remove() {
return ((Integer) queue.remove()).intValue();
}
必须注意的是,尽管Java 5版本很简洁,但两个版本生成的字节码是相同的。因此,尽管自动装箱和自动拆箱非常方便,因为您编写的代码更少,但这些操作确实发生在幕后,具有相同的运行时成本,因此您仍然必须知道它们的存在。
希望这能有所帮助!
在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
}
}
附注:赫伯特·斯希尔特的书被作为参考。
int is a primitive datatype whereas Integer is an object. Creating an object with Integer will give you access to all the methods that are available in the Integer class. But, if you create a primitive data type with int, you will not be able to use those inbuild methods and you have to define them by yourself. But, if you don't want any other methods and want to make the program more memory efficient, you can go with primitive datatype because creating an object will increase the memory consumption.