-2

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?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
BVBC
  • 375
  • 2
  • 4
  • 11

2 Answers2

3

What is an object?

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).

Are primitive types objects?

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.

Primitive types wrapped as objects in Java

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.

augray
  • 3,043
  • 1
  • 17
  • 30
  • 3
    [`std::is_object`](http://en.cppreference.com/w/cpp/types/is_object) would like to disagree with you regarding C++. – Kerrek SB May 25 '15 at 22:19
  • @Kerrek Thanks for the tip. I have adjusted my answer. – augray May 25 '15 at 22:31
  • C++ has always (AFAIK) had the definition of object be "a region of storage", so yeah, it's different than you would anticipate when coming from other languages if you want to be pedantic about the definition. In reality, it's probably easier in some contexts to use "object" for class variables. – chris May 25 '15 at 23:06
  • @chris: If you provide a link to a good definition to that effect, I'll include it the c++ part. Most of what I see in my web searches defines objects in relation to classes. – augray May 25 '15 at 23:09
  • 1
    @chris: Meh, that's a can of worms. When I teach people about storage duration of objects, I don't want anyone thinking that a static vector and a static integer are somehow different. I can't speak for Java, but please don't make C++ any more complicated than it already is. – Kerrek SB May 25 '15 at 23:46
  • 2
    @KerrekSB, Alright, I'll buy that. And augray, [N4140](https://github.com/cplusplus/draft/blob/master/papers/n4140.pdf) §1.8 [intro.object]/1 contains the definition. You could consult other drafts of the standard for other versions of the language. – chris May 26 '15 at 00:47
  • Also relevant is this [paper by the creator of c++](http://www.stroustrup.com/whatis.pdf). In section 2.5 he seems to define object-oriented programming in terms of a language supporting a distinction between the properties of a type and the properties of an instance of a type. "type" is broader than "class" and thus would encompass primitive types. *Note- this isn't a paper about c++ specifically, but would be of interest to those looking for an exact def of an "object-oriented" language. – augray May 26 '15 at 04:11
1

variable1 is not an object in Java - it's simply the primitive type and holds the actual value you have assigned it in memory.

Andrew Fan
  • 1,313
  • 5
  • 17
  • 29