Question:
What is the difference between an object and a primitive?
cwells126
2008-01-03 18:35:37 UTC
What is the difference between an object and a primitive?
Eight answers:
Cipher
2008-01-06 09:47:39 UTC
Some good answers here, but not all are 100% correct. To put it simply, 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 of the programming world. For example, an int in C can not be broken down into smaller data type. An object 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. This does not mean that string in another language is not a primitive. If it is designed to be as a builtin type, then it certainly may. 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.
puredotnet
2008-01-03 18:47:54 UTC
Hubert is on the right track but that is not entirely correct. Primitive types are the "built-in" data types for the programming language, however, each of them is also an object.



Also, characters and strings are also primitive types. A custom object would be more like a "Car" class. In Object Oriented Programming almost everything is an object, this is evidenced very clearly in the .Net framework where, in fact the base class for EVERYTHING is object.



I would recommend some basic reading on Object Oriented Programming to get you more familiar with the concepts.



Good Luck
2016-03-17 05:22:30 UTC
Alex, your question can be construed several ways. All the guys give truth. In java, we have the primitives such as int, double, boolean, short, long.... These parts of the language are lengths of bytes. They can only hold a value. But also we have API types such as Integer, Double, Boolean, Short, Long... (note the caps) Objects have: states, values, and behaviors. One 'thing' holds a collection of different types information. The Object types can all interface with java's Collections. The primitives can not. You assign a primitive... int x = 10; You create an Object... Integer integer = new Integer( 101 ); // with the Objects you have built-in capabilities for Sets, Sorts, Randomization // Objects have dot methods String s = String.valueOf( integer ); int len = s.length();
MacGyver.getAnswer()
2008-01-03 20:38:31 UTC
Objects and primitives are both data types. Primitive data types are the simplest of data types. They are data types like floating point numbers, integers, and characters.



Objects are data types that are created by programmers. You can even create an object! Objects are more complex than primitive types. They can include both attributes and methods. Attributes can be primitive types that describe an object. Methods are behaviors of a class, what an object can do.



Understand that objects carry a lot of overhead (memory) and primitive types are to the point. However sometimes for purposes of readability, it is of your best interest to use objects in your code.
2008-01-04 00:10:15 UTC
Examples of primitive data types are:

integers 1, 81 or 2000

characters b, g, or #

double 1.5, 81.0, or 2000.0333

boolean true or false



Examples of objects are:

Robot RTD2

Car myCar

Player jim

BankAccount customer2



However objects include methods and attributes which include primitive values. For example the Car data type has methods to accelerate and break. It has attributes like color, cost, year, make and model.



Primitives are not objects they store only one value. Objects not only store more than one value, they store more than one different data type including other objects. For example a Car object can also store an Engine object.
Theseus
2008-01-03 18:53:38 UTC
Depends on the language you are talking about.



In VB6 there was a difference but in VB.net, C#, Java, and JavaScript there is no difference because everything is an object, including integers and strings.



When there is a difference, the primitive is simply a format for data and has no properties or methods.



Sometimes intrinsic datatype keywords are referred to as primitives in .NET, but this is misleading because they are merely aliases (syntactic sugar) for System types, which are objects.
Michael JB
2008-01-03 18:48:26 UTC
A primitive type is predefined by the language and is named by a reserved keyword. In Java, there are eight primitive types: byte, short, int, long, float. double, boolean, char.



An object is a variable you name and define. Specifically, you define the object's state and behavior using properties. Objects attempt to model real world items and how they operate or interact with other objects.
Anonymous
2008-01-03 18:38:27 UTC
Primitive data types are those that your programming language already came with such as double and int.



An object is a data type that a class programmer created, such as a String. Objects come with instance variables and methods that can alter those instance variables or return information about the object (such as the .length method for String).


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...