-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4. datatypes.dart
370 lines (294 loc) · 12.5 KB
/
4. datatypes.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/// Data Types in Dart
///
/// This codelab provides a comprehensive guide to understanding and using
/// data types in Dart, including numbers, strings, booleans, lists, maps,
/// sets, runes, null, and type conversion. It also covers exceptions and
/// best practices.
// ignore_for_file: equal_elements_in_set, unnecessary_type_check
void main() {
// --------------------------------------------------------------------------
// Introduction to Data Types
// --------------------------------------------------------------------------
/// **What are Data Types?**
///
/// Data types categorize the different kinds of data you use in your code.
/// They specify the type of value a variable can hold, such as numbers,
/// text, or logical values. Dart is an optionally-typed language, meaning
/// you can explicitly declare types or let Dart infer them.
// --------------------------------------------------------------------------
// Numbers (int, double, num)
// --------------------------------------------------------------------------
/// **Numbers:**
///
/// Dart uses three main types for numbers:
/// - `int`: Represents whole numbers (integers) without decimal points.
/// - `double`: Represents floating-point numbers (numbers with decimal points).
/// - `num`: A supertype for both `int` and `double`, allowing you to store either type.
print("\n--- Numbers ---");
// Declaring Variables
int num1 = 100; // Integer
double num2 = 130.2; // Double
num num3 = 50; // Can be int or double
num num4 = 50.4; // Can be int or double
// Performing Arithmetic
num sum = num1 + num2 + num3 + num4;
// Printing Information
print("Num 1 is $num1"); // Output: Num 1 is 100
print("Num 2 is $num2"); // Output: Num 2 is 130.2
print("Num 3 is $num3"); // Output: Num 3 is 50
print("Num 4 is $num4"); // Output: Num 4 is 50.4
print("Sum is $sum"); // Output: Sum is 330.6
/// **Rounding Double Values:**
///
/// The `.toStringAsFixed(n)` method rounds a double to `n` decimal places.
print("\n--- Rounding Double Values ---");
double price = 1130.2232323233233;
print(price.toStringAsFixed(2)); // Output: 1130.22
// --------------------------------------------------------------------------
// Strings (String)
// --------------------------------------------------------------------------
/// **Strings:**
///
/// Strings represent sequences of characters (text). They can be enclosed in
/// single quotes (`'`) or double quotes (`"`).
print("\n--- Strings ---");
// Declaring Values
String schoolName = "Diamond School";
String address = "New York 2140";
// Printing Values
print("School name is $schoolName and address is $address");
// Output: School name is Diamond School and address is New York 2140
/// **Multi-Line Strings:**
///
/// Use triple quotes (`'''` or `"""`) to create strings that span multiple lines.
print("\n--- Multi-Line Strings ---");
// Multi-Line Using Single Quotes
String multiLineText = '''
This is Multi Line Text
with 3 single quote
I am also writing here.
''';
// Multi-Line Using Double Quotes
String otherMultiLineText = """
This is Multi Line Text
with 3 double quote
I am also writing here.
""";
// Printing Information
print("Multiline text is $multiLineText");
/*
Output:
Multiline text is
This is Multi Line Text
with 3 single quote
I am also writing here.
*/
print("Other multiline text is $otherMultiLineText");
/*
Output:
Other multiline text is
This is Multi Line Text
with 3 double quote
I am also writing here.
*/
/// **Special Characters in Strings:**
///
/// Special characters like `\n` (newline) and `\t` (tab) can be used within strings.
print("\n--- Special Characters in Strings ---");
// Using \n and \t
print("I am from \nUS.");
/*
Output:
I am from
US.
*/
print("I am from \tUS."); // Output: I am from US.
/// **Raw Strings:**
///
/// Raw strings (prefixed with `r`) treat backslashes and special characters
/// as literal characters.
print("\n--- Raw Strings ---");
// Set price value
num priceRaw = 10;
String withoutRawString = "The value of price is \t $priceRaw"; // regular String
String withRawString = r"The value of price is \t $priceRaw"; // raw String
print("Without Raw: $withoutRawString");
// Output: Without Raw: The value of price is 10
print("With Raw: $withRawString");
// Output: With Raw: The value of price is \t $priceRaw
// --------------------------------------------------------------------------
// Type Conversion
// --------------------------------------------------------------------------
/// **Type Conversion:**
///
/// Dart allows you to convert between different data types.
print("\n--- Type Conversion ---");
/// **String to Int:**
///
/// Use `int.parse()` to convert a string to an integer.
print("\n--- String to Int ---");
String strvalue = "1";
print("Type of strvalue is ${strvalue.runtimeType}"); // Output: Type of strvalue is String
int intvalue = int.parse(strvalue);
print("Value of intvalue is $intvalue"); // Output: Value of intvalue is 1
print("Type of intvalue is ${intvalue.runtimeType}"); // Output: Type of intvalue is int
/// **String to Double:**
///
/// Use `double.parse()` to convert a string to a double.
print("\n--- String to Double ---");
String strvalueDouble = "1.1";
print("Type of strvalue is ${strvalueDouble.runtimeType}"); // Output: Type of strvalue is String
double doublevalue = double.parse(strvalueDouble);
print("Value of doublevalue is $doublevalue"); // Output: Value of doublevalue is 1.1
print("Type of doublevalue is ${doublevalue.runtimeType}"); // Output: Type of doublevalue is double
/// **Int to String:**
///
/// Use `.toString()` to convert an integer to a string.
print("\n--- Int to String ---");
int one = 1;
print("Type of one is ${one.runtimeType}"); // Output: Type of one is int
String oneInString = one.toString();
print("Value of oneInString is $oneInString"); // Output: Value of oneInString is 1
print("Type of oneInString is ${oneInString.runtimeType}"); // Output: Type of oneInString is String
/// **Double to Int:**
///
/// Use `.toInt()` to convert a double to an integer (truncates the decimal part).
print("\n--- Double to Int ---");
double num1Double = 10.01;
int num2Int = num1Double.toInt(); // converting double to int
print("The value of num1 is $num1Double. Its type is ${num1Double.runtimeType}");
// Output: The value of num1 is 10.01. Its type is double
print("The value of num2 is $num2Int. Its type is ${num2Int.runtimeType}");
// Output: The value of num2 is 10. Its type is int
/// **Type Conversion Exceptions:**
///
/// If you try to parse a string that is not a valid number, a `FormatException` will be thrown.
print("\n--- Type Conversion Exceptions ---");
try {
String invalidNumber = "abc";
int parsedNumber = int.parse(invalidNumber); // This will throw an exception
print(parsedNumber);
} catch (e) {
print("Error: $e"); // Output: Error: FormatException: Invalid radix-10 number (at character 1)
}
// --------------------------------------------------------------------------
// Booleans (bool)
// --------------------------------------------------------------------------
/// **Booleans:**
///
/// Booleans represent logical values, either `true` or `false`.
print("\n--- Booleans ---");
bool isMarried = true;
print("Married Status: $isMarried"); // Output: Married Status: true
// --------------------------------------------------------------------------
// Lists (List)
// --------------------------------------------------------------------------
/// **Lists:**
///
/// Lists are ordered collections of items. They are also known as arrays.
print("\n--- Lists ---");
List<String> names = ["Raj", "John", "Max"];
print("Value of names is $names"); // Output: Value of names is [Raj, John, Max]
print("Value of names[0] is ${names[0]}"); // Output: Value of names[0] is Raj
print("Value of names[1] is ${names[1]}"); // Output: Value of names[1] is John
print("Value of names[2] is ${names[2]}"); // Output: Value of names[2] is Max
// Finding Length of List
int length = names.length;
print("The Length of names is $length"); // Output: The Length of names is 3
/// **List Index Out of Range Exception:**
///
/// If you try to access an index that is outside the bounds of the list, a `RangeError` will be thrown.
print("\n--- List Index Out of Range Exception ---");
try {
print(names[3]); // This will throw an exception
} catch (e) {
print("Error: $e"); // Output: Error: RangeError (index): Index out of range: index should be less than 3: 3
}
// --------------------------------------------------------------------------
// Sets (Set)
// --------------------------------------------------------------------------
/// **Sets:**
///
/// Sets are unordered collections of unique items. They do not allow duplicates.
print("\n--- Sets ---");
Set<String> weekday = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
print(weekday); // Output: {Sun, Mon, Tue, Wed, Thu, Fri, Sat}
Set<int> uniqueNumbers = {1, 2, 3, 3, 4, 4, 5};
print(uniqueNumbers); // Output: {1, 2, 3, 4, 5} (Duplicates are removed)
// --------------------------------------------------------------------------
// Maps (Map)
// --------------------------------------------------------------------------
/// **Maps:**
///
/// Maps store data in key-value pairs. Each key is unique, but values can be repeated.
print("\n--- Maps ---");
Map<String, String> myDetails = {
'name': 'John Doe',
'address': 'USA',
'fathername': 'Soe Doe'
};
// displaying the output
print(myDetails['name']); // Output: John Doe
/// **Map Key Not Found Exception:**
///
/// If you try to access a key that does not exist in the map, `null` will be returned.
print("\n--- Map Key Not Found Exception ---");
print(myDetails['mothername']); // Output: null
// --------------------------------------------------------------------------
// Var Keyword
// --------------------------------------------------------------------------
/// **Var Keyword:**
///
/// The `var` keyword allows Dart to infer the data type of a variable.
print("\n--- Var Keyword ---");
var name = "John Doe"; // String
var age = 20; // int
print(name); // Output: John Doe
print(age); // Output: 20
// --------------------------------------------------------------------------
// Runes
// --------------------------------------------------------------------------
/// **Runes:**
///
/// Runes represent Unicode code points of strings.
print("\n--- Runes ---");
String value = "a";
print(value.runes); // Output: (97)
// --------------------------------------------------------------------------
// Checking Runtime Type
// --------------------------------------------------------------------------
/// **Checking Runtime Type:**
///
/// Use `.runtimeType` to check the data type of a variable at runtime.
print("\n--- Checking Runtime Type ---");
var a = 10;
print(a.runtimeType); // Output: int
print(a is int); // Output: true
// --------------------------------------------------------------------------
// Optionally Typed Language
// --------------------------------------------------------------------------
/// **Optionally Typed Language:**
///
/// Dart is an optionally-typed language, supporting both static and dynamic typing.
///
/// - **Statically Typed:** Data types are known at compile time.
/// - **Dynamically Typed:** Data types are known at runtime.
print("\n--- Optionally Typed Language ---");
/// **Statically Typed Example:**
///
/// If you use `var` and assign a value, the type is inferred and fixed.
print("\n--- Statically Typed Example ---");
var myVariable = 50; // You can also use int instead of var
print(myVariable.runtimeType); // Output: int
// myVariable = "Hello"; // this will give error
// print(myVariable); // Error: A value of type 'String' can't be assigned to a variable of type 'int'.
/// **Dynamically Typed Example:**
///
/// If you use `dynamic`, the type can change at runtime.
print("\n--- Dynamically Typed Example ---");
dynamic myDynamicVariable = 50;
print(myDynamicVariable.runtimeType); // Output: int
myDynamicVariable = "Hello";
print(myDynamicVariable.runtimeType); // Output: String
print(myDynamicVariable); // Output: Hello
}