Does a primitive type variable count as an object in java? For example, does variable1
in
int variable1=10;
count as an object? Or is variable1
just a reference to the object?
I think that variable1
is an object in c++ . Is this correct?
Does a primitive type variable count as an object in java? For example, does variable1
in
int variable1=10;
count as an object? Or is variable1
just a reference to the object?
I think that variable1
is an object in c++ . Is this correct?
The general meaning of the word object in c++ and in Java is the same. An object in a nutshell is something which can both have state and have operations to transform and examine, or utilize that state. This is true in both languages. Another thing that is true in both of these particular languages is that an object is a specific instance of a "class", which describes the characteristics the object has (its supported interface), and possibly how its available operations and state is accessed and maintained (its "implementation"). A helpful analogy is that an object is like a house, built from a blueprint (the class).
Short answer: For Java, no. For c++, Yes.
Java
Primitive types do not count as objects in Java. This is made quite explicit since primitive types do not inherit from the super class Object
(a special class that every object is an instance of).
There are other differences between primitive types and object types in Java, namely that primitive types are "pass by value" (meaning a copy of their data is made when you pass them into methods or assign them to variables), while objects are "pass by reference", meaning that when you pass them into methods or assign them to variables, you get copies of references to the data (manipulations on the state from one reference change the state seen by the other references). Since primitive types like int
are passed by value, we can also tell that they are not objects.
C++
One argument that could be made that primitive types are not objects in c++ is that primitive types are not instances of any class. However, c++11 provides a handy judge to check whether a given type is an object type or not std::is_object
. This returns true
for primitive types like int, which perhaps closes the debate.
The official working draft of the standard for c++ as of 2014 includes the text:
An object is a region of storage ... An object is created by a definition, by a new-expression, or by the implementation when needed.
(Section 1.8. Internal references removed)
Thus for c++, any data that occupies memory and is created by one of the above mechanisms counts as an object. This would include primitive types like an int
, since instances of the primitive types take up memory and are created by one of the above mechanisms (int variable1 = 2;
counts as a definition as defined in section 3.1 of the specification).
C++ value/reference/address You also inquired about whether your variable was a reference in c++. C++ doesn't have the same "all objects are passed by reference" rule that Java does. C++ actually gives you the flexibility to decide how you want to move around your data and references to it. You can pass by reference (similar to Java), value (copy all the data), or address/pointer (similar to reference in that you can use multiple pointers to manipulate a single object's data, but with some additional freedoms/responsibilities). See this description of reference/value/pointer.
In your particular example of int variable1=10;
, variable1
is holding 10
by value, not reference or address.
Many things in Java revolve around the notion of data being objects (ex: you can only put items into a java.util.List
if it is an object). So it can sometimes be useful to be able to treat primitive types as objects. For this reason (and to provide some helpful utilities related to primitive types), the Java language provides wrapper classes for all the primitive types that allow you to use them as objects.
See: Byte, Short, Integer, Long, Float, Double, Character, and Boolean.
variable1 is not an object in Java - it's simply the primitive type and holds the actual value you have assigned it in memory.