{"id":74,"date":"2023-05-04T12:46:22","date_gmt":"2023-05-04T10:46:22","guid":{"rendered":"http:\/\/oopm.org\/?page_id=74"},"modified":"2024-01-04T15:17:29","modified_gmt":"2024-01-04T14:17:29","slug":"5-data-items","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=74","title":{"rendered":"4A Data-Items and Data Types"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages74&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages74&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"has-pale-cyan-blue-background-color has-background has-small-font-size wp-block-paragraph\">I have preliminary put this page around Chapter 4, since I think it is needed here, but it is up for discussion. The same for the Chapter on 4B Statements &#8211; see also the note there! <\/p>\n\n\n\n<pre class=\"wp-block-code has-pale-pink-background-color has-background\"><code>I guess that the structure of this chapter should be like this:\n\nData items and their types   -- ikke 'and Value Types', da referencer er her\n   Reference data items\n   Value data items\n      Primitive values types\n      Composite value types\n\nOLM: Men skal man starte med references?\nOg hvor t\u00e6nker du der er et afsnit om <strong><em>Values and Objects<\/em><\/strong>?<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this Chapter, we summarize <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\">+++bmp: hvis vi f\u00f8rst indf\u00f8rer value types her, da er det ikke l\u00e6ngere bare et summary<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-secondary-color\">olm: teksten er fra den gang vi t\u00e6nkte summary, s\u00e5 den skal helt klart fornyes.<\/mark> data-items that may represent primitive values, string values and references to objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned in previous chapters, objects may have attributes in the form of data-items that may represent properties characterizing the phenomena. For the <code>Account<\/code> example, we have seen examples of data-items such as&nbsp;<code>owner<\/code>,&nbsp;<code>balance<\/code>,&nbsp;and <code>interestRate<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is a fundamental difference between the data-items <code>owner<\/code> and&nbsp;<code>balance<\/code>&nbsp;of <code>Account<\/code> objects. While <code>owner<\/code> is a <em>reference<\/em> to the object representing the owner of the account,  <code>balance<\/code> represents a property that is given by a <em>value<\/em>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, some value types are denoted <em>primitive values<\/em>. <code>balance<\/code> and <code>interestRate<\/code> are examples of data-items holding primitive values, in this case of type <code>Real<\/code>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\">+++ bmp: Sidste version af <code>Account<\/code> har owner as a <strong>ref<\/strong> <code>Customer<\/code>. <\/mark>The data-item <code>owner<\/code> is holding values of type <code>String<\/code>, but is usually not considered a primitive value type. The reason is that the primitive values may be represented in a fixed number of bytes in the computer whereas a <code>String<\/code> value may need a variable number of bytes for its representation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned, data-items like Account_1010, aClerk, and aCustomer are examples of data-items holding references to objects. These data-items were declared as:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>Account_1010: <strong>obj<\/strong>\n   ...\naClerk: <strong>obj<\/strong>\n   ...\naCustomer: <strong>ref<\/strong> Customer<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The data-items <code>Account_1010<\/code>, and <code>aClerk<\/code>, are constant in the sense that they refer to the same object during the lifetime of the program execution. The data-time <code>aCustomer<\/code> is variable since it may refer to different <code>Customer<\/code> objects during the program execution.<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>JohnSmith: <strong>obj<\/strong> Customer(\"John Smith\")\naCustomer: <strong>ref<\/strong> Customer\naCustomer := Customer(\"Mary Poe\")\n...\naCustomer := JohnSmith<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the above example, <code>JohnSmith<\/code> is constantly referring to the object <code>Customer(\"John Smith\")<\/code> whereas <code>aCustomer<\/code> may refer different <code>Customer<\/code> objects. It is first assigned a new object <code>Customer(\"Mary Poe\")<\/code>. Then later it may be assigned <code>JohnSmith<\/code>, which nimplies that <code>Mary<\/code> refers to the same object as <code>JohnSmith<\/code>.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background has-small-font-size wp-block-paragraph\">This page should be split into subpages.   <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Value data items<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">+++Value data items: name and value type<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Primitive value types<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Data-items such as&nbsp;<code>owner<\/code>,&nbsp;<code>balance<\/code>&nbsp;and&nbsp;<code>interestRate<\/code>&nbsp;are examples of data-items holding values of <em>primitive value types<\/em>. These data-items are declared as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code has-border-color has-tertiary-border-color has-cyan-bluish-gray-background-color has-background has-small-font-size\" style=\"border-width:1px\"><code>owner: <strong>var<\/strong> String\nbalance: <strong>var<\/strong> Real\ninterestRate: <strong>var<\/strong> Real<\/code><\/pre>\n\n\n\n<p class=\"code { font-size: 8px; } wp-block-paragraph\">The specification <code><strong>var<\/strong> String<\/code> in the declaration of <code>owner<\/code>, specifies that <code>owner<\/code> may hold string values, and <code>String<\/code> is called the <em>type<\/em> of <code>owner<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The specification <code><strong>var<\/strong> Real<\/code> in the declarations of <code>balance<\/code> and <code>interestRate<\/code> specifies that these may hold real values, and their type is thus <code>Real<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Real<\/code> and <code>String<\/code> are examples <em>value types<\/em>, which are similar to classes. We discuss value types in Chapter <em>Objects and Values<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the following sections we describe other common primitive values.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Real values<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The data-items <code>balance<\/code> and <code>interestRate<\/code> are as mentioned examples of data-items that represent real values. Examples of real values are: 100, 3.14, 0.56, -12.11, <code>13.5E7<\/code>, and <code>1.11E-7<\/code>. The <code>'E'<\/code> in 1<code>3.5E7 <\/code>means that the value is 13.7 multiplied by 10<sup>7<\/sup>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The keyword <code><strong>var<\/strong><\/code> used in the declaration specifies that these data-items are variable, which means that they may be assigned different values during the execution of the program:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>balance := 500\ninterestRate := 3.07\n...\nbalance := balance + amount\ninterestRate := 2.9\n...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It is also possible to specify that a data-item holds a value which is constant during the whole of the program execution:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code><code>&nbsp;&nbsp;&nbsp;X:&nbsp;<strong>val<\/strong>&nbsp;3.1\n<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The  keyword <strong><code>val<\/code><\/strong> specifies that the data-item <code>X<\/code> is constant and it holds the value <code>3.14<\/code> during the whole of the program execution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The set of real numbers is infinite, and a computer can only represent a subset of the real values. This subset is often called <em>floating point values<\/em>.&nbsp;Since only a subset can be represented, the evaluation of floating point expressions often imply that some values are rounded to a value that the computer can represent. For calculations of a large amount of numbers these rounding may lead to misleading and erroneous results. It is therefore recommended that people writing programs involving floating point calculations learn about how to try to avoid rounding errors.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background has-tiny-font-size wp-block-paragraph\">The above paragraph may be placed in a side box <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Integer values<\/h4>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background has-small-font-size wp-block-paragraph\">We need an example of a integer data-item in one of the previous chapters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the example in Chapter\/Section xx.yy, <code>anInt<\/code> is an example of a data-item that may represent an <code>integer<\/code> value. Integer is another example of a value type representing primitive values. Examples of integer values are 17, 111, -4, 0, and -1014.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An for Real, an integer may redeclared as a variable or a constant:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>anInt: <strong>var<\/strong> Integer\naConst: val 117<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The data-item <code>anInt<\/code> is an example of a variable that may hold different integers during the program execution. the data-item <code>aConst<\/code> on the other hand is constant and holding the value 117 during the whole programm execution.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Boolean values<\/h4>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background has-small-font-size wp-block-paragraph\">We need an example using booleans.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A data-item may be of type <code>Boolean<\/code> and such a data-item may have the value&nbsp;<code>True<\/code>&nbsp;or&nbsp;<code>False<\/code>.  A data-item of type&nbsp;<code>Boolean<\/code>&nbsp;may be declared as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code><code>&nbsp;&nbsp;&nbsp;B1:&nbsp;<strong>val<\/strong>&nbsp;True\n&nbsp;&nbsp;&nbsp;B2:&nbsp;<strong>var<\/strong>&nbsp;Boolean<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The data-item <code>B1<\/code> is a constant having the value True during the whole program execution. The data-item <code>B2<\/code> is a variable that may hold the values True or False at different point during the program execution. In the next example, <code>B2<\/code> is assigned the value False<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code><code>&nbsp;&nbsp;&nbsp;B2&nbsp;:=&nbsp;False<\/code><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Character values<\/h4>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background has-small-font-size wp-block-paragraph\">We need an example using char<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A data-item of type Char may hold a value representing a character from the UniCode character set. A character may be a letter, a digit, a special character, a blank\/space or an end-of-line character. A character literal is typically written within single quotes like:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code><code>&nbsp;&nbsp;&nbsp;\u2018a\u2019,&nbsp;\u2018b\u2019,&nbsp;\u2018Q\u2019,&nbsp;'L\u2019&nbsp;&nbsp;\u2014&nbsp;examples&nbsp;of&nbsp;letters\n&nbsp;&nbsp;&nbsp;\u20180\u2019,&nbsp;7\u2032&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u2014&nbsp;examples&nbsp;of&nbsp;digits\n&nbsp;&nbsp;&nbsp;\u2018.\u2019,&nbsp;\u2018:\u2019,&nbsp;\u2018+\u2019,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u2014&nbsp;examples&nbsp;of&nbsp;special&nbsp;characters\n&nbsp;&nbsp;&nbsp;\u2018&nbsp;\u2018&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u2014&nbsp;the&nbsp;blank&nbsp;character<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">An end-of-line character has no graphical form and is thus denoted as \u2018\\n\u2019. The character \u2018\\\u2019 is a so-called escape character the implies that the next character defines a special character. The following are examples of characters defined using the escape character<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code><code>&nbsp;&nbsp;&nbsp;\u2018\\n\u2019&nbsp;&nbsp;\u2014&nbsp;end-of-line\n&nbsp;&nbsp;&nbsp;\u2018\\t\u2019&nbsp;&nbsp;\u2014&nbsp;tab\n&nbsp;&nbsp;&nbsp;\u2018\\\u201d&nbsp;&nbsp;&nbsp;\u2014&nbsp;the&nbsp;quote&nbsp;character&nbsp;\u2018\n&nbsp;&nbsp;&nbsp;\u2026<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The following example shows the declaration of data-items of type char and assigment statements: <\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code><code>&nbsp;&nbsp;&nbsp;ch1:&nbsp;<strong>val<\/strong>&nbsp;\u2018!\u2019<br>&nbsp;&nbsp;&nbsp;ch2:&nbsp;<strong>var<\/strong>&nbsp;Char<br>&nbsp;&nbsp;&nbsp;ch2&nbsp;:=&nbsp;\u2018Q\u2019<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The data-item&nbsp;<code>ch1<\/code>&nbsp;is a a constant holding the exclamation mark character<code>&nbsp;'!'<\/code>. The data-item&nbsp;<code>ch2<\/code>&nbsp;is a variable of type&nbsp;<code>char<\/code>. It may hold different character values during the program execution. Execution of the assignment statement&nbsp;<code>ch2 := 'Q'&nbsp;<\/code>has the effect that&nbsp;<code>ch2<\/code>&nbsp;holds the character&nbsp;<code>'Q'<\/code>.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">String values<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As mention, owner is a data-item that may hold string values. A string value is a sequence of characters as opposed to char values, which are single characters. A string value may thus consist of zero or more characters. In the program text, a string value is enclosed in double quotes (&#8220;) and special characters are preceded by a slash (\\). Example are shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>\"John Smith\"       \n\"Hello world\\n\".          -- a newline is represented by \\n\n\"a + b * (c +111)\"\n\"He said: \\\"Go away!\\\"\"   -- the character \" is represented by \\\" in the string<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=74\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>I have preliminary put this page around Chapter 4, since I think it is needed here, but it is up for discussion. The same for the Chapter on 4B Statements &#8211; see also the note there! In this Chapter, we summarize +++bmp: hvis vi f\u00f8rst indf\u00f8rer value types her, da er det ikke l\u00e6ngere bare [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2825,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-74","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=74"}],"version-history":[{"count":41,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/74\/revisions"}],"predecessor-version":[{"id":2268,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/74\/revisions\/2268"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2825"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}