Skip to main content

Data types are the most important concept in programming.

What are data types?

Just from the name, it basically is the type of the data which your program can process. The English language is made up of letters, words, numbers and characters (. , ? / : ; “”). Programming is too, but  computer systems define them a little differently.

The common ones are :

Integer int 1000, 5, 23, 678
Floating point float 6.9, 8.94, 1.20
String string hello, Have a nice day
Character char a, !, B, h
Boolean bool true, false

 

Integers

Integers (int) store whole numbers. Basically numbers that do not have decimal points. Positive and negative numbers. -20, 20, 0, 898…

int age = 45

Floating point

(float) is used for numbers with decimal points.

float quantity = 12.23456

floats occupy 4 bytes of memory, so many times they aren’t the best option to use when you want a decimal number that is very accurate, for instance when dealing with monetary values. In this case you’re advised to use…

Double

Doubles are also for decimal numbers but with more accuracy. They occupy 8 bytes of memory so the digits after the decimal point can go up to 16. double data type is your best bet when you’re building am application that deals with money.

double tax = 23.89785763856

Character

char. From the name, char holds a single character. Could be a single letter, a symbol, punctuation mark or a blank space. Characters are surrounded by single quotation marks ‘ ‘.

char comma = ','

String

A string is a sequence of characters. A word or a sentence. In some cases it can be used to store a phone number.

string phoneNumber = "+2547123456789"

Note that the beginning of the phone number has a symbol. Combining different characters makes a string. As an integer, the phone number would look like:

int phoneNumber = 2547123456789

string firstName = "Lulu"

Another thing to remember, strings are surrounded by double quotation marks ” ” .

Boolean

There are only two boolean values. true and false. A boolean does not store the string “true” or “false”, but the keywords true or false.

bool winner = true;    bool winner = false;

bool winner = 1;          bool winner = 0;

Some languages allow the use of 1 for true and 0 for false.

 

Date

Used to store just that, a date in the format YYYY-MM-DD.

Time

Used to store just the time in the format hh:mm:ss. Not only does it store the time of day, but can be used in cases where the program needs to store time elapsed.

DateTime

A combination of date and time YYYY-MM-DD hh:mm:ss

Void

Void means no value. Void data types are used mainly on functions that do not return a value.

 

When to use data types

Data types instruct a computer system how to interpret data and its values. Data types are used when defining variables, and at the beginning of functions / methods to determine the return type of the method.

Defining variables

Variables store information that can be manipulated by the program. This information needs to a type

int age = 40

string initials = Mrs. 

char exclamation = !

date = 2021-10-19

The items above are defined variables. Datatype, name of variable , assign sign and the value respectively from left to right.

After you define a variable once you are able to use manipulate it based on the type it is.

For example:

int futureAge = age + 2;  –> future age is now 42.

Since we had defined age as an integer, it can be manipulated using mathematical operators.

Defining functions

Functions are a set of instructions that perform a task. This task is usually performed on variables, and the result is given back in a return. The type of return is determined by the data type at the beginning of the function. Like so:

int incrementAge(int age)

{

      int newAge = age + 2;

       return newAge;

}

As you’ve guessed, this is a simple code snippet that increments age by 2, and returns the new age. Note that at the beginning of the function, there is the type int . Whatever this function returns, it will have to be a value of type int.

 

Defining parameters

Parameters or arguments are special variables that are used to define inputs given in a function.

int incrementAge(int age)

This is the beginning of the function above. For the function to increment an age, it needs to be given the age to increment. Hence the parameter in the brackets (int age)

Functions can take more than one parameter.

 

Datatypes are the one of the building blocks of programming, meaning programs won’t run properly without them.

 

Read more about datatypes :

 

Stay tuned for the next lesson 😀