Primitive Data Type
Primitive is the most fundamental data type usable in the Programming language. There are eight primitive data types: Boolean, byte, character, short, int, long, float, and double. In a Programming language, these data types serve as the foundation for data manipulation.
All basic data types are built-in into the majority of programming languages. Furthermore, many languages provide a set of composite data types. Primitive data types may or may not have a one-to-one correspondence with objects in the computer’s memory, depending on the language and its implementation. However, operations on basic primitive data types are typically thought to be the fastest language constructs.
For example, integer addition can be performed as a single machine instruction, and some processors provide specific instructions for processing character sequences with a single instruction. The C standard specifically states that “a ‘plain’ int object has the natural size suggested by the execution environment’s architecture”. On a 32-bit architecture, this means that int will most likely be 32 bits long. Value types are always basic primitive types.
Most programming languages do not allow programmes to change the behaviour or capabilities of primitive (built-in or basic) data types. Smalltalk is an exception, allowing all data types to be extended within a programme, expanding the operations that can be performed on them or even redefining the built-in operations.
Such data types serve a single purpose: they contain pure, simple values of a type. Because these data types are defined by default in the Programming languages type system, they come with a set of predefined operations. Such primitive types cannot have new operations defined. There are three more types of primitives in the Java type system:
- Short, int, long, float, and double are the numerical primitives. These primitive data types can only store numbers. Simple arithmetic (addition, subtraction, etc.) or comparison operations are associated with such data types (greater than, equal to, etc.)
- Textual primitives include bytes and characters. Characters are stored in these primitive data types (that can be Unicode alphabets or even numbers). Textual manipulation operations are associated with data types (comparing two words, joining characters to make words, etc.). However, byte and char can perform arithmetic operations as well.
- Primitives with boolean and null values: boolean and null.
The actual number of primitive data types available is determined by the programming language that is used. Strings, for instance, are a composite but built-in data type in C#, even as they are integrated to a primitive data type that is both basic and built-in in advanced accents of BASIC and JavaScript.
Considering the Java Programming language, the primitive data structures include integers, floats, characters, and pointers. In general, there are 8 data types. They are as follows:
- Boolean data type
- byte data type
- char data type
- short data type
- int data type
- long data type
- float data type
- double data type
Boolean Data Type
A Boolean data type comprises a single bit of information that can only store true or false values. True or false conditions are tracked using this data type, and Boolean data types are also used to store the result of various conditions. Let’s write a small program and see how it works.
Byte Data Type
The byte data type is an illustration of a primitive data type. It is a signed two’s complement integer of 8 bits, and it stores whole numbers ranging from -128 to 127. A byte data type is useful for saving large amounts of memory. Let’s write a small program and see how it works.
Char Data Type
A single character is stored in this data type, and the character must be enclosed in single quotes, such as ‘E’ or ‘e’. You can also use ASCII values to display specific characters. Let’s look at a simple example to see how it works.
Short Data Type
A short data type is larger than a byte but smaller than an integer, and it saves values ranging from -32,768 to 32767. This data type’s default size is 2 bytes. Let’s look at an example to understand the short data type better.
Int Data Type
This data type is capable of storing whole numbers ranging from -2147483648 to 2147483647. When creating variables with numeric values, int is generally the preferred data type.
Long Data Type
This data type is a two’s complement 64-bit integer. A long data type’s default size is 64 bits, and its value ranges from -263 to 263-1.
Float Data Type
It would be best to use a floating-point type when you need a number with a decimal, such as 8.88 or 3.14515.
This data type supports fractional numbers ranging from 3.4e038 to 3.4e+038. It is important to note that the value should end with an “f.” Let’s look at a specific example to understand this data type better.
Double Data Type
The double data type can store fractional numbers from 1.7e-308 to 1.7e+308. Note that you should end the value with a “d”: