0% found this document useful (0 votes)
44 views

Data Types Note

The document discusses different data types used in programming languages. It covers common data types like strings, integers, floats, and enums. It also discusses the difference between strongly and weakly typed languages, and provides examples of each.

Uploaded by

ejioforjo01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Data Types Note

The document discusses different data types used in programming languages. It covers common data types like strings, integers, floats, and enums. It also discusses the difference between strongly and weakly typed languages, and provides examples of each.

Uploaded by

ejioforjo01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DATA TYPES

Overview

Within a program, we process data in various ways such as adding them up or sorting
them. This data comes in different forms. Examples include:

• your name – a string of characters


• your age – usually an integer
• the amount of money in your pocket – usually a value measured in naira and
kobo (something with a fractional part)
A major part of understanding how to design and code programs is centered in
understanding the types of data that we want to manipulate and how to manipulate that
data.

A data type is a classification of data which tells the compiler or interpreter how the
programmer intends to use the data. Most programming languages support various
types of data, including integer, real, character or string, and Boolean.
Almost all programming languages explicitly include the notion of data type, though
the possible data types are often restricted by considerations of simplicity,
computability, or regularity. An explicit data type declaration typically allows the
compiler to choose an efficient machine representation.

Data types are used within type systems, which offer various ways of defining,
implementing, and using them. In a type system, a data type represents a constraint
placed upon the interpretation of data, describing representation, interpretation and
structure of values or objects stored in computer memory. The type system uses data
type information to check correctness of computer programs that access or manipulate
the data.

Definition
Data types can be defined in five different ways:
i. Syntactic
A type is a purely syntactic label associated with a variable when it is declared.

1
ii. Representation
A type is defined in terms of a composition of more primitive types—often machine
types.
iii. Representation and behaviour
A type is defined as its representation and a set of operators manipulating these
representations.
iv. Value space
A type is a set of possible values which a variable can possess. Such definitions make
it possible to speak about (disjoint) unions or Cartesian products of types.
v. Value space and behaviour
A type is a set of values which a variable can possess and a set of functions that one can
apply to these values.

The definition in terms of a representation was often done in imperative languages


such as ALGOL and Pascal, while the definition in terms of a value space and
behaviour was used in higher-level languages such as Simula and CLU. Types
including behavior align more closely with object-oriented models.

Categories of Data Types


Machine data types
All data in computers based on digital electronics is represented as bits (alternatives 0 and 1) on
the lowest level. The smallest addressable unit of data is usually a group of bits called
a byte (usually an octet, which is 8 bits). The unit processed by machine code instructions is called
a word (as of 2011, typically 32 or 64 bits).
Machine data types expose or make available fine-grained control over hardware, but this can also
expose implementation details that make code less portable. Hence machine types are mainly used
in systems programming or low-level programming languages. In higher-level languages most
data types are abstracted in that they do not have a language-defined machine representation.
The C programming language, for instance, supplies types such as Booleans, integers, floating-
point numbers, etc., but the precise bit representations of these types are implementation-defined.
The only C type with a precise machine representation is the char type that represents a byte.
Boolean type
The Boolean type represents the values true and false. Although only two values are possible,
they are more often represented as a word rather as a single bit as it requires more machine
instructions to store and retrieve an individual bit. Many programming languages do not have an
explicit Boolean type, instead using an integer type and interpreting (for instance) 0 as false and
other values as true. Boolean data refers to the logical structure of how the language is interpreted

2
to the machine language. In this case a Boolean 0 refers to the logic False. True is always a non-
zero, especially a one which is known as Boolean 1.

Numeric types
Almost all programming languages supply one or more integer data types. They may either supply
a small number of predefined subtypes restricted to certain ranges (such as short and long and their
corresponding unsigned variants in C/C++); or allow users to freely define subranges such as 1..12
(e.g. Pascal/Ada). If a corresponding native type does not exist on the target platform, the compiler
will break them down into code using types that do exist. For instance, if a 32-bit integer is
requested on a 16-bit platform, the compiler will tacitly treat it as an array of two 16 bit integers.
Floating point data types represent certain fractional values (rational numbers, mathematically).
Although they have predefined limits on both their maximum values and their precision, they are
sometimes misleadingly called reals (evocative of mathematical real numbers). They are typically
stored internally in the form a × 2b (where a and b are integers) but displayed in
familiar decimal form.
Fixed point data types are convenient for representing monetary values. They are often
implemented internally as integers, leading to predefined limits.

