43

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 ?

0

8 Answers 8

33

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.

18
  • whats the difference between basic and built in datatypes ?
    – progammer
    Commented Mar 14, 2012 at 15:32
  • 1
    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" .
    – progammer
    Commented Mar 14, 2012 at 16:24
  • @Appy - see edit.
    – John Bode
    Commented Mar 14, 2012 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. Commented Mar 15, 2012 at 16:33
  • 2
    @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.
    – rlperez
    Commented Mar 16, 2012 at 17:15
20

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.

9
  • 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
    Commented Mar 14, 2012 at 16:08
  • 8
    @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.
    – user7043
    Commented Mar 14, 2012 at 16:15
  • @Jesper Why are primitive types faster to work with? Just simply due to the fact they don't have the extra wrapping non-primitives do? I'm trying to figure out the difference between primitives and non-primitives...as in are primitives built into the CPU or something? Commented Aug 18, 2016 at 11:59
  • From Wikipedia: "Depending on the language and its implementation, primitive data types may or may not have a one-to-one correspondence with objects in the computer's memory. However, one usually expects operations on basic primitive data types to be the fastest language constructs there are." --- So this 1-to-1 correspondence is the root cause of it being faster, whereas with objects, it isn't 1-to-1. Am I correct? Commented Aug 18, 2016 at 12:01
  • 1
    @Abdul don't make it too complicated. The CPU has a number of registers, that can contain different kinds of primitive values (for example a 32-bit or 64-bit integer or a single or double precision floating point number) and the CPU has instructions implemented in hardware to do operations on the values in the registers. Your last remark is essentially correct.
    – Jesper
    Commented Aug 18, 2016 at 13:42
9

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.

3
  • Thanks :) Your answer is very informative , but I am unable to understand clearly . CAn you give some examples (probably with C/C++/Java) .
    – progammer
    Commented Mar 14, 2012 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
    Commented Mar 14, 2012 at 16:03
  • 3
    @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
    Commented Mar 15, 2012 at 14:51
5

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.

1
  • @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 :) .
    – progammer
    Commented Mar 16, 2012 at 11:33
0

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.

15
  • 2
    C#'s primitive int data type does have methods...
    – Heinzi
    Commented Mar 14, 2012 at 15:30
  • Could you give an example?
    – JoshRagem
    Commented Mar 14, 2012 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
    Commented Mar 14, 2012 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
    Commented Mar 14, 2012 at 20:09
  • 1
    @barlop: Java is not descended from C++. If Herbert Schildt can't even get such a simple fact straight, I'm not sure I would trust the rest of the book either. Patrick Naughton, original designer of Java, was a huge Objective-C fan. In fact, he had already given notice to work on Objective-C at NeXT, when Scott McNealy convinced him to stay at Sun and design a new language. James Gosling, the other original designer, was very familiar with Smalltalk (direct ancestor of Objective-C) and Lisp (inspiration for Smalltalk). Later on, several members of the Objective-C design team moved to Sun. Commented Apr 20, 2014 at 2:27
0

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 .

-2

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.

5
  • 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 ?
    – progammer
    Commented Mar 14, 2012 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
    Commented Mar 14, 2012 at 15:15
  • Addressed your comment int the question itself. Plz see edit.
    – TheTechGuy
    Commented Mar 14, 2012 at 15:18
  • 2
    strings in C++ do not need to be created with new
    – jk.
    Commented Mar 14, 2012 at 15:51
  • @jk I was out of touch with C++. You are right. Will fix the ans.
    – TheTechGuy
    Commented Mar 14, 2012 at 15:53
-2

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.

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