thinkingincode.ninja [Dark]

Primitive data types

While a car or a dog have definite attributes, there are some things that don't really have attributes, at least not in the same way. For example, it's possible for something to be either true or false; the statement "a square has 3 sides" is false, while the statement "a triangle has 3 sides" is true. The values "true" and "false" are just that, values. There are no attributes to "true" or properties of "false". Thus, these are examples of a primitive data type. "True" and "False" are boolean values, and they are the only two boolean values. Boolean values are used all the time in programs, because conditional statements, described in a later post, are based off of whether an expression is true or not.

One way to look at it is that a primitive data type is only a value. Values can be compared directly, without any extra steps. For example, it's easy to see that the expressions 2 + 2 and 8 / 2 are equivalent, because they both evaluate to 4. However, how can you determine if two cars are equivalent? Non-primitive data types cannot be compared directly, instead you must have some method to determine equivalence.

Another kind of primitive data type, as hinted at above, is that of a number. While numbers do have "properties", such as even/odd or prime/composite, these properties can be calculated, and are not part of the number itself. Computers are very, very good at dealing with numbers and performing calculations quickly (and accurately, of course), and thus numbers are a primitive data type in practically every (useful) language. It is worth noting here that generally programming languages don't use the type name "number", but instead have more specific types. This is because, amongst other things, there is a significant difference in how computers handle whole numbers and numbers with decimal points. There is also a difference between "fixed point" and "floating point" decimal numbers; the difference is in how precise the numbers are, with fixed point decimal numbers having a specified number of digits after the decimal point, and floating point decimal numbers having a variable number of digits following the decimal point (hence the name "floating" point). Thus, you are likely to encounter the type names int (short for integer), short (or short int), or long (or long int) for whole numbers, and float or double for floating point decimal numbers. The different types are for storing numbers of different sizes. Fixed point decimal numbers aren't present in most languages, and if they are, the type name is generally language-dependent, so none are listed here.

The last major primitive data type is that of a character, frequently abbreviated as char. A character is just a single letter or number or punctuation character, or any printable character (yes I used the word character in the definition of character), such as a, x, or ?. A string, which is not a primitive data type, is composed of zero or more characters. For example, the string Hello! is composed of 6 characters, H, e, l, l, o, and !. In most programming languages that distinguish between characters and strings, you will see characters surrounded by single-quotes, while strings are surrounded by double-quotes: 'H' vs "Hello!". This is to make it clear to the computer what you are trying to do. Some programming languages don't make the distinction between a character and a single-letter string, and will let you use either double-quotes or single-quotes around a string. Regardless, most languages require you to wrap characters and strings with either single or double quotes.

Suggested next reading: Operators