Devlane Blog
>
Mobile & Web

What are Primitive Data Types?

In the vast programming world, primitive types are the foundation on which software applications and systems are built. These fundamental data types allow you to represent and manipulate information easily. From integers to individual characters and Boolean values, primitive types are the cornerstone of program development. ‍They are those that the language provides us with and with which we can build abstract data types and data structures.

by
Andrés Jimenez
|
May 13, 2024

Primitive types can vary depending on the programming language.

Here are a few that are commonly found:

Integers: Integers represent whole numbers with no fractional or decimal parts. They can be further classified according to their size, such as int (whole),long (long integer),short (short integer), or byte (8-bit integer).

Byte: The byte is the smallest data type among all integer data types. It is an 8-bit signed two's complement integer. Stores integers ranging from -128 to 127.

Syntax: byte variable de byte;

Short: Short is a 16-bit signed two's complement integer. Stores integers with values ​​ranging from -32768 to 32767. Its default value is 0.

Syntax: variable short short;

Int: Int is a 32-bit signed two's complement integer that stores integer values ​​ranging from 2147483648 (-2^31) to 2147483647 (2^31 -1). Its default value is 0.

Syntax: int intVariable;

Long: is a 64-bit signed two's complement integer that stores values ​​ranging from -9223372036854775808(-2^63) to 9223372036854775807(2^63 -1). It is used when we need a range of values ​​greater than those provided by int. Its default value is 0L. This data type ends with 'L' or 'l'.

Syntax: variable long long;

Example

class IntegerDataTypes

{

  public static void main(String args[]) {

    int a = 10;

    short s = 2;

    byte b = 6;

    long l = 125362133223l;

  System.out.println("The integer variable is " + a + '\n');

    System.out.println("The short variable is " + s  + '\n');

    System.out.println("The byte variable is " + b + '\n');

    System.out.println("The long variable is " + l);

   }

}

Floating Point Numbers: Floating point numbers represent numbers with fractional parts. They can include types like float (single precision floating point) and double (double precision floating point).

Float: It is a floating point data type that stores the values, including their decimal precision. It is not used for precise data such as currency or research data.

A float value: It is a 32-bit or 4-byte single-precision IEEE 754 floating point, can have 7-digit decimal precision, ends in 'f' or 'f', default = 0.0f, stores fractional numbers ranging from 3.4e- 038 to 3.4e+038

Syntax: float floatVariable

Double: The double data type is similar to float. The difference is that it is twice the float in the decimal precision case. It is used for decimal values ​​like float and should not be used for precise values.

A double value:

It is a 64-bit or 8-byte double-precision IEEE 754 floating point.

Can have 15-digit decimal precision

Default = 0.0d

Stores fractional numbers ranging from 1.7e-308 to 1.7e+308

Example

class FloatDataTypes

{

  public static void main(String args[]) {

 

    float f = 65.20298f;

    double d = 876.765d;

 

    System.out.println("The float variable is " + f);

    System.out.println("The double variable is " + d);

  }

}

Characters: Characters represent symbols or individual letters. They are denoted by the typechar and, in many programming languages, are based on the Unicode character set and provide provision for multiple languages ​​like English, French, German, etc. It occupies a memory space of 16 bits or 2 bytes. Stored values ​​range from 0 to 65536.

Example

class CharDataType {

    public static void main(String[] args) {

        char var1 = 'A';

        char var2 = 'd';

        System.out.println(var1);

        System.out.println(var2);

    }

}

Booleanos: Booleans represent logical values ​​and can have one of two states: true OR false. They are useful for making decisions and controlling the flow of the program.

Example

class BooleanDataTypes

{

  public static void main(String args[]) {

      boolean var1 = true;

      if (var1 == true) //checks if the value is true or false

      {

          System.out.println("Boolean value is True");

      }

      else

      {

          System.out.println("Boolean value is False");  

      }

  }

}

These primitive types form the basis for building more complex data structures and performing calculations in programming.

How do Primitive types work?

When using a Primitive type, a specific amount of memory is reserved to store the corresponding value. Each primitive type has a default size in bytes, determining the memory required to store its value. 

