What is the difference between an int and an Integer in Java and C#?

前端 未结 26 1736
生来不讨喜
生来不讨喜 2020-11-22 12:00

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 i

26条回答
  •  独厮守ぢ
    2020-11-22 12:19

    An int and Integer in Java and C# are two different terms used to represent different things. It is one of the the primitive data types that can be assigned to a variable that can store exactly. One value of its declared type at a time.

    For example:

    int number = 7;
    

    Where int is the datatype assigned to the variable number which holds the value seven. So an int is just a primitive not an object.

    While an Integer is a wrapper class for a primitive data type which has static methods. That 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.

    For example:

    Integer number = new Integer(5);
    

提交回复
热议问题