object vs. primitive
Asked Answered
C

5

34

Recently had an interviewer ask to define the difference between objects and primitives. Seemed like an odd question considering that all languages begin with a primitive. How would you have answered this question?

I should also note that this interview was for a front-end development position so the language (I assume) he was referring to was JavaScript.

Cramoisy answered 27/12, 2011 at 9:54 Comment(2)
You mentioned that you assume that the interviewer was referring to JavaScript. I think you should have asked him just to be sure -- no point in guessing about what he's asking when a simple question would have clarified that.Defamatory
Good advice, determining what he was getting at would have helped.Cramoisy
G
49

A primitive is a data type that is composed of no other data types and can not be broken down any further. It is like the atoms in the programming scenario. I say atom because atom is a basic unit of matter and there is nothing that can be derived from it.

I mean, an int in C can not be broken down into smaller data type. An object, on the other hand can be thought of a molecule, consisting of more than one primitive type. For example, string comes as part of the C++ standard library; however, it is an object and it is composed of smaller data types internally and contains methods.

It is important to note that not all object-oriented languages are class based (eg. Javascript) You can not define a class in Javascript, so an object is quite different here. Even though everything in Javascript is an object (Ruby also), the Number object is really a wrapper for an internal primitive.

Guenna answered 27/12, 2011 at 9:56 Comment(2)
Might be good to point out that since es6 classes can be defined in JavaScript too.Creed
es6 classes are just syntax-sugar: under the hood, they make classes in the same way we did pre-es6. That is, prototype-based classes. This makes them different from most languages' classes. Does this difference matter? Maybe more frequently in interviews than in real life : ) developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Enjoin
T
27

From Java perspective:

  1. A primitive is not composed of other data types. Where as an object can be and generally is.
  2. Primitives are passed by value, i.e. a copy of the primitive itself is passed. Whereas for objects, the copy of the reference is passed, not the object itself.
  3. Primitives are independent data types, i.e. there does not exist a hierarchy/super class for them. Whereas every Object is descendent of class "Object".
  4. Every object has some default methods of itself, which it inherits from the class Object (e.g. toString(), clone(), wait(), notify(), notifyAll(), etc.). The primitives does not have any method associated with themselves.
  5. Primitives are immutable, they can be used as so without any special care. Whereas for objects, special care needs to be taken, they are not immutable by default.
  6. With objects, there are two types of copies, Shallow and Deep. There is a significant difference between them. So the choice depends on how do we intend to use them. With primitives, there is no such difference, everything is by default deep copy only.
Theatrical answered 27/12, 2011 at 21:35 Comment(0)
T
3

Let us Visualize the actual differences.

Primitives

1) A primitive data type uses a small amount of memory to represent a single item of data. All data of the same primitive type are the same size.

For Example: Primitive type int represents integers using 32 bits. All variables of type int use 32 bits.

2) There are only eight primitive data types in Java: byte, short, int, long, float, double, char, and boolean. A Java program cannot define any other primitive data types.

Objects

An object is a large chunk of memory that can potentially contain a great deal of data along with methods (little programs) to process that data. There are thousands of object classes that come standard with Java, and a programmer can easily create additional classes. (Although there are thousands of standard classes, for this course you only need become familiar with a dozen or so classes.)

Tonetic answered 18/12, 2019 at 1:27 Comment(0)
B
1

I think primitive cannot divided any more like int, string (such as built-in data type) . On the other way, object can divide into small pieces like array, structure.

I am just a student and this is my opinion.
If you think my answer is wrong, you can correct me.
Thanks

Becht answered 9/8, 2019 at 17:34 Comment(0)
I
1

In Java, when you send k field to test method, firstly it convert to Integer type. Because a primitive type is not one of sub types of Object type.

public static void main(String[] args) {
    int k=3;
    test(k);
    test2(k);
}

private static void test(Object k) {
    System.out.print(k.getClass().isPrimitive());//false
}

private static <T> void test2(T k) {
    System.out.print(k.getClass().isPrimitive());//false
}
Ioneionesco answered 12/12, 2021 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.