Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

My understanding of a primitive datatype is that

It is a datatype provided by a language implicitly (Others are user defined classes)

So different languages have different sets of datatypes which are considered primitive for that particular language. Is that right?

And what is the difference between a "basic datatype" and "built-in datatype". Wikipedia says a primitive datatype is either of the two.

PS - Why is "string" type considered as a primitive type in SNOBOL4 and not in Java ?

share|improve this question
7  
@Yannis No ! Its not homework . I am studying "programming languages principles" and this particular field requires clear understanding of such terms which I lack ! I tried few sources but nothing mentions clearly . –  jsp99 Mar 14 '12 at 14:55
6  
Ok then, please edit your question and tell us exactly what you think a primitive data type is. It will help us understand what confuses you and we can help a lot more than just giving you another definition. –  Yannis Rizos Mar 14 '12 at 14:58
    
is question, why the name primitive? –  ManuPK Mar 14 '12 at 14:58
    
@ManuPK Not exactly .. I edited it . I am sorry for not being clear . –  jsp99 Mar 14 '12 at 15:04
21  
It's the data types the cavemen used to implement fire. –  Paul Tomblin Mar 14 '12 at 15:15

9 Answers 9

up vote 23 down vote accepted

It kind of depends on the language.

For example, in languages like C and C++, you have a number of built-in scalar types - int, float, double, char, etc. These are "primitive" in the sense that they cannot be decomposed into simpler components. From these basic types you can define new types - pointer types, array types, struct types, union types, etc.

Then you have a language like old-school Lisp, where everything is either an atom or a list. Again, by the above definition, an atom is "primitive" in the sense that it cannot be decomposed into something simpler.

Edit

As far as I'm concerned, the terms "primitive", "basic", and "built-in" are pretty much interchangeable. If you want to get really pedantic, though, you can distinguish between types that are "built-in" (those explicitly provided by the language definition) and types derived from the built-in types that are still "primitive" or "basic" in that they cannot be decomposed into simpler elements. C's typedef facility allows you to create new type names for existing types. Ada allows you to create new scalar types that have constraints on them. For example, you can derive a Latitude type from the built-in floating type, with the constraint that it can't take on values outside the range [-90.0, 90.0]. It's still a primitive or basic type in that it cannot be broken down into any simpler components, but since it's user-defined, it's not considered a "built-in" type.

Again, these concepts are a little fuzzy, and it really depends on context. For example, the notion of a "built-in" type is meaningless for a typeless language like BLISS.

share|improve this answer
    
whats the difference between basic and built in datatypes ? –  jsp99 Mar 14 '12 at 15:32
    
Is "string" a primitive datatype in Java ?? (I have seen delnan's comment to Jesper's answer) . I want to know your opinion , because in one of your comments you said "whether or not primitive type depends on whom you ask" . –  jsp99 Mar 14 '12 at 16:24
    
@Appy - see edit. –  John Bode Mar 14 '12 at 16:43
3  
@Appy String in Java isn't primitive because it is a compound type. If you look at String class source code (src.zip in JDK folder) you will see, that internally String is represented as an array of characters. So you can decompose String into individual characters. You can't decompose character into smaller values which makes char a primitive type and String a compound type. –  Jacek Prucia Mar 15 '12 at 16:33
1  
@Appy You are missing the point. SNOBOL4 strings can't be decomposed. Java strings can. Thats the difference. Both are built in but only SNOBOL4 strings are primitive. –  Rig Mar 16 '12 at 17:15

From the Java perspective:

In Java, there is a very clear distinction between primitive and non-primitive types.

A variable of a primitive type directly contains the value of that type (in other words, they are value types).

A variable of a non-primitive type doesn't contain the value directly; instead, it is a reference (similar to a pointer) to an object. (It is not possible in Java to create user-defined value types).

Java has eight primitive types: byte, short, int, long, char, boolean, float and double. Anything else is a non-primitive type.

share|improve this answer
    
So String datatype is non-primitive in Java. In every Java main() program we write "public static void main(String args[])". Why? Why we always have to pass a String argument in main? Also we never need to import the String type in Java the way we include it in C/C++(eg. #include<string> or using std::string). Please explain. –  Maxood Mar 14 '12 at 16:08
6  
@Maxood String does have special status in the language (it gets literals, it's in the signature of main, there's interning which I presume is explicitly allowed by the JLS, it's available automagically, etc.) but it isn't a primitive data type. One might call it built-in. –  delnan Mar 14 '12 at 16:15

A primitive is a basic data type that's not built out of other data types. It can only represent one single value. All primitives are built-in data types by necessity, (the compiler has to know about them,) but not all built-in data types are primitives.

In some languages, the compiler has built-in knowledge of certain types that are built out of other things, because it has to be able to treat them in a special way. Examples would be the Variant in Delphi and Visual Basic (and the Boo equivalent, "Duck") and the base object class in most OO languages.

share|improve this answer
    
Thanks :) Your answer is very informative , but I am unable to understand clearly . CAn you give some examples (probably with C/C++/Java) . –  jsp99 Mar 14 '12 at 15:14
    
@Mason Wheeler This is where we use the term loose typing and strong typing. As i have heard people saying that VB is a loosely typed language while Java is a strongly typed one. Please confirm. –  Maxood Mar 14 '12 at 16:03
2  
@Appy C++ ships with classes for strings, vectors, and maps. These are built-in they come with the language, are in the language definition etc. However they are written in C++ they come from the standard library. You could write your own that behaves identically. Compare this to an int or a char. You can't define your own char in C++. You could overload every operator on a class and get something that behaves similarly but not identically. This is a primitive. Basically something defined in a language that is not implementable in that language. –  stonemetal Mar 15 '12 at 14:51
    
@stonemetal ... Thanks :) –  jsp99 Mar 16 '12 at 1:16

