A primitive in programming is a built-in data type that represents a single, basic value, such as a number, a character or a boolean, rather than a structure composed of other values. Primitives are defined by the language itself, and a variable of a primitive type typically holds the value directly instead of pointing to an object.
Which types count as primitive differs from language to language. Java has eight, C has its basic arithmetic types, JavaScript has seven, and Python deliberately treats everything as an object.
Most languages share these ideas, with exceptions noted below:
Reference types, such as arrays, objects and collections, are different. A variable of a reference type holds a reference to data stored elsewhere, so two variables can point at the same data.
| Language | Primitive types |
|---|---|
| Java | byte, short, int, long, float, double, char, boolean |
| C (C23) | char, short, int, long, long long, float, double, _Bool (bool in C23), plus unsigned variants |
| C++ | The C types plus bool, wchar_t, char8_t, char16_t, char32_t |
| JavaScript | string, number, bigint, boolean, undefined, null, symbol |
| Go | bool, string, int and sized integers, float32, float64, complex64, complex128, byte, rune |
| Rust | i8 to i128, u8 to u128, isize, usize, f32, f64, bool, char |
| Python | None in the strict sense: int, float, bool and str are all objects, although int, float, bool, str and bytes are immutable |
Strings show how much the definition varies. In Java, String is a class and therefore a reference type. In JavaScript, a string is a primitive. In C, there is no string type at all, only arrays of char.
In both languages, changing a copied primitive never affects the original, while changing a shared object affects every holder.
Some languages pair each primitive with an object type so primitives can be used where objects are required, such as in generic collections.
Java provides Integer, Double, Boolean and similar wrapper classes. Converting between them is called boxing and unboxing, and the compiler does it automatically:
Boxing allocates objects, so large collections of wrapper types use more memory than arrays of int. Unboxing a null wrapper throws a NullPointerException. Project Valhalla is working on value classes to narrow this gap, so check your Java version's release notes before relying on it.
int in Java and C, number in JavaScript, int in Go) unless you have a reason to choose another.int32_t in C or int64 in Go, when data crosses a file, network or hardware boundary.It depends on the language. Strings are primitive in JavaScript and Go, an object type in Java and Python, and arrays of characters in C.
A primitive is a simple built-in value without methods or fields, copied by value. An object bundles data and behaviour, is usually created with a class, and is typically handled through a reference.
No. Local primitives are often kept in registers or on the stack, but a primitive field of an object lives wherever that object lives, usually the heap. The language rules describe behaviour, not memory placement.
null a primitive?In JavaScript, yes. In Java, null is a special literal for reference types, not a primitive. In C, there is a null pointer value instead.
A primitive is a basic built-in value type such as an integer, floating-point number, character or boolean. Primitives are copied by value, cheap to use and map closely to hardware. The exact list depends on the language, so check whether strings, booleans and wrapper types behave as primitives in the one you use.