Java Primitive Types: int vs. Integer

前端 未结 6 1393
予麋鹿
予麋鹿 2020-12-03 14:02

I am confused about when to use primitive vs. non-primitive(?) types (i.e. int vs. Integer) in Java. I realize that in some places you can\'t use primitive types (for exampl

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 14:30

    In Java, int is a primitive data type, while Integer is a Wrapper class.

    int, being a primitive data type has less flexibility. We can only store the binary value of an integer in it. Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating integer data. Integer is a class and thus it can call various in-built methods defined in the class. Variables of type Integer store references to Integer objects, just as with any other reference (object) type.

    You can find a more detailed explanation here.

提交回复
热议问题