33

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Zach Shallbetter
  • 1,901
  • 6
  • 23
  • 37
  • 3
    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. – Mansoor Siddiqui Dec 27 '11 at 23:20
  • 1
    Good advice, determining what he was getting at would have helped. – Zach Shallbetter Dec 28 '11 at 19:55

5 Answers5

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.

Mithun Sasidharan
  • 20,572
  • 10
  • 34
  • 52
  • 1
    Might be good to point out that since es6 classes can be defined in JavaScript too. – faerin Jan 06 '19 at 19:38
  • 1
    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 : ) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes – Zachary Ryan Smith Sep 16 '21 at 13:48
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.
Bhushan
  • 18,329
  • 31
  • 104
  • 137
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.)

N.Neupane
  • 281
  • 1
  • 4
  • 17
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

ejderuby
  • 710
  • 5
  • 21
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
}
bmck
  • 211
  • 2
  • 6