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

前端 未结 26 1473
生来不讨喜
生来不讨喜 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:29

    An int variable holds a 32 bit signed integer value. An Integer (with capital I) holds a reference to an object of (class) type Integer, or to null.

    Java automatically casts between the two; from Integer to int whenever the Integer object occurs as an argument to an int operator or is assigned to an int variable, or an int value is assigned to an Integer variable. This casting is called boxing/unboxing.

    If an Integer variable referencing null is unboxed, explicitly or implicitly, a NullPointerException is thrown.

提交回复
热议问题