Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

object vs. primitive

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.

like image 563
Zach Shallbetter Avatar asked Dec 27 '11 09:12

Zach Shallbetter


People also ask

What is the difference between primitive and object?

Primitive data types are predefined. Object data types are user-defined. These data types are held in a stack. In these data types, the original object is kept in the heap, and the reference variable is kept in the stack.

What is primitives and objects in Java?

Java has a two-fold type system consisting of primitives such as int, boolean and reference types such as Integer, Boolean. Every primitive type corresponds to a reference type. Every object contains a single value of the corresponding primitive type.

Are primitive types objects?

In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties. There are 7 primitive data types: string.

Is object primitive or non-primitive?

Non-Primitive data types refer to objects and hence they are called reference types.


1 Answers

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.

like image 103
Mithun Sasidharan Avatar answered Oct 04 '22 05:10

Mithun Sasidharan