This memory is directly allocated and can be efficiently accessed to read and modify stored data. Primitive types are simple, offering extensive functionality for manipulating and operating on data. 

Each primitive type has specific associated operations and behaviors that allow you to perform mathematical calculations, logical operations, comparisons, and character manipulations, among other functions. 

This functionality will enable you to create complex algorithms and applications using only primitive types, demonstrating its versatility and utility in various programming contexts.

Programming Languages ​​That Use Primitive Types

Java

  • Integers: int, long, short, byte - They are used to store integers in different ranges and sizes of memory.

  • Floating point numbers: float, double - They represent numbers with a fractional or decimal part with different levels of precision.

  • Characters: char - Stores a single Unicode character, such as a letter or symbol.

  • Booleanos: boolean - Represents a value of true (true) the fake (false) and is used in logical expressions and decision making.

C++

  • Integers: int, long, short, byte - Store integers in different ranges and sizes in memory, similar to Java.

  • Floating point numbers: float, double - Represent numbers with fractional or decimal parts, similar to Java.

  • Characters: char - Stores a single ASCII or Unicode character.

  • Booleanos: bool - Stores a true value (true) the fake (false), similar to Java.

Python

  • Integers: int, long - They store whole numbers (such as 5, -10, 100), with long providing greater storage capacity.

  • Floating point numbers: float - Represents numbers with a fractional or decimal part, like 3.14, -0.5, 10.0.

  • Characters: str (text string) - Stores a sequence of characters. Represents a text string, such as "Hello", "Python", "123".

  • Booleanos: bool - Stores a true value (True) the fake (False), used in logical expressions and decision making.

C#

  • Integers: int, long, short, byte - Similar to Java and C++, they are used to store integers in different ranges and sizes of memory.

  • Floating point numbers: float, double - Similar to Java and C++, they represent numbers with a fractional or decimal part.

  • Characters: char - Stores a single Unicode character.

  • Booleanos: bool - Similar to Java and C++, stores a true value (true) the fake (false).

JavaScript

  • Integers: JavaScript does not have explicit integer types. Floating point numbers are used to represent both integers and decimal numbers.

  • Floating point numbers: number - Represents numbers with a fractional or decimal part.

  • Characters: string - Stores a sequence of characters.

  • Booleanos: boolean - Stores a true value (true) the fake (false), similar to other languages.

It is important to note that the names and specific features may vary slightly from language to language. However, the fundamental idea of ​​primitive types remains consistent in most languages.

Importance of using Primitive types

The importance of primitive types in programming lies in the following aspects:

  • Fundamental data representation: They allow representing and manipulating the most essential data in a program. These include integers, floating point numbers, single characters, and boolean values. By having specific primitive types for each type of data, it is easier to store and manipulate the necessary information in the program.

  • Memory efficiency and performance: Primitive types usually have a fixed memory size, meaning they take up a predefined amount of space. This allows for efficient resource management and prevents wasted memory. In addition, operations with primitive types are executed quickly and efficiently since they do not require additional data manipulation processes. This translates into optimal application performance, especially when you work with large volumes of data or need high execution speed.

  • Compatibility and portability: are widely supported and used in many programming languages. This ensures that programs written in a specific language with primitive types can run on different platforms and operating systems without issue. By not relying on complex features or structures, programs that use primitive types are more portable and versatile, making them easier to deploy in different environments.

  • Simplicity and readability of the code: Primitive types provide a clear and concise way to define and communicate the nature of the data used in a program. By declaring variables with primitive types, you provide explicit information about the kind of data you expect to store in that variable, making your code easier to understand and maintain. Primitive type names are often intuitive and descriptive, which improves readability and makes it easier for development teams to collaborate.

Conclusion

In conclusion, primitive types are fundamental elements in programming. They provide an efficient and simple way to represent and manipulate basic data in a program or application. 

Using primitive types, programmers can optimize resource usage, improve application performance, and make code easier to understand and maintain. 

In short, understanding primitive types and how they work is essential for any programmer. 

Primitive types form the foundation for more complex data structures and advanced algorithms. 

Its knowledge and proper use are essential to develop efficient, robust, and compatible applications in different programming environments.