Can a primitive value be considered an object in java?

后端 未结 3 789
长情又很酷
长情又很酷 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条回答
  •  情话喂你
    2021-01-19 18:31

    Can a primitive value be considered an object?

    The answer is No.

    The JLS states

    There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3).

    Those are Primitive Types and Values which are

    predefined by the Java programming language and named by its reserved keyword

    and Reference Types and Values which can be one of

    class types (§8), interface types (§9), type variables (§4.4), and array types (§10).

    Note also that there is a special type which is the null type and its corresponding value the null reference

    There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.

    ...

    The null reference is the only possible value of an expression of null type.

    For the primitive types the JLS has defined a mechanism called Boxing Conversion for converting them into the corresponding reference types.

    Boxing conversion converts expressions of primitive type to corresponding expressions of reference type.

    So since there is the need for a conversion to go from a primitive type to a corresponding reference type one cannot say that primitives can be considered as objects and vice versa. But one can say they are convertible.

提交回复
热议问题