Choosing and Using Data Types

Every data in program have type. Choosing data type is actually not that hard, you just have to know what you expect from a variable you created before. For example :

  • variable 'age', you will expect only positive small range number
  • variable 'address', you will expect combination of words and number
  • variable 'married', you will expect only yes or no (true/false)

After knowing that, you just have to choose from what your programming language provided.

Applying Data Types

When writing source code, some programming language required vary level of detail for finding a data type. Some allowed and some required you to very specific. But even with this difference, most of language treat data type in similar fashion. So we will cover the concept that most language use most of the time.

Numeric Variables

When dealing with number values, it's common to make a distinction between whether you have an integer (a whole number with nothing after the decimal point) or whether you have value that should allow a fractional part to it.

// integers
int age = 21;
int pages = 542;
int speedLimit = 60;

//floating-point numbers
float temperature = 72.4;
float snailSpeed = 0.029;

As you can see, depends of what type of number we use, the data type is different too. Integer use 'int' and fractional number use 'float'. It will have different result in calculation. For example :

int score  = 5/2 

//score now will have value of 2 instead of 2.5 because int doesn't allow fractional part

Boolean Values

Most language have boolean data type. This is value of either true or false. That's it. No other option.

// Java
boolean isUserLogin = true;

// Swift
var isRecording: Bool = false;

// JavaScript
var hasEaten = true;

Text/Character Data

We also expect a data type for text or character (word, paragraph, sentences, etc). Most language have data type for just a single character called 'char'. But often we need more than just one letter, that's why we using data type called 'string'. A 'string' is collection of character all strung together in a sequence.

// Java
string message = "Thank you!!";

// Python
message = "Thank you!!"

// Swift
var message: String = "Thank you!!"

You maybe notice the value is between double quote. You have to do that so word you want is treated as string and not variables.

results matching ""

    No results matching ""