beanz Magazine

Data Types

r2hox on Flickr

Programming languages use data types to allocate memory and enforce data integrity. They also reveal the nature of a language.

Would you multiply the words Fred by Flintstone? It makes more sense to multiply the numbers 5 and 33, doesn’t it?

Programming languages avoid this kind of error with data types. A person’s first name and last name are identified as strings while the numbers 5 and 33 are identified as integers (a fancy word for numbers). Data types help languages process data efficiently with as few preventable errors as possible.

This Code Snippets section of the magazine explains parts of programming languages common across most or all languages. It's a great way to understand how languages work. Learning how to code also should be much easier and less frightening if I explain a part of a programming language, then show you how it works in a couple different languages.

Let's get started by talking about data types.

Common Data Types

Here are the most common data types you encounter with all programming languages. As we’ll see, a language may use a different name but the data type is the same as other languages.

Data Type Description
boolean A boolean is true or false, yes or no.
floating Fractional numbers often expressed as decimals.
integer Whole numbers which may or may not include negative numbers.
pointer A value that refers to (points to) another value stored in a specific location, usually in computer memory.
string Alphanumeric characters as well as numbers not treated or processed as integers.

 

Of course, this is an extremely simplified description of data types. There are complex data types, for example, arrays which are collections of nested data often organized by keys where each key has a specific value. This article only introduces you to key concepts, not the gory details you learn as you code in a new language.

Each data type also has a size, measured in bytes, which the programming language uses to allocate memory as the application runs. If you don’t know, or remember, a byte is the smallest addressable space in computer memory. A byte is eight bits in length where each bit is a binary digit of either 0 or 1. Binary digit equals bit, if you get it? So 2 bytes is 16 bits, or binary digits, in length and 4 bytes is 32 binary digits in length. But most people speak in terms of bytes, not bits.

When you learn any programming language, pay attention to how the language handles these data types. Often the available data types defines the language. For example, Erlang doesn’t have booleans but it does have atoms which are true or false.

C

The C language has a rich set of data types, not only the basics of booleans, integers, and strings but also points, floating, void, and so on. C also has short integers and long integers which differ based on their size. C also has arrays, pointers, and other constructed data types.

Erlang

In the Erlang language, any data within a data type is called a term. So the word house and 551 are both called terms. The Number data types are integers and floats. What other languages call a boolean Erlang calls an atom with a value that is true or false. While strings are used in Erlang, it is not a data type.

The language also has a data type called fun which is a function treated as an object. The same way you pass a number from one function to another, in Erlang you use a fun to pass a function as an object from one function to another.

PHP

The PHP language supports eight data types: boolean, integer, float, string, array, object, resource, and null. As with some other languages, you can check the data type of a value to ensure code only runs if a value is the correct type. So you can avoid the problem of accidentally multiplying Fred times Flintstone.

Ruby

The way Ruby handles data types is a bit different from other languages. The method is duck typing which means any function assumes the values it works on are the correct values. So a class called Duck with a walk function and a quack function would not confirm the correct values are passed to it before it runs the quack or walk function. If the values are the wrong type, an error occurs. The burden is on the programmer to ensure only correct values are passed through and errors alert the programmer to fix any problems.

In other words, Ruby uses strings, integers, booleans, arrays, and other data types found in other languages. However, handling the data types is more loose and data types are enforced through run-time errors.

Learn More

Data Types

https://en.wikipedia.org/wiki/Datatype

C

https://en.wikipedia.org/wiki/C_%28programming_language%29#Data_types

Erlang

http://www.erlang.org/doc/reference_manual/data_types.html
https://en.wikipedia.org/wiki/Erlang_%28programming_language%29

PHP

http://www.php.net/manual/en/language.types.intro.php
https://en.wikipedia.org/wiki/PHP_%28programming_language%29#Data_types

Ruby

https://en.wikipedia.org/wiki/Ruby_%28programming_language
https://en.wikipedia.org/wiki/Duck_typing

Common Lisp

https://en.wikipedia.org/wiki/Common_Lisp#Data_types

Also In The May 2014 Issue

Computer Science Unplugged

The Computer Science Unplugged movement introduces non-technical people to computer hardware and software concepts that drive the technologies we use.

What is Computational Thinking?

While computers think in rigid predictable patterns, learning computational thinking helps us understand how and why computers work.

CAPTCHA

We've all used Captchas and found some impossible to solve. Here's why they exist, how they work, and less frustrating alternatives.

Data Types

Programming languages use data types to allocate memory and enforce data integrity. They also reveal the nature of a language.

Lauren Ipsum

If the idea of a computer science book without computers upsets you, please close your eyes until you've finished reading.

The Turing Test

The Turing Test, and its creator Alan Turing, have had a profound effect on computer science and artificial intelligence.

If debugging is the process of removing software bugs, then programming must be the process of putting them in.

May 2014 Learn More Links

Links from the bottom of all the May 2014 articles, collected in one place for you to print, share, or bookmark.

Fortran

One of two key programming languages (Lisp is the other), FORTRAN defined many of the key ideas used in programming languages.

May 2014 News Wire

Interesting stories about computer science, software programming, and technology for the month of April 2014.

How to Create (and Crack) Secret Codes and Ciphers

Secret codes, or ciphers, are a great way to teach computational thinking.

Grace Hopper

One of the first female programmers, Grace Hopper also worked as a mathematician and had an unusual career for women in the 1900s.

BOGONs

Bogons are not an evil race of aliens. But they do shine a light on one part of the internet little known outside of a few security technologists.