Data Types Note
Data Types Note
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:
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.
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.
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.
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.
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
• 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.
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.
C# is a strongly typed programming language as all variable types have to be specified, and all
errors are detected and flagged during compilation.
Unlike C#, JavaScript is a weakly typed programming language because you do not have to specify
the variable type in advance.