Can a primitive value be considered an object in java?

后端 未结 3 883
长情又很酷
长情又很酷 2021-01-19 18:01

When I started out using Java, it was implied to me that we have two different types:

Objects (strings, from classes, println, etc)
primitive values (int, do         


        
3条回答
  •  Happy的楠姐
    2021-01-19 18:21

    There is an implicit mapping between primitives and objects, so an int can be used where an Integer is expected. It is a feature called autoboxing.

    For example:

    Integer foo = 4; // 4 is really an int
    

    Also, primitives have things in common with objects. They have a Class, such as int.class.

    It should also be noted that all arrays are objects.

    However, the notion of Object vs primitive is one of the worst thing there is in Java. A lot of modern languages simply don't make too much difference between them, or even don't have any primitives. (In java as well, you can do everything without ever using explicitly a primitive).

提交回复
热议问题