Enumerations
The enumerated type has distinct values, which can be compared and assigned, but which do not
necessarily have any particular concrete representation in the computer's memory; compilers and
interpreters can represent them arbitrarily. For example, the four suits in a deck of playing cards
may be four enumerators named CLUB, DIAMOND, HEART, SPADE, belonging to an
enumerated type named suit. If a variable V is declared having suit as its data type, one can assign
any of those four values to it. Some implementations allow programmers to assign integer values
to the enumeration values, or even treat them as type-equivalent to integers.

String and text types


Strings are a sequence of characters used to store words or plain text, most often
textual markup languages representing formatted text. Characters may be a letter of
some alphabet, a digit, a blank space, a punctuation mark, etc. Characters are drawn from a
character set such as ASCII. Character and string types can have different subtypes according to
the character encoding. The original 7-bit wide ASCII was found to be limited, and superseded by
8, 16 and 32-bit sets, which can encode a wide variety of non-Latin alphabets (such
as Hebrew and Chinese) and other symbols. Strings may be of either variable length or fixed
length, and some programming languages have both types. They may also be subtyped by their
maximum size.
Since most character sets include the digits, it is possible to have a numeric string, such as "1234" .
These numeric strings are usually considered distinct from numeric values such as 1234 , although
some languages automatically convert between them.

3
Strongly Typed Programming Language

A strongly typed programming language is one in which each type of data, such as integers,
characters, hexadecimals, and packed decimals, is predefined as part of the programming
language, and all constants or variables defined for a given program must be described with one
of the data types. Certain operations might be allowable only with specific data types.

Strongly typed programming language refers to an idea that describes the enforcement of firm
restrictions on mixing different data types and values. When there is a violation, errors, also known
as exceptions, occur. Strongly typed programming languages use a language compiler to enforce
data typing compliance.

Strongly vs. loosely typed programming languages

In computer programming, a programming language is strongly typed if it demands the


specification of data types. A programming language is loosely typed, or weakly typed, when it
does not require the explicit specification of different types of objects and variables. The "loose"
typing rules in weakly typed programming languages can produce erroneous or unpredictable
results. It can execute implicit type conversions at runtime.

In programming languages, the concepts of strong and weak, or loose, typing are related to but
different from static and dynamic typing. A programming language that is strongly typed can be
either statically or dynamically typed.

In static typing, type checking takes place at compile time and catches such things as missing
functions, invalid type arguments, or a mismatch between a data value and the type of variable to
which it's assigned -- before the program has a chance to run the erroneous code and risk crashing.
In dynamically typed languages, type checking occurs at runtime, so the compiler will ignore
invalid type arguments or mismatched data. If the type checker detects an error, it will alert the
developer to correct the code to avoid the program crashing.

Examples of strongly and loosely typed programming languages

Here are some examples of strongly typed and loosely typed programming languages.

4
Strongly typed programming languages include the following:

• C

• C++

• C#

• Java

• Pascal

• Python

• TypeScript

Loosely typed programming languages include the following:

• C

• JavaScript

• Perl

• PHP

• Ruby

• shell
Why is Java a strongly typed language?

Java is considered strongly typed because it demands the declaration of every variable with a data
type. Users cannot create a variable without the range of values it can hold. Once declared, the
data type of the variable cannot be changed.

Why is Python a strongly typed language?

Python is strongly typed because the interpreter keeps track of all variable types. Python can be
very dynamic, as it rarely uses this information to limit variable usage.

5
Is C a strongly or weakly typed programming language?

C is strongly typed in that the variable type must be specified when declaring the variable.
However, C can also be regarded as weakly typed because users can convert data types through a
cast and without compiler errors.

Why is C++ a strongly typed programming language?

The C++ programming language is considered strongly typed and has


parametric polymorphism available through templates. This means you can create a set of generic
data types and accurately represent them.

Why is C# a strongly typed programming language?

C# is a strongly typed programming language as all variable types have to be specified, and all
errors are detected and flagged during compilation.

Why is JavaScript a weakly typed programming language?

Unlike C#, JavaScript is a weakly typed programming language because you do not have to specify
the variable type in advance.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy