简体   繁体   中英

Is a primitive type an object?

The start of the Reflection tutorial @ the Java Tutorials states:

Every object is either a reference or primitive type.

Apart from the types used to box primitive types, when and how is a primitive type an object?

It says object , not Object . int , for example, is a primitive type and an object (interpret as the general termn), but not an Object .

The classification is exclusive, that means that if a variable is a primitive type, it is not a reference to an Object (an entity that has attributes and methods).

In this case, the author is using the term object (in lowercase) as a synonym of a thing (variable, parameter, constant, attribute, whatever) that holds a value.

According to the Java Language Specification:

An object is a class instance or an array.

Here's the relevant paragraph .

However there is a source for misunderstandings in the fact that there are contexts where the term object is used to mean roughly any piece of memory that can be explicitly referenced in a program. With this definition attributes and variables of primitive types are indeed objects.

・The types of the Java programming language are divided into two categories: primitive types and reference types.

・The primitive types are the boolean type and the numeric types.

・The reference types are class types, interface types, and array types. There is also a special null type.

・An object is a dynamically created instance of a class type or a dynamically created array.

So, a primitive type can't be an object. But if you say everything in the world is object, then it is.

I don't think it is an object. An object is a type of something with definitions on how you can create it (contructor). A primitive type doesn't have this, it's value gets stored directly onto memory.

I would assume that the same would apply for classes.

class and java.lang.Class

In Java a primitive type cannot be an Object.

The primitive types are not accessed with references, but direct. If you pass them to a method, they are copied. If you pass an Object, only the reference to it is copied. Primitives don't have any overhead in memory, like Objects, they need as much bytes as specified in their definition (for example int needs 4). Using an Integer Object, it uses more than the 4 Integer bytes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM