Data Types

Primitive vs. Non-Primitive Data Types

Learn how primitive and non-primitive data types differ, with examples of what each holds and how Java, Python and JavaScript handle them in practice.

On this page
  1. What is a primitive data type?
  2. What is a non-primitive data type?
  3. How primitive and non-primitive types differ
  4. Primitive vs non-primitive in Java, Python and JavaScript
  5. Why the difference matters
  6. Common mistakes
  7. Is a string a primitive data type?

What is a primitive data type?

A primitive data type is a basic type that a programming language provides built in. It holds one simple value, such as a whole number, a decimal number, a single character or a true/false value. Other types are built from these basic pieces.

The exact set of primitive types depends on the language. Java defines eight, and JavaScript defines seven. Python does not draw the same line at all, which is covered below. For the full Java list, see what are the 8 primitive data types. For a grouping that is not tied to one language, see what are the 7 types of data.

What is a non-primitive data type?

A non-primitive data type is built from primitive types, from other non-primitive types, or both. It can hold many values or a structure of related values. Common examples include arrays, lists, dictionaries, classes and objects. Some languages also treat strings as non-primitive.

Terminology varies. Some sources call these reference types, and others call them composite or derived types.

Arrays are the usual example. They group many values under one name, and the answer to “is an array a composite data type?” is yes in most languages.

How primitive and non-primitive types differ

Primitive types Non-primitive types
What they hold One simple value Many values, or a structure of values
Where they come from Built into the language Built from other types, by the programmer or a standard library
Size Fixed for each type Varies. Some types grow as you add items, while a Java array has a fixed length
Assignment Copies the value Shares a reference, so two names can point to the same data

The exact behavior depends on the language, so check its rules before you rely on any of these differences.

Primitive vs non-primitive in Java, Python and JavaScript

Java

Java has eight primitive types: byte, short, int, long, float, double, char and boolean. Every other type is a reference type. String is a class, so it is non-primitive even though programs use it constantly. Arrays are objects too, even when they hold primitive values.

int count = 5;             // primitive: holds the value itself
int[] scores = {90, 85};   // non-primitive: an array object
String name = "Ada";       // non-primitive: a String object

Where these values are stored in memory is a separate topic, covered in what is the difference between heap and stack memory in java.

Python

Python does not split its values into primitive and non-primitive groups the way Java does. Everything is an object, including integers, floats, booleans and functions. A more useful split in Python is mutable versus immutable. An int or a string cannot be changed in place, while a list or a dictionary can.

x = 5           # int, an immutable object
name = "Ada"    # str, also immutable
items = [1, 2]  # list, a mutable object
items.append(3) # changes the list in place

JavaScript

JavaScript has seven primitive types: string, number, bigint, boolean, undefined, symbol and null. Everything else is an object, including arrays and functions. For the full list with examples, see what are the 7 primitive data types in JavaScript.

let count = 5;         // number, a primitive
let scores = [90, 85]; // array, an object

Primitives are compared by value, so "a" === "a" is true. Objects are compared by reference, so {} === {} is false, even though both look empty.

Why the difference matters

Knowing which kind of type you are using changes how code behaves in three main ways.

  • Copying. Assigning a primitive usually gives you an independent value. Assigning a non-primitive often gives you a second name for the same data, so a change made through one name can be seen through the other.
  • Comparison. Comparing two primitives checks their values. Comparing two objects often checks whether they are the same object, not whether their contents match. The rules depend on the language and the operator.
  • Function arguments. Passing a non-primitive to a function often lets the function change the original data. Passing a primitive usually leaves the caller’s value alone.

Common mistakes

  • Assuming Python works like Java. Python does not have a separate set of primitive types, so the Java rules do not carry over.
  • Treating String as primitive in Java. It is a class. Strings in JavaScript are primitives, which is a difference between the languages, not a single rule.
  • Trusting typeof null in JavaScript. It returns “object”, a long-standing quirk of the language. Check for null with === null.
  • Using one word for every language. “Primitive” means different things in different languages, so check the documentation for the language you are using.

Is a string a primitive data type?

It depends on the language. In JavaScript, strings are primitive. In Java, String is a class, so it is non-primitive. Python does not use a primitive category, so the question does not map cleanly onto Python, though its strings are immutable objects.

Keep exploring

Guides from other topics

All topics →

Recursive Algorithm Examples

Recursive algorithm examples in Python: factorial, binary search, tree traversal, Tower of Hanoi, and merge sort, with base cases explained for each.

Recursion 7 min read