1 00:00:01,080 --> 00:00:01,950 ‫Welcome back. 2 00:00:01,980 --> 00:00:05,250 ‫In this video we are going to look into nobles. 3 00:00:05,250 --> 00:00:10,350 ‫And nobles are a concept that you see in other programming languages. 4 00:00:10,350 --> 00:00:15,840 ‫For example, Swift uses them quite extensively and there they are called options. 5 00:00:15,840 --> 00:00:21,210 ‫So they are variables that can have a value or they don't have a value. 6 00:00:21,210 --> 00:00:23,610 ‫So there are two options, right? 7 00:00:23,610 --> 00:00:29,310 ‫So either something has a value or does not have a value, and usually that's null or not. 8 00:00:29,310 --> 00:00:30,870 ‫So let's create some. 9 00:00:31,230 --> 00:00:35,610 ‫And by the way, it's a data type, so the data type can be empty or not. 10 00:00:35,610 --> 00:00:44,880 ‫And usually that's like Nullarbor as you can see here and Nullarbor of type int 32 for example. 11 00:00:44,880 --> 00:00:48,840 ‫So that's generally how you write, how you would write it. 12 00:00:49,470 --> 00:00:51,570 ‫That would be a Nullarbor in 32. 13 00:00:51,570 --> 00:00:58,920 ‫So evaluated can have values from -2 billion roughly up to plus 2 billion. 14 00:00:58,920 --> 00:01:01,260 ‫So let's go ahead and create them. 15 00:01:01,260 --> 00:01:07,200 ‫So in order to create a novel, you just add a question mark after the data type. 16 00:01:07,200 --> 00:01:13,920 ‫So if you want to have a integer Nullarbor as I've just written, you will just use end question mark 17 00:01:13,920 --> 00:01:17,880 ‫and I'm just going to call that number one and it's going to be null. 18 00:01:18,900 --> 00:01:23,490 ‫Let's create another one that I'm calling NOM two and set that to NULL. 19 00:01:24,330 --> 00:01:31,800 ‫And as you can see, we get an error here so it's complaining it says cannot convert null to int because 20 00:01:31,800 --> 00:01:35,490 ‫it is not or a non unknowable value type. 21 00:01:35,490 --> 00:01:40,020 ‫So you cannot assign null to an integer. 22 00:01:40,080 --> 00:01:46,890 ‫That's what you say C here, but you can assign null to a global integer, which we've done in line 23 00:01:46,890 --> 00:01:47,490 ‫nine. 24 00:01:47,730 --> 00:01:56,580 ‫All right, so let's create another one and it will be num two and it will be 1337 1337. 25 00:01:56,970 --> 00:02:00,630 ‫That's how you would go ahead and create an elbow. 26 00:02:00,930 --> 00:02:04,170 ‫When you see it's either null or it has a value. 27 00:02:04,200 --> 00:02:05,730 ‫The same goes for doubles. 28 00:02:05,730 --> 00:02:08,070 ‫So double question mark. 29 00:02:08,070 --> 00:02:15,240 ‫Number three is new double and question mark like that. 30 00:02:15,240 --> 00:02:19,080 ‫So that's also how you can assign a multiple double. 31 00:02:19,080 --> 00:02:28,710 ‫So this one will be empty or here num four is 3.14157. 32 00:02:28,710 --> 00:02:30,210 ‫So that's PI. 33 00:02:30,570 --> 00:02:32,940 ‫And the same goes for Booleans. 34 00:02:32,940 --> 00:02:41,940 ‫So it's a multiple bool and I'm going to say Boo Vol is a new bool 35 00:02:44,340 --> 00:02:47,310 ‫question mark like that. 36 00:02:48,330 --> 00:02:49,470 ‫So now let's check it out. 37 00:02:49,470 --> 00:02:53,640 ‫Let's just display that on the screen and see what we get. 38 00:02:53,640 --> 00:03:07,350 ‫So see w our global numbers are and we're going to use zero one. 39 00:03:09,890 --> 00:03:13,160 ‫Two and three. 40 00:03:13,460 --> 00:03:15,140 ‫And that will be number one. 41 00:03:15,860 --> 00:03:18,170 ‫Numb to numb. 42 00:03:18,410 --> 00:03:20,180 ‫Three and four. 43 00:03:20,300 --> 00:03:24,320 ‫And now, of course, it's complaining because I need to end my string here. 44 00:03:24,800 --> 00:03:25,400 ‫All right. 45 00:03:25,400 --> 00:03:29,630 ‫And let's also display our bool. 46 00:03:29,630 --> 00:03:43,280 ‫So c w the multiple boolean value is and bool vol. 47 00:03:45,140 --> 00:03:45,770 ‫Now okeydokey. 48 00:03:45,770 --> 00:03:48,290 ‫So let's try that control f five. 49 00:03:50,540 --> 00:03:54,200 ‫And our knowable numbers are, as you can see, nothing. 50 00:03:55,790 --> 00:03:57,410 ‫So it's not even an empty space. 51 00:03:57,410 --> 00:03:58,250 ‫It's just nothing. 52 00:03:58,250 --> 00:04:03,410 ‫One, three, three, seven, nothing and 31415, seven. 53 00:04:03,410 --> 00:04:06,440 ‫And the null boolean value is nothing. 54 00:04:06,440 --> 00:04:09,770 ‫So as you can see, it can be nothing and it's not complaining. 55 00:04:09,770 --> 00:04:14,570 ‫So even though there is no value in there, it's still fine. 56 00:04:14,570 --> 00:04:17,570 ‫It's not crashing our program or stuff like that. 57 00:04:17,690 --> 00:04:23,270 ‫If you would try to do the same thing with an integer which is not knowable, so let's create an integer 58 00:04:23,270 --> 00:04:26,870 ‫called number five and let's just do it like that. 59 00:04:26,870 --> 00:04:30,650 ‫So we didn't initialize it and it's also empty, right? 60 00:04:30,650 --> 00:04:37,070 ‫So there is no value in there if I try to access that one like that. 61 00:04:37,070 --> 00:04:40,940 ‫And I'm going to use number five here. 62 00:04:41,300 --> 00:04:42,470 ‫Let's check that out. 63 00:04:42,470 --> 00:04:43,820 ‫Control the five. 64 00:04:44,270 --> 00:04:49,790 ‫And as you can see, we have build errors so we can't even stop that because it says use of unassigned 65 00:04:49,790 --> 00:04:51,380 ‫local variable number five. 66 00:04:51,890 --> 00:04:53,870 ‫So that's the whole point about that. 67 00:04:53,870 --> 00:04:59,840 ‫You create a variable that can be empty or it cannot be empty and that's totally fine with the program. 68 00:04:59,840 --> 00:05:00,800 ‫It won't crash. 69 00:05:02,090 --> 00:05:03,470 ‫And why would you use that? 70 00:05:03,500 --> 00:05:06,890 ‫What would be the idea behind Noble's? 71 00:05:07,100 --> 00:05:15,290 ‫Well, imagine you have a program which works with loads of data, so it has user data and it gets it 72 00:05:15,290 --> 00:05:16,520 ‫from the database. 73 00:05:16,520 --> 00:05:22,150 ‫And for some values or for some columns, there are simply no entries. 74 00:05:22,160 --> 00:05:25,250 ‫So let's say we have usernames. 75 00:05:25,250 --> 00:05:31,830 ‫We have personal name or first name, last name, then we have birthday and so forth. 76 00:05:31,850 --> 00:05:37,220 ‫All of those information can be stored within our database, but for most users we only have the username. 77 00:05:37,220 --> 00:05:40,400 ‫We don't have their first name, last name. 78 00:05:40,400 --> 00:05:43,550 ‫We don't have the password or email address, nothing like that. 79 00:05:43,580 --> 00:05:48,320 ‫We only have the username but we still want our program to work correctly. 80 00:05:48,320 --> 00:05:51,260 ‫So we will just use nobles which are fine. 81 00:05:51,260 --> 00:05:52,430 ‫If there is no value. 82 00:05:52,460 --> 00:05:54,500 ‫They will not make our program crash. 83 00:05:54,800 --> 00:05:57,890 ‫So let's make it very, very generic example of that. 84 00:05:58,490 --> 00:06:07,520 ‫And I'm going to use a bool here, which will be a multiple bool and I'm going to call it Ismael and 85 00:06:07,520 --> 00:06:09,080 ‫I'm going to set it to NULL. 86 00:06:09,380 --> 00:06:15,140 ‫And in here I'm going to just check is mail, is it null or actually is it true? 87 00:06:15,170 --> 00:06:27,860 ‫So if it's true, let's write something like user is mail then else if it's mail is false, we're going 88 00:06:27,860 --> 00:06:31,970 ‫to say something like user is female. 89 00:06:33,260 --> 00:06:46,190 ‫And then finally else we just say something like no gender chosen or maybe something like no gender 90 00:06:46,190 --> 00:06:49,520 ‫could be found or something like that, and that we are. 91 00:06:49,520 --> 00:06:52,880 ‫So now you can go ahead and use that kind of approach. 92 00:06:52,880 --> 00:06:53,390 ‫Right. 93 00:06:53,960 --> 00:06:56,930 ‫And here I'm going to put it in the next line. 94 00:06:59,370 --> 00:07:02,900 ‫And if we run that well, of course, we get no gender chosen. 95 00:07:02,910 --> 00:07:08,250 ‫And in my case, my program will shut down straight away after it says that. 96 00:07:08,760 --> 00:07:10,740 ‫And I still have. 97 00:07:13,260 --> 00:07:16,380 ‫An error somewhere and. 98 00:07:16,380 --> 00:07:18,160 ‫Well, how would I figure it out? 99 00:07:18,180 --> 00:07:19,410 ‫Well, it's this one here. 100 00:07:19,410 --> 00:07:23,400 ‫So I have this for still in my text and it's trying to access it. 101 00:07:23,400 --> 00:07:26,460 ‫But there is something missing, so that's why it doesn't work. 102 00:07:26,550 --> 00:07:27,810 ‫All right, now we can try it again. 103 00:07:27,810 --> 00:07:33,600 ‫Control F five and we are our knowable numbers are. 104 00:07:34,050 --> 00:07:34,980 ‫That's totally fine. 105 00:07:34,980 --> 00:07:37,200 ‫And then here, no gender chosen. 106 00:07:39,840 --> 00:07:40,140 ‫All right. 107 00:07:40,140 --> 00:07:41,310 ‫That's all good and stuff. 108 00:07:41,310 --> 00:07:47,220 ‫But what if you want to convert a gullible into a non gullible type? 109 00:07:47,580 --> 00:07:49,800 ‫Well, let's go ahead and try that. 110 00:07:50,430 --> 00:07:53,700 ‫Let's create a gullible double here. 111 00:07:54,990 --> 00:08:03,930 ‫Let's call it number five and it's going to be 13.1 and then double or actually let's call it number 112 00:08:03,930 --> 00:08:08,940 ‫six, because we have this number five here, which we never used, but I'm just going to leave it there. 113 00:08:08,940 --> 00:08:10,440 ‫It's not hurting anyone. 114 00:08:10,440 --> 00:08:13,230 ‫So number seven is going to be null. 115 00:08:13,560 --> 00:08:23,460 ‫So how do I convert those into a non knowledgeable type and I'm going to create a NUM eight which will 116 00:08:23,460 --> 00:08:25,790 ‫be a non knowable double. 117 00:08:26,100 --> 00:08:28,590 ‫So it's a general double, right. 118 00:08:28,590 --> 00:08:31,920 ‫It can't be null or we cannot assign null to it. 119 00:08:32,160 --> 00:08:44,040 ‫And now we just check if num six equals null then num eight will be 0.0. 120 00:08:45,030 --> 00:08:54,990 ‫And else we're just going to say num eight should be num num six six. 121 00:08:55,290 --> 00:09:04,740 ‫But there we have the problem because as you can see that we entered that I cannot just assign it it 122 00:09:04,740 --> 00:09:12,270 ‫says cannot implicitly convert type double question mark to double and explicit conversion exists. 123 00:09:12,270 --> 00:09:13,860 ‫So are you missing a cast? 124 00:09:13,860 --> 00:09:15,240 ‫And that's what we need to do. 125 00:09:15,240 --> 00:09:24,090 ‫So we need to cast it into a double because we need we know if it's not now, then num six internally 126 00:09:24,090 --> 00:09:26,580 ‫is a double, so it contains a double value. 127 00:09:26,580 --> 00:09:35,220 ‫So I'm just going to use that and assign that to NUM eight so we can just go ahead and display that 128 00:09:35,220 --> 00:09:42,510 ‫value of NUM eight is num eight. 129 00:09:43,770 --> 00:09:46,320 ‫So let's start out with control f five. 130 00:09:46,500 --> 00:09:49,980 ‫And as you can see, value of NUM eight is 13.1. 131 00:09:49,980 --> 00:09:54,450 ‫So the casting, the explicit conversion worked. 132 00:09:56,050 --> 00:09:58,840 ‫There is, however, a faster way to do that. 133 00:09:58,840 --> 00:10:01,540 ‫So as you can see, it was quite some effort. 134 00:10:01,540 --> 00:10:02,380 ‫We needed this. 135 00:10:02,380 --> 00:10:08,680 ‫If an LS there is the null coalescing operator that we can use for that. 136 00:10:08,680 --> 00:10:12,700 ‫So we can just say num eight is equal to num six. 137 00:10:12,730 --> 00:10:13,570 ‫Question mark. 138 00:10:13,570 --> 00:10:16,930 ‫Question mark 8.53 for example. 139 00:10:16,930 --> 00:10:29,020 ‫So if num six is empty, then go ahead and add or enter 8.53 and if not, then do something else. 140 00:10:29,020 --> 00:10:36,640 ‫So what I'm going to do is I'm going to add another line and I'm going to write something like value 141 00:10:36,640 --> 00:10:48,670 ‫of num eight is and actually let's just copy that because we need that twice and I don't want to type 142 00:10:48,670 --> 00:10:52,840 ‫so much so that we are now let's go ahead and try that again. 143 00:10:52,990 --> 00:10:54,250 ‫Let's see the difference. 144 00:10:56,080 --> 00:11:04,360 ‫And as you can see, value of NUM eight is 13.1 value of num eight is 13.1 and value of NUM eight is 145 00:11:04,360 --> 00:11:06,130 ‫8.53. 146 00:11:06,130 --> 00:11:07,570 ‫So what happened here? 147 00:11:07,570 --> 00:11:16,150 ‫What was ignored here in line 50 because it said well, it was well, I assigned 8.53 to it. 148 00:11:16,150 --> 00:11:18,640 ‫But as it seems now, I'm six. 149 00:11:19,520 --> 00:11:23,120 ‫Had a value and it was taken. 150 00:11:23,120 --> 00:11:32,660 ‫So as six had a value, just took a 13.1 and as number seven didn't have a value, num eight now is 151 00:11:32,660 --> 00:11:39,800 ‫8.53 because if we look at it, num seven is null, so we assign a different value to it. 152 00:11:39,800 --> 00:11:43,010 ‫So that's called I'm just going to. 153 00:11:44,990 --> 00:11:46,550 ‫Copy it over for you. 154 00:11:47,060 --> 00:11:49,880 ‫The null collapsing operator. 155 00:11:49,970 --> 00:11:51,150 ‫Double question mark. 156 00:11:51,170 --> 00:11:58,340 ‫So if you want to convert from a normal to a non movable type, then you can use the null coalescing 157 00:11:58,340 --> 00:11:59,150 ‫operator.