Difference between revisions of "Conversions"
Jeff-strong (talk | contribs) m (Editorial review and minor corrections) |
|||
Line 6: | Line 6: | ||
== Introduction == | == Introduction == | ||
In some cases we have a value of one type but want to use it in another context, as a different type. | 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 | ''Depending on language'', this could be referred to as "type conversion," "type casting," or "type coercion." Often, '''type casting''' refers to the mere reinterpretation of existing bits, whereas '''type conversion''' creates a new representation of data as a different type. | ||
== Type Casting == | == Type Casting == | ||
When type casting, we consider an existing bit pattern as other than that originally declared. | When type casting, we consider an existing bit pattern as other than that originally declared. For example, consider the following C program: | ||
<syntaxhighlight lang="c" line> | <syntaxhighlight lang="c" line> | ||
#include <stdio.h> | #include <stdio.h> | ||
Line 24: | Line 24: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
On line 3, we declare an integer variable, "c" and assign the initial value, 65. | 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 reinterpreted and the ''character'' "A" is printed. (If you don't remember why it's A, review [[W1033 Character Encoding]]). | ||
== Type Conversion == | == Type Conversion == | ||
'''Type conversion''' is a more extensive process than ''type casting'' because it involves | '''Type conversion''' is a more extensive process than ''type casting'' because it involves creating 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: | ||
{{CodeExplorer | {{CodeExplorer | ||
|exerciseID=1 | |exerciseID=1 | ||
Line 38: | Line 38: | ||
print(r) | print(r) | ||
}} | }} | ||
Run the above. What happens on line 3? Swift is protecting us from ''accidentally'' adding operands of different types. | Run the above. What happens on line 3? 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 do this by creating a ''new, unnamed, temporary constant'' of the desired type as follows: | ||
{{CodeExplorer | {{CodeExplorer | ||
|exerciseID=2 | |exerciseID=2 | ||
Line 61: | Line 61: | ||
print(c) | print(c) | ||
}} | }} | ||
We use the same methodology, i.e. creating new, temporary constants for other types of conversions, such as from Int to String: | We use the same methodology, i.e., creating new, temporary constants for other types of conversions, such as from Int to String: | ||
{{CodeExplorer | {{CodeExplorer | ||
|exerciseID=4 | |exerciseID=4 | ||
Line 72: | Line 72: | ||
print(r) | print(r) | ||
}} | }} | ||
It's important to note that the operators will be interpreted in accordance with the operands ''after'' any conversions have taken place. | It's important to note that the operators will be interpreted in accordance with the operands ''after'' any conversions have taken place. As such, the operator on line 3 above specifies ''concatenation'' rather than addition. | ||
{{CodeExplorer | {{CodeExplorer | ||
|exerciseID=5 | |exerciseID=5 | ||
Line 87: | Line 87: | ||
== Key Concepts == | == Key Concepts == | ||
{{KeyConcepts| | {{KeyConcepts| | ||
* Changing the type of a value | * Changing the type of a value can be referred to, depending on the language, as "type conversion," "type casting," or "type coercion." | ||
* '''Type casting''' refers to the mere | * '''Type casting''' refers to the mere reinterpretation of existing bits | ||
* '''Type conversion''' creates a new representation of data as a different type | * '''Type conversion''' creates a new representation of data as a different type | ||
* Operators will be interpreted in accordance with the operands ''after any conversions'' have taken place | * Operators will be interpreted in accordance with the operands ''after any conversions'' have taken place |
Latest revision as of 08:54, 11 February 2023
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 could be referred to as "type conversion," "type casting," or "type coercion." Often, type casting refers to the mere reinterpretation of existing bits, whereas 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);
int d = -c;
printf("The value is: %i\n", d);
printf("The value is: %u\n", d);
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 reinterpreted 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 creating 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:
Run the above. What happens on line 3? 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 do this by creating a new, unnamed, temporary constant of the desired type as follows:
Pay close attention to line 3, where we create a Double from n, and then add the result to the Double d.
We use the same methodology, i.e., creating new, temporary constants for other types of conversions, such as from Int to String:
It's important to note that the operators will be interpreted in accordance with the operands after any conversions have taken place. As such, the operator on line 3 above specifies concatenation rather than addition.
Key Concepts[edit]
- Changing the type of a value can be referred to, depending on the language, as "type conversion," "type casting," or "type coercion."
- Type casting refers to the mere reinterpretation of existing bits
- Type conversion creates a new representation of data as a different type
- Operators will be interpreted in accordance with the operands after any conversions have taken place
Exercises[edit]
After completing Printing and String Interpolation:
- M1039-10 Complete Merlin Mission Manager Mission M1039-10.
- M1039-11 Complete Merlin Mission Manager Mission M1039-11.
References[edit]
- Type Conversion (Wikipedia)
- Type Casting (Swift Documentation)
Experience Metadata
Experience ID | W1039 |
---|---|
Next experience ID | |
Unit | Encoding & data types |
Knowledge and skills | §10.711 |
Topic areas | Primitive types |
Classroom time | 20 m |
Study time | 30 m |
Acquired knowledge | understanding type casting and type conversions with regard to primitive types |
Acquired skill | ability to identify the appropriate target type for a conversion ability to perform conversions to an appropriate type |
Additional categories |