1 00:00:00,970 --> 00:00:01,380 ‫All right. 2 00:00:01,390 --> 00:00:06,310 ‫Now that you have seen the theory behind variables and data types, let's actually use them in practice. 3 00:00:06,310 --> 00:00:10,540 ‫And we're going to start with integers, doubles as well as floats. 4 00:00:10,540 --> 00:00:12,190 ‫So let's get started. 5 00:00:12,190 --> 00:00:19,590 ‫First of all, in order to declare a variable, we just use the data type and then we give the variable 6 00:00:19,600 --> 00:00:20,200 ‫a name. 7 00:00:20,200 --> 00:00:22,570 ‫And of course we need to add the semicolon. 8 00:00:22,570 --> 00:00:28,750 ‫So you see here I'm declaring a variable even though I'm not giving it a value, even though I'm not 9 00:00:28,750 --> 00:00:33,160 ‫initializing the variable, which means I'm not assigning a value to it. 10 00:00:33,160 --> 00:00:39,910 ‫So if I now assign a value to it, I can do that after the fact, for example, in the next line. 11 00:00:39,910 --> 00:00:45,730 ‫So here I can assign a value to a variable just by using the variable name. 12 00:00:45,730 --> 00:00:50,890 ‫And now I'm saying at this position I want to store the value of 13. 13 00:00:51,820 --> 00:00:58,810 ‫So here we declared the variable and now we are assigning a value or we are initializing that variable. 14 00:00:59,400 --> 00:01:07,870 ‫Okay, now let's actually write this variable onto our console so we can write the variable using console 15 00:01:07,870 --> 00:01:08,590 ‫right line. 16 00:01:08,590 --> 00:01:13,960 ‫And then in brackets we just use num one here and it will then display num one for us. 17 00:01:13,960 --> 00:01:20,890 ‫So depending on the settings that you have, you will potentially also need to use console to read in 18 00:01:20,890 --> 00:01:25,630 ‫order to keep your console open and running when testing this. 19 00:01:25,630 --> 00:01:29,680 ‫So let's test this real quick and we can see it says 13. 20 00:01:30,700 --> 00:01:33,220 ‫So here this 13 up there. 21 00:01:33,760 --> 00:01:35,080 ‫Okay, so that works. 22 00:01:35,110 --> 00:01:37,210 ‫Now we can display a value. 23 00:01:37,210 --> 00:01:39,490 ‫So let's display something more interesting. 24 00:01:39,490 --> 00:01:42,190 ‫Let's actually display the sum of two variables. 25 00:01:42,190 --> 00:01:45,970 ‫So now we have NUM one which currently is 13. 26 00:01:46,450 --> 00:01:52,090 ‫Now let's create another variable called NUM two. 27 00:01:52,330 --> 00:01:57,250 ‫So I'm going to declare and initialize the value in the same line. 28 00:01:57,250 --> 00:02:06,430 ‫So here we are declaring and initializing the variable in one go we're saying data type integer name 29 00:02:06,430 --> 00:02:09,250 ‫num two is going to be 23. 30 00:02:09,250 --> 00:02:15,970 ‫So now internally for this variable name, numb to it will store the value of 23. 31 00:02:16,150 --> 00:02:21,460 ‫So what we can do now, of course, is apply mathematical operations on it. 32 00:02:21,460 --> 00:02:28,390 ‫So let's just create a new variable called sum and this will now be the sum of NUM one and num two. 33 00:02:28,390 --> 00:02:33,910 ‫And by the way, once I do that, you can see that the underline goes away. 34 00:02:33,910 --> 00:02:40,000 ‫So here, this one here where the number two is never used, you see this green underlining now it goes 35 00:02:40,000 --> 00:02:43,990 ‫away because this green underlining just says that this variable is never used. 36 00:02:43,990 --> 00:02:47,890 ‫So now we are actually using it in this line here. 37 00:02:48,460 --> 00:02:57,250 ‫Okay, so now we have the sum, let's actually print out the sum of 13 and 23 in this bright line statement 38 00:02:57,250 --> 00:02:57,670 ‫here. 39 00:02:58,590 --> 00:02:59,870 ‫Okay, let's do that. 40 00:02:59,880 --> 00:03:01,290 ‫Let's run our program. 41 00:03:01,290 --> 00:03:08,550 ‫And it says 36 because 23 plus 13 or 13 plus 23 is going to be 36. 42 00:03:09,000 --> 00:03:09,420 ‫All right. 43 00:03:09,420 --> 00:03:10,200 ‫That's great. 44 00:03:10,290 --> 00:03:14,070 ‫But now I would like to actually see what I am doing there. 45 00:03:14,190 --> 00:03:17,490 ‫Therefore, I can use this concept of concatenation. 46 00:03:17,520 --> 00:03:19,320 ‫Let me show you real quick. 47 00:03:19,530 --> 00:03:23,760 ‫So here, concatenation where I'm using. 48 00:03:24,800 --> 00:03:26,300 ‫The number one. 49 00:03:26,330 --> 00:03:29,600 ‫I'm saying number one on the console plus. 50 00:03:30,330 --> 00:03:36,450 ‫Which means it's going to display num one with the actual value of none one. 51 00:03:36,450 --> 00:03:43,930 ‫So this variable here then I'm saying in the text the empty space plus empty space numb to. 52 00:03:43,950 --> 00:03:48,150 ‫So now I'm using the value of num two here plus num two. 53 00:03:49,070 --> 00:03:50,180 ‫Is some. 54 00:03:50,390 --> 00:03:52,450 ‫So this looks a little complicated. 55 00:03:52,460 --> 00:03:55,430 ‫Let's break it down in a second, but let's first run it. 56 00:03:55,430 --> 00:03:57,890 ‫So you see, number one is 13 plus. 57 00:03:57,890 --> 00:04:00,080 ‫Number two is 23 is 36. 58 00:04:00,080 --> 00:04:03,630 ‫So here I would need an empty space to. 59 00:04:05,310 --> 00:04:07,020 ‫Indicate that a little better. 60 00:04:07,200 --> 00:04:09,210 ‫So let's do that once again. 61 00:04:09,330 --> 00:04:11,970 ‫Now it says number one, 13 plus number two. 62 00:04:11,970 --> 00:04:13,740 ‫23 is 36. 63 00:04:14,430 --> 00:04:21,390 ‫So let's use a more simple example where I'm just going to use the console right line. 64 00:04:21,390 --> 00:04:26,430 ‫You can just enter c w and press double tap, then it will give you the code automatically, which really 65 00:04:26,430 --> 00:04:26,980 ‫helps. 66 00:04:27,000 --> 00:04:35,310 ‫So now I'm going to just say no one is and then an empty space plus num one. 67 00:04:35,520 --> 00:04:43,170 ‫So what I'm doing here is I'm just saying, please use this text that I have here, plus whatever is 68 00:04:43,170 --> 00:04:44,610 ‫behind this num one. 69 00:04:45,270 --> 00:04:52,470 ‫And now even though this is an integer, this console right line method will be smart enough to understand 70 00:04:52,470 --> 00:05:00,450 ‫that it can convert this into a string, which means it can convert it into a text format as well. 71 00:05:00,930 --> 00:05:02,940 ‫Because internally this will still be a number. 72 00:05:02,940 --> 00:05:03,450 ‫Right? 73 00:05:03,450 --> 00:05:09,150 ‫But right now and we'll be smart enough to make it a text because this just displays text, right? 74 00:05:09,330 --> 00:05:15,480 ‫So if we run it, it says NUM is 13 and actually it should be NUM one is 13. 75 00:05:16,050 --> 00:05:16,350 ‫Okay. 76 00:05:16,350 --> 00:05:19,380 ‫So this will now display NUM one. 77 00:05:19,380 --> 00:05:26,880 ‫But what we're doing in this line here is we are displaying num one plus num two is sum and here we're 78 00:05:26,880 --> 00:05:33,120 ‫just giving out the value so that we know what we are actually calculating in order to know whether 79 00:05:33,150 --> 00:05:34,410 ‫what we're doing is correct. 80 00:05:36,430 --> 00:05:46,210 ‫So the concept of adding strings together and also adding numbers into strings together is called concatenation. 81 00:05:46,210 --> 00:05:51,790 ‫So here this plus is not an actual sum that we're building. 82 00:05:51,790 --> 00:05:54,580 ‫So we're not actually operating a plus here. 83 00:05:54,760 --> 00:05:56,410 ‫We're not adding something. 84 00:05:56,410 --> 00:06:01,030 ‫We are just saying this play this text and then add to this text. 85 00:06:01,030 --> 00:06:03,850 ‫This text, whatever detects behind it is. 86 00:06:03,850 --> 00:06:09,360 ‫Then add to that text that you had so far, this text over here and so forth. 87 00:06:09,670 --> 00:06:11,200 ‫So that's what's happening here. 88 00:06:12,070 --> 00:06:16,810 ‫So we're not actually calculating anything here, even though we're using a bunch of pluses. 89 00:06:17,710 --> 00:06:18,220 ‫Okay. 90 00:06:18,220 --> 00:06:20,290 ‫So that is concatenation. 91 00:06:20,320 --> 00:06:27,280 ‫Now let's look at how we can create multiple variables at once, meaning in one line of code. 92 00:06:27,310 --> 00:06:33,580 ‫Therefore, we can use this code here saying something like data type. 93 00:06:33,610 --> 00:06:36,770 ‫Then the name of the first variable comma. 94 00:06:36,910 --> 00:06:40,340 ‫Name of the second variable comma, name of the third variable. 95 00:06:40,360 --> 00:06:46,120 ‫So here we are separating them with a comma, and then we're ending this statement with a semicolon, 96 00:06:46,120 --> 00:06:46,940 ‫as always. 97 00:06:46,960 --> 00:06:51,490 ‫So this is just a quicker way to create variables. 98 00:06:51,580 --> 00:06:58,510 ‫And usually you would do that at the beginning of your, in this case, method or even class, depending 99 00:06:58,510 --> 00:06:59,850 ‫on where you want to use it. 100 00:06:59,860 --> 00:07:02,860 ‫And we're going to look into when to use what later on. 101 00:07:03,010 --> 00:07:09,070 ‫So here I'm putting it at the top, which will allow me to make sure that I have all of my variables 102 00:07:09,070 --> 00:07:12,150 ‫at one spot, at least the declaration of the variables at one spot. 103 00:07:12,160 --> 00:07:15,580 ‫I can, of course, then later on use them and overwrite them. 104 00:07:15,760 --> 00:07:24,010 ‫So for example here when I created this variable NUM two and I set it up as 23, it had the value of 105 00:07:24,010 --> 00:07:30,820 ‫23, of course, but now I can override it by using them two, and I could assign a value of 100 to 106 00:07:30,820 --> 00:07:31,780 ‫it, for example. 107 00:07:32,590 --> 00:07:35,350 ‫Now let's run this code and see what's going to happen. 108 00:07:35,860 --> 00:07:41,860 ‫So here it says, number one, 13 plus number two, which is 100, is going to be 36. 109 00:07:42,010 --> 00:07:43,030 ‫But that's not correct. 110 00:07:43,030 --> 00:07:45,850 ‫Right, because 13,100 is not 36. 111 00:07:46,120 --> 00:07:49,480 ‫That is because we did the actual calculation here. 112 00:07:49,510 --> 00:07:54,370 ‫Then we change the value of number two and then we used it in this line here. 113 00:07:54,730 --> 00:07:58,690 ‫So you have to be super careful with this approach when you're using it. 114 00:07:58,690 --> 00:08:04,600 ‫So let's now actually cut this line out here and use it down there. 115 00:08:05,620 --> 00:08:11,650 ‫So now it will give us the correct value, which should be 113 here as the result. 116 00:08:11,650 --> 00:08:16,090 ‫And you can see 13 plus two is 130. 117 00:08:18,820 --> 00:08:21,400 ‫Okay, now let's do the same thing with doubles. 118 00:08:21,400 --> 00:08:24,970 ‫So I said we would also use doubles, which is a different data type. 119 00:08:24,970 --> 00:08:30,460 ‫So if you want to use doubles, you just need to use the double keyword and then you give it a name. 120 00:08:30,460 --> 00:08:36,840 ‫So I'm going to call this double one and it will be the value of 3.4 or 1415. 121 00:08:36,850 --> 00:08:42,940 ‫I could of course have also called this one PI and then I'm going to have another double, which is 122 00:08:42,940 --> 00:08:45,700 ‫going to be something like 5.1 or something like that. 123 00:08:45,850 --> 00:08:47,350 ‫Nothing too complicated. 124 00:08:47,620 --> 00:08:49,450 ‫And now I'm going to divide. 125 00:08:49,450 --> 00:08:56,020 ‫All right, so let's use this new variable that I'm going to call def double dividend. 126 00:08:56,410 --> 00:09:03,910 ‫So it's going to use D one and divided by D two and it's going to store it in this variable here. 127 00:09:04,060 --> 00:09:13,210 ‫So I'm just going to print out the result into my console saying DX one divided by DX two is. 128 00:09:14,390 --> 00:09:18,320 ‫And then I'm going to use the div like so. 129 00:09:18,590 --> 00:09:23,390 ‫So now let's see what 3.14 divided by 5.1 is going to be. 130 00:09:23,750 --> 00:09:26,900 ‫And we can see it's 0.615 and so forth. 131 00:09:26,900 --> 00:09:28,820 ‫So it's a very long number here. 132 00:09:28,820 --> 00:09:32,760 ‫So you see this double can hold a very long number, which is great. 133 00:09:32,780 --> 00:09:35,690 ‫Now let's look at the same example with floats. 134 00:09:35,930 --> 00:09:40,820 ‫So here I'm going to use float and I'm going to use the same variable value. 135 00:09:40,820 --> 00:09:44,180 ‫So also 3.1415, whatever. 136 00:09:44,180 --> 00:09:51,170 ‫And then I'm going to use this float F two in order to also hold the value of 5.1. 137 00:09:51,470 --> 00:09:58,160 ‫So now I'm running into an issue here because I'm trying to assign a value here, but it doesn't accept 138 00:09:58,160 --> 00:09:58,250 ‫it. 139 00:09:58,250 --> 00:10:07,070 ‫I'm getting an error and that is because there's 3.1415 by default is considered to be a double by our 140 00:10:07,070 --> 00:10:07,790 ‫program. 141 00:10:07,790 --> 00:10:14,060 ‫So things that we are trying to assign a double value to a floating point variable, which doesn't work. 142 00:10:14,180 --> 00:10:21,260 ‫So what we can do now is we can add an F here and this says, okay, this is a floating point number. 143 00:10:21,530 --> 00:10:23,930 ‫So now we can add the F here as well. 144 00:10:23,930 --> 00:10:30,090 ‫And now we're specifically stating, okay, this 5.1 is not a double, but it's a floating point value. 145 00:10:30,110 --> 00:10:35,840 ‫This was just internally decided at one point when the programming language was developed, and that's 146 00:10:35,840 --> 00:10:36,920 ‫the end result that we have. 147 00:10:36,920 --> 00:10:41,570 ‫So we need to add the F if we are working with floats, because otherwise it will think that we're working 148 00:10:41,570 --> 00:10:43,910 ‫with it double like we had seen here. 149 00:10:44,500 --> 00:10:47,530 ‫Okay, now let's do this division as well. 150 00:10:47,540 --> 00:10:52,010 ‫F div is going to be f one divided by f two. 151 00:10:53,410 --> 00:10:56,830 ‫And now let's also write this onto our console. 152 00:10:57,250 --> 00:11:03,670 ‫So here F one divided by F two is going to be f div and let's run it. 153 00:11:04,240 --> 00:11:08,920 ‫And we can see that the result is rounded here. 154 00:11:08,920 --> 00:11:13,360 ‫At this point, it's around the two of four and it is not as precise. 155 00:11:13,360 --> 00:11:17,440 ‫So floating point values aren't as precise as doubles. 156 00:11:17,440 --> 00:11:20,890 ‫So if you need a high precision, then you need to use doubles. 157 00:11:20,920 --> 00:11:27,690 ‫If you need to a low precision, then a float is the better choice because it will require less memory. 158 00:11:27,700 --> 00:11:32,110 ‫This is very, very important when writing efficient software. 159 00:11:33,690 --> 00:11:39,360 ‫So make sure to only use the type of variable that will hold exactly what you are intending to do with 160 00:11:39,360 --> 00:11:39,930 ‫the variable. 161 00:11:39,930 --> 00:11:43,470 ‫So if you only need whole numbers, then just use integers. 162 00:11:43,470 --> 00:11:47,010 ‫If you need super long numbers, then use longs. 163 00:11:47,010 --> 00:11:55,350 ‫So something like along my long num and then you can assign a long number to it. 164 00:11:55,770 --> 00:11:57,750 ‫So this is even too long? 165 00:11:57,750 --> 00:11:58,580 ‫For a long. 166 00:11:58,590 --> 00:12:00,870 ‫But something like this will be good enough. 167 00:12:01,110 --> 00:12:08,490 ‫So for integers, for example, my long int it wouldn't be able to hold this long number. 168 00:12:09,090 --> 00:12:11,340 ‫So you see here it doesn't accept it. 169 00:12:11,340 --> 00:12:18,330 ‫It says represents a 64 bit signed integer but cannot implicitly convert type long into int. 170 00:12:18,330 --> 00:12:24,360 ‫So now because the number is so long, it thinks it is long, but we're trying to assign it to an int, 171 00:12:24,360 --> 00:12:25,740 ‫so that's why it doesn't work. 172 00:12:25,980 --> 00:12:30,900 ‫So that's something that you have to be aware of when creating variables. 173 00:12:32,670 --> 00:12:38,760 ‫So not something that you need to take into consideration also is that you have to be careful when you're 174 00:12:38,760 --> 00:12:40,320 ‫working with different data types. 175 00:12:40,320 --> 00:12:47,520 ‫So let's say we want to divide, let's create a new variable here called double. 176 00:12:48,310 --> 00:12:56,750 ‫And I'm going to call this 1di div, which will use a double and an integer to divide. 177 00:12:56,770 --> 00:13:00,250 ‫So let's divide D one by num one. 178 00:13:01,510 --> 00:13:03,310 ‫Let's see what's going to happen here. 179 00:13:03,700 --> 00:13:06,970 ‫So let's write that onto the console also. 180 00:13:07,510 --> 00:13:10,540 ‫So here this will be the one divided by num. 181 00:13:10,540 --> 00:13:14,470 ‫One is d i div. 182 00:13:15,260 --> 00:13:15,620 ‫Okay. 183 00:13:15,620 --> 00:13:16,160 ‫So. 184 00:13:16,880 --> 00:13:25,610 ‫You see here that now, even though I divided and double by an integer, I still got a double here because 185 00:13:25,610 --> 00:13:27,320 ‫that's what I stored here. 186 00:13:27,620 --> 00:13:29,540 ‫So this D diff is a double. 187 00:13:29,720 --> 00:13:33,110 ‫Now let's do the same thing, but this time with an integer. 188 00:13:33,830 --> 00:13:40,400 ‫So I'm going to call this one d div int and it will in fact hold an integer. 189 00:13:41,060 --> 00:13:45,320 ‫So now I'm dividing a double by an integer and you see I get an error. 190 00:13:45,350 --> 00:13:49,890 ‫Cannot implicitly convert type double to int an explicit conversion exists. 191 00:13:49,910 --> 00:13:51,230 ‫Are you missing a cast? 192 00:13:51,230 --> 00:13:58,280 ‫So we could now cast and explicitly convert it, but that is something that we are going to see later 193 00:13:58,280 --> 00:13:58,700 ‫on. 194 00:13:59,420 --> 00:14:05,240 ‫So be aware that you have to be careful with the data types that you are using and this is something 195 00:14:05,240 --> 00:14:07,940 ‫that you will over time get from experience. 196 00:14:07,970 --> 00:14:09,830 ‫Now, here, this is no problem, right? 197 00:14:09,830 --> 00:14:14,360 ‫Because we are in full control of the data that we are getting. 198 00:14:14,420 --> 00:14:20,390 ‫But it's going to be a different story if you are suddenly getting the data from an external source 199 00:14:20,480 --> 00:14:26,060 ‫where you get the data from a database, for example, from the cloud, and then you wouldn't have full 200 00:14:26,060 --> 00:14:27,930 ‫awareness of every single variable. 201 00:14:27,950 --> 00:14:34,610 ‫You would just get the data and you want to have it in a certain format, but you can not always be 202 00:14:34,610 --> 00:14:37,400 ‫100% certain that you get it in the format you want. 203 00:14:37,400 --> 00:14:41,420 ‫So that's something that you will learn once you will become more experienced. 204 00:14:41,420 --> 00:14:46,520 ‫And we are going to look at data basis in a later chapter as well. 205 00:14:46,640 --> 00:14:47,000 ‫All right. 206 00:14:47,000 --> 00:14:48,230 ‫That's it for this video. 207 00:14:48,230 --> 00:14:53,900 ‫So now you should have a better idea of how to use variables and data types specifically int, float 208 00:14:53,900 --> 00:14:54,530 ‫and double. 209 00:14:54,770 --> 00:14:56,000 ‫See you in the next video.