Why is "string" type considered as a primitive type in SNOBOL4 and not in Java ?

Because the Java Language Specification very clearly specifies what the word "primitive" means in the context of Java, and Java's String doesn't conform to this specification, whereas the SNOBOL4 Language Specification defines what "primitive" means in the context of SNOBOL4 and SNOBOL4's string conforms to that specification.

There's really nothing more to it. The specification of the respective language defines what certain words mean and something either fits that description or it doesn't. For example, the C Language Specification defines the meaning of the word "function" to be something which is very clearly not what the word "function" usually means, but that's the way the word is used inside the C community.

share|improve this answer
    
@Mittag Based on your answer , I googled some phrases and came across a very nice link which almost clears all the doubts on this particular topic :) . –  jsp99 Mar 16 '12 at 11:33

A 'primitive' data type means that you have a value stored in memory--this value has no methods or internal structure. A primitive can only be operated on by external operations.

In Java, primitives are numbers (int, long, etc.) and char. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

If your value has structure (I'm thinking of struts here) then it is not a primitive. If it has methods (an object or class) it is not a primitive.

share|improve this answer
2  
C#'s primitive int data type does have methods... –  Heinzi Mar 14 '12 at 15:30
    
Could you give an example? –  JoshRagem Mar 14 '12 at 15:40
    
var myString = 3.ToString(); (see Int32.ToString()). Note that int in C# is a synonym for Int32, i.e., Int32 is a primitive type (unlike Java, where Integer is different from int). Thus, 3.ToString() in C# does not involve boxing! –  Heinzi Mar 14 '12 at 16:08
    
That's very interesting; the link you provided indicates that 'int' is really an object of the class 'Int32' (or maybe a combination of the primitive and a wrapper class), so by the definition I put: 'int' is not a primitive. :) –  JoshRagem Mar 14 '12 at 20:09
    
Then there are no primitive classes in C#, since all data types have a ToString() method. ;-) –  Heinzi Mar 14 '12 at 22:12

I came across some information and felt it would aid the other answers . So want to share it -

Wikipedia says -

In computer science, primitive data type is either of the following:

A basic type is a data type provided by a programming language as a basic building block. Most languages allow more complicated composite types to be recursively constructed starting from basic types.
A built-in type is a data type for which the programming language provides built-in support.

In most programming languages, all basic data types are built-in. In addition, many languages also provide a set of composite data types("string" type in Java).

I would like to say it this way -
A basic datatype is a datatype which cannot be further broken down ("string" type can be made from "char", but char cannot be 'broken down' into any datatype (In most of Programming languages) . So it is basic datatype)

String type , in SNOBOL4 is considered primitive .Though I dont know the exact reason why . It's both basic as well as built-in in SNOBOL4 .

Java specification clearly mentions the list of datatypes it considers to be primitive type . Because the string type is not in their specification , it's not a primitive datatype in Java , inspite of being a built-in type .

share|improve this answer

Primitive data types are intrinsic to the programming language. They're built into the language, so you don't need to import/include code from a class library to use them. They represent simple values such as numbers, characters, and boolean values. Without them the programming language becomes virtually unusable. They serve as basic building blocks for more complex data types.

share|improve this answer

For better understanding the concept of primitive types and user-defined datatypes all that the other answers lacks is the concept of compiler.

If you understand the concept of lexemes getting selected as tokens, you will come to know that primitives are the only datatypes that a compiler knows by itself.

share|improve this answer

Primitives are fundamental data types provided by the language itself. Example in C language are int, float, char data types. It is provided as fundamental part of the language.

Non primitives are user defined variables or objects. For example you can define an object which store data or you can use struct in C to define you own variables. These are not primitive because you had to define them first in order to used them.

Examples: Employee, Student, a_very_large_number_that_you_defined_yourself

Edit with more explanation and addressing the comment I think of primitive as variables that came originally with the language and not variables like String in C++ which you have to declare using the New keyword. That makes it an object so a String would not be a primitive.

Answer you comment: It probably has to do with implementation of the variable. If it was part of language core, it is primitive. If not it is not. Example is String in C++. This is not a core variable and is declared using New keyword. So it is not primitive.

share|improve this answer
    
Thanks for the answer . Does classification of a datatype as "primitive" depend on the implementation ? Like in C/C++ we store strings as char arrays .But we need to explicitly define them . So if some other language has the same implementation to store a string but provides it implicitly , will the datatype "string" be a primitive in that particular language ? –  jsp99 Mar 14 '12 at 15:08
    
@Appy - possibly. C doesn't provide a string data type as such; strings are represented as aggregates of the primitive type char. C++ introduced a proper string data type, but whether or not it's a "primitive" type itself depends on whom you ask. Personally, I would not classify it as such. However, I would classify an old-school BASIC string type as a primitive type, for reasons that are probably completely arbitrary. –  John Bode Mar 14 '12 at 15:15
    
Addressed your comment int the question itself. Plz see edit. –  Noname Mar 14 '12 at 15:18
2  
strings in C++ do not need to be created with new –  jk. Mar 14 '12 at 15:51
    
@jk I was out of touch with C++. You are right. Will fix the ans. –  Noname Mar 14 '12 at 15:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.