What are datatypes ?

What are Datatypes ?

Did you read my first blog ? If you did not. Go read it right now. 

I will like to turn your mind around a bit (Pun intended again.)

When you see a person , What comes to your mind ? 

This person might be my future partner. 

This person is super hot. 

We will name our kids bytes and bits. 

How are you defining the person ? With some values ! 

They will have a name, age, body structure (if you're objectifying them) etc. These are all data that define that person. Names are always words, ages are always numbers.

Let us take another example. 

You want to eat a Burger (Or Pizza or whatever you want just get something to eat).

How do you define it ?

It has a name, a flavor, a  color and some ingredients you like. 

These are the data which define your burger and they have types. 

A name is of a type "Word".

A flavor is of a type "Word".

Computers are stupid just like you. They need to be told what kind of data you will provide. (Except dynamically typed languages like python and javascript. We will talk about them later)

There are 3 types of classification of data in a programming language.

1. Primitive Datatypes

These datatypes are the most fundamental datatypes.

1. Integers.

2. Characters.

3. Floating point Numbers. (LOL the point actually floats. Search it up).

Lets take up an example.

Lets say we define a person like you.

You are depressed and a therapist needs to rate your depression with a number.

So you are rated a 10 .

This 10 is of a datatype Integer.

we write it like (in C,C++ and Java)

int depression_rate = 10;

 To define you again, I will take up your grade.

Say you are rated an 'F'

char grade = 'F'

 2. Non primitive datatypes

Non primitive datatypes are basically a new type of datatype made using a collection of the primitive datatypes.

1. Strings

Strings are a collection of characters. 

When you write a sentence like "I need Help. ", This sentence is a collection of characters and thus, of String datatype. 

2. Arrays

Array is a collection of data of same datatype. Can be any datatype. 

Lets say you want to hold the number of times you have been depressed each day for 7 days.

You will write it as :

    int depressed[7]={10,12,13,15,18,19,21}

Here the items of the collection can be accessed by their corresponding index. 

Index starts from 0.

The 1st element is at index 0.

The 2nd element is at index 1.

The 3rd element is at index 2.

...

To access the element at 3rd position, you write depressed[2].

 Why do we start at 0 ? There are a lot of reasons. I am not telling you. Programming is depressing.

3. User defined datatypes

This is where you get depressed. User defined datatypes are datatypes defined by programmers.

It can be anything you want. Just take some primitive and non primitive datatypes together, put them in a container and you make a new datatype.

For example, Lets say I want to make a datatype for depressed people.

So I write:

class depressed

{

 String name;

int depression_level;

}

Here depressed has become a datatype. I can make variables of this datatype.

depressed me;

me.name = "Punnyartha";

me.depression_level = 65535;


Comments