Data types
Introduction
In computer programming, data types are used to define the type of data that a variable can hold. This helps the compiler or interpreter to allocate memory and perform operations on the data accurately. Commonly used data types in programming include integers, booleans, floats, doubles, and strings. Table 1 presents a summary of the most commonly used programming data types.
| Data Type | Description | Example | |
|---|---|---|---|
|
A binary data type that can have one of two values: true or false. |
true, false |
|
|
A whole number data type, including negative numbers. |
42, -10, 0 |
|
|
A decimal number data type, also known as a floating-point number. |
3.14, -2.5, 0.0 |
|
|
A data type that can store larger decimal values with higher precision than a float. |
3.14159265359, -0.00001 |
|
|
A data type that represents a single character. |
'a', 'B', '%' |
|
|
A data type that represents a sequence of characters. |
"hello", "world", "123" |
|
|
A data type that stores a fixed-size sequential collection of elements of the same type. |
"[1, 2, 3], ["apple", "banana", "cherry"]" |
|
|
A data type that represents a complex data structure with properties and methods. |
company = { name: "Powerspex", age: 25 } |
| The datatypes primarily used in PsxCad are boolean, integer, double and object. |
Boolean
A boolean is a data type that represents a logical value, which can be either True or False.
boolean value = True;
Integer
An integer is a whole number data type that represents a numerical value without a fractional component. Integers can be positive, negative, or zero, and can be used in mathematical operations such as addition, subtraction, multiplication, and division.
int age = 25;
Float
A float (short for floating-point) is a data type that represents a numerical value with a fractional component. Floats are often used to represent real numbers, such as decimal numbers or scientific notation.
float pi = 3.14159;
Double
A double is a data type that represents a floating-point number with double precision. Doubles are similar to floats, but they have a higher precision and can represent larger or smaller values with greater accuracy.
double weight = 75.5;
Character
A character is a data type that represents a single character, such as a letter, digit, or symbol.
char letter = 'A';
String
A string is a sequence of characters that represents a text or a message. The characters in a string can be letters, digits, symbols, or spaces,
String message = "Hello, world!";
Array
An array is a data structure that stores a collection of values of the same type in a contiguous block of memory. The values are typically accessed using an index, which is an integer value that indicates the position of the value in the array.
Arrays can be used to store and manipulate large sets of data efficiently, such as lists of numbers, strings, or other objects.
int[] numbers = {1, 2, 3, 4, 5};
The elements are accessed using their index value, such as "numbers[0]" to access the first element, which has a value of 1 in the example above.