Lesson 6

Dart Data Types and Conversion Essentials

Topic Overview

Greetings, future Dart programmers! Are you prepared to delve deeper into Dart? Our upcoming adventure explores fundamental language constructs: Basic Data Types and Type Conversion. We'll examine the process of converting one type of data to another, a process akin to decrypting alien messages into human speech. We'll study both explicit and implicit conversions and learn to recognize potential pitfalls. So, let's supercharge our cognitive engines!

Implicit Conversions

Dart, being a type-safe language, generally does not perform many implicit conversions to avoid unexpected behavior and maintain code clarity. However, Dart does allow some types to be used interchangeably in a context where it makes sense, without the need for explicit conversion. A common scenario involves numeric types and the num superclass.

For example, Dart implicitly allows the assignment of an int value to a variable of type num because num is a common superclass for both int and double types:

1num n = 10; // Implicitly, an integer is also a num. 2print("The value of n: $n"); // Output: The value of n: 10

This works because num can hold either an integer or a double value, with Dart intelligently inferring the specific type based on the assigned value. However, for most other type conversions, Dart requires explicit action.

Explicit Conversions

When it comes to converting types that are not implicitly interchangeable, like converting double to int or vice versa, Dart requires explicit conversion to ensure the programmer's intention is clear, safeguarding against potential data loss or precision issues. Here's how you can convert a double to an int explicitly, and vice versa:

1double d = 10.25; // a double value 2int i = d.toInt(); // explicitly casting the double to int 3print("The value of i: $i"); // Output: The value of i: 10 4 5int j = 100; // an integer 6double e = j.toDouble(); // explicitly converting int to double 7print("The value of e: $e"); // Output: The value of e: 100.0

Notice that converting a double to an int explicitly removes the fractional part. When casting an int to a double, the number becomes a floating-point, but its value essentially remains the same.

Converting to and from Strings

A common type of conversion in Dart involves transforming to and from String values. We often need to convert numbers into strings for display purposes, while sometimes we need to parse strings as numbers for computation.

1int ten = 10; // an integer value of 10 2String tenString = ten.toString(); // A string with value "10" 3print("The value of tenString: $tenString"); // Output: The value of tenString: 10 4 5String twentyFiveString = "25"; 6int twentyFive = int.parse(twentyFiveString); 7print("The value of twentyFive: $twentyFive"); // Output: The value of twentyFive: 25 8 9String invalidNumber = "25abc"; 10int number = int.parse(invalidNumber); // Oops! This will throw an error, "25abc" is not a number!

In converting to String, we employ the num.toString() method for int and double types. For the conversion from String to an integer, we utilize the int.parse(str) method. Similarly,double.parse(str) is used to obtain double numbers from a string.

Lesson Summary and Practice

Well done! You've mastered Basic Data Types and Type Conversion in Dart! Now, you should understand both explicit and implicit conversions and be aware of the potential hurdles in data conversion.

For our next step, let's take on some hands-on exercises to reinforce the concepts. Practicing is a perfect way to apply your newly gained knowledge and refine your skills!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.