Difference between revisions of "Conversions"

From Coder Merlin
Line 23: Line 23:


== Type Conversion ==
== Type Conversion ==
'''Type conversion''' is a more extensive process than ''type casting'' because it involves the creation of a new value based on an existing value of a different type.
'''Type conversion''' is a more extensive process than ''type casting'' because it involves the creation of a new value based on an existing value of a different type. As we learned previously, Doubles and Integers are stored using very different bit patterns, i.e. 1.0 (a Double) is very different from 1 (an Int).  But we'll encounter many situations where we'll want to perform an operation on values of more than one type, for example, and an Int to a Double.  In these cases, we'll need to form a new value of the appropriate type using an existing value.  Let's look at an example in Swift:
<syntaxhighlight lang="swift" line highlight="3">
let n = 1
let d = 2.0
let r = n + d
</syntaxhighlight>
Line 3 will result in the error: <span style="color:red"> error: binary operator '+' cannot be applied to operands of type 'Int' and 'Double'</span>.  This is because Swift is protecting us from ''accidentally'' adding operands of different types.  If we choose to do so, we'll need to do so explicitly.  We can accomplish this by creating a new, unnamed, temporary constant of the desired type as follows:
<syntaxhighlight lang="swift" line highlight="3">
let n = 1
let d = 2.0
let r = Double(n) + d
</syntaxhighlight>


== Key Concepts ==
== Key Concepts ==

Revision as of 00:09, 8 November 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Boeing, N780BA, B747-409(LCF) Dreamlifter

Prerequisites[edit]

Introduction[edit]

In some cases we have a value of one type but want to use it in another context, as a different type. Depending on language, this may be referred to as "type conversion", "type casting", or "type coercion". Often, type casting refers to the mere re-interpretation of existing bits, while type conversion creates a new representation of data as a different type.

Type Casting[edit]

When type casting, we consider an existing bit pattern as other than that originally declared. For example, consider the following C program:

#include <stdio.h>
int main(int argc, char *argv[]) {
  int c = 65;
  printf("The value is: %i", c);
  printf("The value is: %c", c);
  return 0;
}

On line 3, we declare an integer variable, "c" and assign the initial value, 65. On line 4, we print the integer value. On line 5, we use the printf command to indicate that we actually have a character, rather than an integer. The existing bit pattern is then re-interpreted and the character "A" is printed. (If you don't remember why it's "A", review W1033 Character Encoding.

Type Conversion[edit]

Type conversion is a more extensive process than type casting because it involves the creation of a new value based on an existing value of a different type. As we learned previously, Doubles and Integers are stored using very different bit patterns, i.e. 1.0 (a Double) is very different from 1 (an Int). But we'll encounter many situations where we'll want to perform an operation on values of more than one type, for example, and an Int to a Double. In these cases, we'll need to form a new value of the appropriate type using an existing value. Let's look at an example in Swift:

let n = 1
let d = 2.0
let r = n + d

Line 3 will result in the error: error: binary operator '+' cannot be applied to operands of type 'Int' and 'Double'. This is because Swift is protecting us from accidentally adding operands of different types. If we choose to do so, we'll need to do so explicitly. We can accomplish this by creating a new, unnamed, temporary constant of the desired type as follows:

let n = 1
let d = 2.0
let r = Double(n) + d

Key Concepts[edit]

Exercises[edit]

References[edit]