Dart Programming (Data Type in Dart)
Dart Programming
DartPad is an online, interactive code editor and playground for the Dart programming language. It allows developers to write, run, and experiment with Dart code directly in a web browser without needing to install any software locally. DartPad is a convenient tool for quickly prototyping, sharing code snippets, and learning Dart without the need for a full development environment setup. It's often used for educational purposes, code sharing, and trying out Dart features and libraries.
Link: https://dartpad.dev/?
Write Your Dart Code:
Create a new file with a .dart
extension (e.g., my_program.dart
) using a text editor or integrated development environment (IDE). You can use any text editor like VSCode, Sublime Text, or even a basic one like Notepad.
Here's a simple Dart program that prints "Hello, World!" to the console:
void main() {
print("Hello, World!");
}
Save the File: Save your Dart code in the .dart
file you created.
Run the Dart Program: dart my_program.dart
Output: Hello, World!
Data Types in Dart:
Dart supports several data types that you can use to represent different kinds of values. Here is a list of the main data types in Dart:
1. Numbers:
- `int`: Represents integer values.
- `double`: Represents floating-point (decimal) values.
2. Strings:
- `String`: Represents a sequence of characters.
3. Booleans:
- `bool`: Represents a Boolean value (`true` or `false`).
4. Lists:
- `List`: Represents an ordered collection of objects. Dart supports both fixed-size and growable lists.
5. Maps:
- `Map`: Represents a collection of key-value pairs, where each key is unique.
6. Sets:
- `Set`: Represents an unordered collection of unique objects.
7. Runes:
- `Runes`: Represents a sequence of Unicode code points. It is used for handling non-ASCII characters.
8. Symbols:
- `Symbol`: Represents an operator or identifier as a string, mainly used for reflection.
9. Functions:
- `Function`: Represents a function as a first-class citizen, which means functions can be assigned to variables, passed as arguments, and returned from other functions.
10. Objects:
- Dart is an object-oriented language, so you can create your own custom data types using classes.
11. Null:
- `Null`: Represents the absence of a value or uninitialized variables. In Dart, all variables are nullable by default unless you explicitly specify otherwise.
12.
var
Keyword:
- `var`
is used for declaring variables with type inference, which means that the Dart compiler determines the variable's data type based on the initial value assigned to it.
- Once the data type is inferred, it cannot be changed. The variable remains statically typed.
13. `dynamic`
is a special data type that represents a variable whose type is not known at compile-time.
- Type Flexibility: Variables of type dynamic
can hold values of any type, and their types can change during the execution of the program.
- No Static Type Checking: Because dynamic
variables can change their type.
void main()
{
print("Hello World!");
int a = 10;
print("This is the updated value of the A (int): ${a+10}");
double b = 534.379; // 'float' isn't a type.
print("This is the updated value of the B (float): ${b+10}");
String c = "My name is ${"Karhtikeyan A"}, My Age is: ${a+9}"; // 'float' isn't a type.
print("This is the value of the C (String): ${c}");
bool status = true;
print("My Marriage Status is: ${status}");
List<int> myMark = [85,97,100,58,90];
print("This is the updated value of the B (List): ${myMark}");
Map<String,List<String>> otherAct = {"Skills": ["Dart", "Flutter", "Java"],"hobby":["Cooking","Learning"]};
print("This is my Other info (Map): ${otherAct}");
// ``dynamic`` Data Type:
print("\n\t Dynamic Data Type");
dynamic myVariable = 42; // Assign an integer to a dynamic variable
print(myVariable); // Prints: 42
myVariable = "Hello, Dart!"; // Change the type to a string
print(myVariable); // Prints: Hello, Dart!
myVariable = [1, 2, 3]; // Change the type to a list
print(myVariable); // Prints: [1, 2, 3]
// Using the `var` keyword for type inference
print("\n\t Var Data Type");
var myVariable2 = 42; // Dart infers that myVariable2 is of type int
print(myVariable2); // Prints: 42
// You cannot change the type of myVariable2 after inference
// myVariable2 = "Hello, Dart!"; // This would cause a compile-time error
// However, you can assign values of the same inferred type
myVariable2 = 100; // Assigning another integer
print(myVariable2); // Prints: 100
}
Output:
Comments
Post a Comment