Data types: Difference between revisions
Mr. MacKenty (talk | contribs) |
|||
(11 intermediate revisions by one other user not shown) | |||
Line 5: | Line 5: | ||
Data types are important in programming languages because they help the compiler or interpreter more efficiently allocate memory. Data types tells MMU that how much memory requirement it has before the program compiles. Also, the data type defines which operations can safely be performed to create, transform and use the variable in another computation. | Data types are important in programming languages because they help the compiler or interpreter more efficiently allocate memory. Data types tells MMU that how much memory requirement it has before the program compiles. Also, the data type defines which operations can safely be performed to create, transform and use the variable in another computation. | ||
The main purpose of a type system is to reduce possibilities for bugs in computer programs by defining interfaces between different parts of a computer program, and then checking that the parts have been connected in a consistent way. This checking can happen statically (at compile time), dynamically (at run time), or as a combination of both. Type systems have other purposes as well, such as expressing business rules and enabling certain compiler optimizations <ref>https://en.wikipedia.org/wiki/Type_system</ref> | |||
== | By the way, a [[Abstract data structures|data structure]] (or [[Abstract data structures|abstract data structure]]) simply stores data types in a specific way. | ||
== Primitive types == | |||
Not all languages have the same primitive data types. Please see the table below for a representative list of types: | Not all languages have the same primitive data types. Please see the table below for a representative list of types: | ||
Line 16: | Line 18: | ||
! Data type !! Definition !! Notes | ! Data type !! Definition !! Notes | ||
|- | |- | ||
| [[Int]] || | | [[Int]] || Integer. The mathematical name for any positive or negative whole number. || This is a common primitive data type | ||
|- | |- | ||
| [[ | | [[Float]] || A float is a number that has a fractional or decimal part. || This is a common primitive data type | ||
|- | |- | ||
| [[ | | [[Boolean]] || A boolean is simply true or false, and can be nothing other than true or false || This is a common primitive data type | ||
|- | |- | ||
| [[ | | [[String]] || A string is used to store characters, which could be text or numbers. || This is a common primitive data type | ||
|- | |- | ||
| [[Char]] || | | Pointer / Reference || This type points to (or references) a location in memory|| This is a common primitive data type in compiled languages. | ||
|- | |||
| [[Char]] || Character. This data type stores one single character. || This is a common primitive data type in compiled languages. | |||
|- | |||
| Date / Time || This data types stores data as date and time || This is not a "classic" primitive data type, but shows up often enough. | |||
|} | |} | ||
== Statically typed languages == | |||
A language is statically typed if the type of a variable is known at compile time. For some languages this means that you as the programmer must specify what type each variable is (e.g.: Java, C, C++); other languages offer some form of type inference, the capability of the type system to deduce the type of a variable (e.g.: OCaml, Haskell, Scala, Kotlin) | |||
The main advantage here is that all kinds of checking can be done by the compiler, and therefore a lot of trivial bugs are caught at a very early stage.<ref>https://stackoverflow.com/questions/1517582/what-is-the-difference-between-statically-typed-and-dynamically-typed-languages</ref> | |||
Examples: C, C++, Java, Rust, Go, Scala | |||
== Dynamically typed languages == | |||
A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc. This means that you as a programmer can write a little quicker because you do not have to specify types every time (unless using a statically-typed language with type inference). | |||
Examples: Perl, Ruby, Python, PHP, JavaScript | |||
Most scripting languages have this feature as there is no compiler to do static type-checking anyway, but you may find yourself searching for a bug that is due to the interpreter misinterpreting the type of a variable. Luckily, scripts tend to be small so bugs have not so many places to hide.<ref>https://stackoverflow.com/questions/1517582/what-is-the-difference-between-statically-typed-and-dynamically-typed-languages</ref> | |||
== Do I understand this? == | |||
If you are still stuck, or you have other questions, you may want to [https://discuss.computersciencewiki.org/ '''ask a question on our discussion board''']. | |||
== Some helpful links == | |||
* [https://www.youtube.com/watch?v=A37-3lflh8I This is a basic youtube video which is good for an overview] | |||
== References == | == References == |
Latest revision as of 10:02, 13 August 2022
Data type determines what sort of data is being stored and how it will be used by the program. In computer science and computer programming, a data type or simply type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data [2].
Data types are important in programming languages because they help the compiler or interpreter more efficiently allocate memory. Data types tells MMU that how much memory requirement it has before the program compiles. Also, the data type defines which operations can safely be performed to create, transform and use the variable in another computation.
The main purpose of a type system is to reduce possibilities for bugs in computer programs by defining interfaces between different parts of a computer program, and then checking that the parts have been connected in a consistent way. This checking can happen statically (at compile time), dynamically (at run time), or as a combination of both. Type systems have other purposes as well, such as expressing business rules and enabling certain compiler optimizations [3]
By the way, a data structure (or abstract data structure) simply stores data types in a specific way.
Primitive types[edit]
Not all languages have the same primitive data types. Please see the table below for a representative list of types:
Data type | Definition | Notes |
---|---|---|
Int | Integer. The mathematical name for any positive or negative whole number. | This is a common primitive data type |
Float | A float is a number that has a fractional or decimal part. | This is a common primitive data type |
Boolean | A boolean is simply true or false, and can be nothing other than true or false | This is a common primitive data type |
String | A string is used to store characters, which could be text or numbers. | This is a common primitive data type |
Pointer / Reference | This type points to (or references) a location in memory | This is a common primitive data type in compiled languages. |
Char | Character. This data type stores one single character. | This is a common primitive data type in compiled languages. |
Date / Time | This data types stores data as date and time | This is not a "classic" primitive data type, but shows up often enough. |
Statically typed languages[edit]
A language is statically typed if the type of a variable is known at compile time. For some languages this means that you as the programmer must specify what type each variable is (e.g.: Java, C, C++); other languages offer some form of type inference, the capability of the type system to deduce the type of a variable (e.g.: OCaml, Haskell, Scala, Kotlin)
The main advantage here is that all kinds of checking can be done by the compiler, and therefore a lot of trivial bugs are caught at a very early stage.[4]
Examples: C, C++, Java, Rust, Go, Scala
Dynamically typed languages[edit]
A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc. This means that you as a programmer can write a little quicker because you do not have to specify types every time (unless using a statically-typed language with type inference).
Examples: Perl, Ruby, Python, PHP, JavaScript
Most scripting languages have this feature as there is no compiler to do static type-checking anyway, but you may find yourself searching for a bug that is due to the interpreter misinterpreting the type of a variable. Luckily, scripts tend to be small so bugs have not so many places to hide.[5]
Do I understand this?[edit]
If you are still stuck, or you have other questions, you may want to ask a question on our discussion board.
Some helpful links[edit]
References[edit]
- ↑ http://www.flaticon.com/
- ↑ https://en.wikipedia.org/wiki/Data_type
- ↑ https://en.wikipedia.org/wiki/Type_system
- ↑ https://stackoverflow.com/questions/1517582/what-is-the-difference-between-statically-typed-and-dynamically-typed-languages
- ↑ https://stackoverflow.com/questions/1517582/what-is-the-difference-between-statically-typed-and-dynamically-typed-languages