Difference Between Primitive and Non Primitive

Ibnu Arseno
Code Storm
Published in
4 min readMar 23, 2022
Photo by Raphael Schaller on Unsplash

Data structure means managing data that is in memory, Data can be organized in two ways namely Linear and non-linear.

There are two types of data structures available for programming purposes:
1. primitive data structures.
2. non-primitive data structure.

Primitive data structure

Primitive data structures can only hold one value in one particular location, unlike non-primitive data structures which can be in a both linear and non-linear order.

Primitive data structures are predefined data types that are supported in programming languages. The size and type of the variable value are specified, and there are no additional methods.

Primitive data structures contain only the values provided by the programmer. The four main data structures found in every language are int, float, character, and boolean. but in general, there are 8 data types namely boolean, byte, char, short, int, long, float, and double.

boolean data type

The Boolean data type can only take up to two values, namely TRUE or FALSE. For the most part, boolean values are used for conditional tests.

byte data type

It is an 8-bit signed two’s complement integer. It stores whole numbers ranging from -128 to 127. A byte data type helps save memory in large amounts.

char data type

The character data type is used to store single word characters, both uppercase and lowercase, such as ‘Z’ or ‘z’. Alternatively, you can also use ASCII values ​​to display certain characters.

short data type

A short data type is greater than bytes in terms of size and less than an integer. It stores the value that ranges from -32,768 to 32767.
Generally applied to 16-bit computers, which are now increasingly rare.

int data type

Integers are used to represent numeric data. Integers generally store integers that can be positive or negative. Long can be used in cases where the range of the integer data type is not large enough.

long data type

This type is used for certain cases whose values ​​are outside the range of the int type because this type has the highest range than Integer. By default, the size of a long data type is 64 bit and its value ranges from -263 to 263–1.

float data type

This type is used to denote values ​​that contain single-precision or single-precision values ​​that use 32-bit storage space. Single precision is usually faster for certain processors and takes up half as much storage space as double precision. Note that you should end the value with an “f”.

double data type

This type contains a level of double-precision or double precision and uses 64-bit storage space to store values. The double type is certainly faster to perform mathematical calculations than the float type. For calculations that are real numbers and produce more accurate results, it is better to use the double type.
Note that you should end the value with a “d”

Non-primitive data structure

Non-primitive data types are types defined by the programmer. They are further classified into linear and nonlinear data types. They are also called ‘variable references’ or ‘reference objects’ because they refer to memory locations that store data.

Non-Primitive data types refer to objects and hence they are called reference types. Examples of non-primitive types include Strings, Arrays, Classes, Interfaces, etc.

Strings

The string data structure can be defined as an array of characters. The main difference between the character array and the string data structure is that string data structures terminate with a special character called NULL denoted as ‘\0’. The common operations performed on the string data structure are concatenation, copying, replacing, counting, and searching.

Arrays

An array data structure can hold a fixed number of primitive data structures (such as integers, characters, etc.). All array elements must have the same primitive data structure type, arrays cannot store separate data types such as integers with characters. Arrays are a common data structure used in many algorithms. The basic operations that can be performed on arrays are insertion, deletion, search, update, and array search.

class

A class can be defined as a blueprint (blueprint) or prototype/framework that defines the variables (data) and general methods (behavior) of an object. In other words, a class is an integrated entity between methods and data that refers to an object.

class is used to declare a variable which is an object. Variables in the form of objects are often referred to as object references.

Interface

An interface is a collection of methods that only contains method declarations and structures, without implementation details. While the details of the method are in the class that implements the interface. Interfaces are used when you want to apply a specific method, which is not obtained from the more restrictive inheritance process. The data types that are allowed on the interface are only constant data types.

Difference between primitive and non-primitive data types

The difference between primitive and non-primitive data types are as follows:

  • Non Primitive types can be used to call methods to perform certain operations, while primitive types cannot.
  • A primitive type always has a value, whereas non-primitive types can be null.
  • A primitive type starts with a lowercase letter, while non-primitive types start with an uppercase letter.
  • The size of a primitive type depends on the data type, while non-primitive types all have the same size.

--

--