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

那么,有什么不同呢?


当前回答

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.

其他回答

在java中,根据我的知识,如果你学习那么,当你写在一个;然后在java泛型中,它将编译像Integer a = new Integer()这样的代码。 因此,根据泛型不使用Integer,而是使用int。 所以这是有区别的。

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”是原始数据类型,是Java包装类中的“Integer”。“Integer”可以用作需要对象的方法的参数,而“int”可以用作需要整数值的方法的参数,可用于算术表达式。

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.

我只在这里发布,因为其他一些帖子在与c#相关方面略有不准确。

正确:int是System.Int32的别名。 错误:float不是System的别名。浮动,但为了系统。单

基本上,int是c#编程语言中保留的关键字,是System的别名。Int32值类型。

float和float是不一样的,因为float的正确系统类型是system . single。有一些这样的类型,其保留关键字似乎与类型名称不直接匹配。

在c#中,“int”和“System”没有区别。Int32 ",或任何其他对或关键字/系统类型,定义枚举时除外。通过枚举,您可以指定要使用的存储大小,在这种情况下,您只能使用保留关键字,而不能使用系统运行时类型名称。

int中的值是存储在堆栈中,还是存储在内存中,还是作为引用的堆对象,这取决于上下文以及如何使用它。

方法中的声明:

int i;

定义一个System类型的变量i。Int32,位于寄存器或堆栈中,这取决于优化。一个类型(结构或类)中的相同声明定义了一个成员字段。方法参数列表中的相同声明定义了一个参数,具有与局部变量相同的存储选项。(注意,如果你开始将迭代器方法混合在一起,这段话是无效的,它们完全是不同的野兽)

要获得一个堆对象,你可以使用装箱:

object o = i;

这将在堆上创建I的内容的盒装副本。在IL中,您可以直接访问堆对象上的方法,但在c#中,您需要将其转换回int类型,这将创建另一个副本。因此,在c#中不容易更改堆上的对象,除非创建一个新的int值的新盒装副本。(啊,这段话读起来可不那么容易。)