‫All right.
‫Now that you have seen the theory behind variables and data types, let's actually use them in practice.
‫And we're going to start with integers, doubles as well as floats.
‫So let's get started.
‫First of all, in order to declare a variable, we just use the data type and then we give the variable
‫a name.
‫And of course we need to add the semicolon.
‫So you see here I'm declaring a variable even though I'm not giving it a value, even though I'm not
‫initializing the variable, which means I'm not assigning a value to it.
‫So if I now assign a value to it, I can do that after the fact, for example, in the next line.
‫So here I can assign a value to a variable just by using the variable name.
‫And now I'm saying at this position I want to store the value of 13.
‫So here we declared the variable and now we are assigning a value or we are initializing that variable.
‫Okay, now let's actually write this variable onto our console so we can write the variable using console
‫right line.
‫And then in brackets we just use num one here and it will then display num one for us.
‫So depending on the settings that you have, you will potentially also need to use console to read in
‫order to keep your console open and running when testing this.
‫So let's test this real quick and we can see it says 13.
‫So here this 13 up there.
‫Okay, so that works.
‫Now we can display a value.
‫So let's display something more interesting.
‫Let's actually display the sum of two variables.
‫So now we have NUM one which currently is 13.
‫Now let's create another variable called NUM two.
‫So I'm going to declare and initialize the value in the same line.
‫So here we are declaring and initializing the variable in one go we're saying data type integer name
‫num two is going to be 23.
‫So now internally for this variable name, numb to it will store the value of 23.
‫So what we can do now, of course, is apply mathematical operations on it.
‫So let's just create a new variable called sum and this will now be the sum of NUM one and num two.
‫And by the way, once I do that, you can see that the underline goes away.
‫So here, this one here where the number two is never used, you see this green underlining now it goes
‫away because this green underlining just says that this variable is never used.
‫So now we are actually using it in this line here.
‫Okay, so now we have the sum, let's actually print out the sum of 13 and 23 in this bright line statement
‫here.
‫Okay, let's do that.
‫Let's run our program.
‫And it says 36 because 23 plus 13 or 13 plus 23 is going to be 36.
‫All right.
‫That's great.
‫But now I would like to actually see what I am doing there.
‫Therefore, I can use this concept of concatenation.
‫Let me show you real quick.
‫So here, concatenation where I'm using.
‫The number one.
‫I'm saying number one on the console plus.
‫Which means it's going to display num one with the actual value of none one.
‫So this variable here then I'm saying in the text the empty space plus empty space numb to.
‫So now I'm using the value of num two here plus num two.
‫Is some.
‫So this looks a little complicated.
‫Let's break it down in a second, but let's first run it.
‫So you see, number one is 13 plus.
‫Number two is 23 is 36.
‫So here I would need an empty space to.
‫Indicate that a little better.
‫So let's do that once again.
‫Now it says number one, 13 plus number two.
‫23 is 36.
‫So let's use a more simple example where I'm just going to use the console right line.
‫You can just enter c w and press double tap, then it will give you the code automatically, which really
‫helps.
‫So now I'm going to just say no one is and then an empty space plus num one.
‫So what I'm doing here is I'm just saying, please use this text that I have here, plus whatever is
‫behind this num one.
‫And now even though this is an integer, this console right line method will be smart enough to understand
‫that it can convert this into a string, which means it can convert it into a text format as well.
‫Because internally this will still be a number.
‫Right?
‫But right now and we'll be smart enough to make it a text because this just displays text, right?
‫So if we run it, it says NUM is 13 and actually it should be NUM one is 13.
‫Okay.
‫So this will now display NUM one.
‫But what we're doing in this line here is we are displaying num one plus num two is sum and here we're
‫just giving out the value so that we know what we are actually calculating in order to know whether
‫what we're doing is correct.
‫So the concept of adding strings together and also adding numbers into strings together is called concatenation.
‫So here this plus is not an actual sum that we're building.
‫So we're not actually operating a plus here.
‫We're not adding something.
‫We are just saying this play this text and then add to this text.
‫This text, whatever detects behind it is.
‫Then add to that text that you had so far, this text over here and so forth.
‫So that's what's happening here.
‫So we're not actually calculating anything here, even though we're using a bunch of pluses.
‫Okay.
‫So that is concatenation.
‫Now let's look at how we can create multiple variables at once, meaning in one line of code.
‫Therefore, we can use this code here saying something like data type.
‫Then the name of the first variable comma.
‫Name of the second variable comma, name of the third variable.
‫So here we are separating them with a comma, and then we're ending this statement with a semicolon,
‫as always.
‫So this is just a quicker way to create variables.
‫And usually you would do that at the beginning of your, in this case, method or even class, depending
‫on where you want to use it.
‫And we're going to look into when to use what later on.
‫So here I'm putting it at the top, which will allow me to make sure that I have all of my variables
‫at one spot, at least the declaration of the variables at one spot.
‫I can, of course, then later on use them and overwrite them.
‫So for example here when I created this variable NUM two and I set it up as 23, it had the value of
‫23, of course, but now I can override it by using them two, and I could assign a value of 100 to
‫it, for example.
‫Now let's run this code and see what's going to happen.
‫So here it says, number one, 13 plus number two, which is 100, is going to be 36.
‫But that's not correct.
‫Right, because 13,100 is not 36.
‫That is because we did the actual calculation here.
‫Then we change the value of number two and then we used it in this line here.
‫So you have to be super careful with this approach when you're using it.
‫So let's now actually cut this line out here and use it down there.
‫So now it will give us the correct value, which should be 113 here as the result.
‫And you can see 13 plus two is 130.
‫Okay, now let's do the same thing with doubles.
‫So I said we would also use doubles, which is a different data type.
‫So if you want to use doubles, you just need to use the double keyword and then you give it a name.
‫So I'm going to call this double one and it will be the value of 3.4 or 1415.
‫I could of course have also called this one PI and then I'm going to have another double, which is
‫going to be something like 5.1 or something like that.
‫Nothing too complicated.
‫And now I'm going to divide.
‫All right, so let's use this new variable that I'm going to call def double dividend.
‫So it's going to use D one and divided by D two and it's going to store it in this variable here.
‫So I'm just going to print out the result into my console saying DX one divided by DX two is.
‫And then I'm going to use the div like so.
‫So now let's see what 3.14 divided by 5.1 is going to be.
‫And we can see it's 0.615 and so forth.
‫So it's a very long number here.
‫So you see this double can hold a very long number, which is great.
‫Now let's look at the same example with floats.
‫So here I'm going to use float and I'm going to use the same variable value.
‫So also 3.1415, whatever.
‫And then I'm going to use this float F two in order to also hold the value of 5.1.
‫So now I'm running into an issue here because I'm trying to assign a value here, but it doesn't accept
‫it.
‫I'm getting an error and that is because there's 3.1415 by default is considered to be a double by our
‫program.
‫So things that we are trying to assign a double value to a floating point variable, which doesn't work.
‫So what we can do now is we can add an F here and this says, okay, this is a floating point number.
‫So now we can add the F here as well.
‫And now we're specifically stating, okay, this 5.1 is not a double, but it's a floating point value.
‫This was just internally decided at one point when the programming language was developed, and that's
‫the end result that we have.
‫So we need to add the F if we are working with floats, because otherwise it will think that we're working
‫with it double like we had seen here.
‫Okay, now let's do this division as well.
‫F div is going to be f one divided by f two.
‫And now let's also write this onto our console.
‫So here F one divided by F two is going to be f div and let's run it.
‫And we can see that the result is rounded here.
‫At this point, it's around the two of four and it is not as precise.
‫So floating point values aren't as precise as doubles.
‫So if you need a high precision, then you need to use doubles.
‫If you need to a low precision, then a float is the better choice because it will require less memory.
‫This is very, very important when writing efficient software.
‫So make sure to only use the type of variable that will hold exactly what you are intending to do with
‫the variable.
‫So if you only need whole numbers, then just use integers.
‫If you need super long numbers, then use longs.
‫So something like along my long num and then you can assign a long number to it.
‫So this is even too long?
‫For a long.
‫But something like this will be good enough.
‫So for integers, for example, my long int it wouldn't be able to hold this long number.
‫So you see here it doesn't accept it.
‫It says represents a 64 bit signed integer but cannot implicitly convert type long into int.
‫So now because the number is so long, it thinks it is long, but we're trying to assign it to an int,
‫so that's why it doesn't work.
‫So that's something that you have to be aware of when creating variables.
‫So not something that you need to take into consideration also is that you have to be careful when you're
‫working with different data types.
‫So let's say we want to divide, let's create a new variable here called double.
‫And I'm going to call this 1di div, which will use a double and an integer to divide.
‫So let's divide D one by num one.
‫Let's see what's going to happen here.
‫So let's write that onto the console also.
‫So here this will be the one divided by num.
‫One is d i div.
‫Okay.
‫So.
‫You see here that now, even though I divided and double by an integer, I still got a double here because
‫that's what I stored here.
‫So this D diff is a double.
‫Now let's do the same thing, but this time with an integer.
‫So I'm going to call this one d div int and it will in fact hold an integer.
‫So now I'm dividing a double by an integer and you see I get an error.
‫Cannot implicitly convert type double to int an explicit conversion exists.
‫Are you missing a cast?
‫So we could now cast and explicitly convert it, but that is something that we are going to see later
‫on.
‫So be aware that you have to be careful with the data types that you are using and this is something
‫that you will over time get from experience.
‫Now, here, this is no problem, right?
‫Because we are in full control of the data that we are getting.
‫But it's going to be a different story if you are suddenly getting the data from an external source
‫where you get the data from a database, for example, from the cloud, and then you wouldn't have full
‫awareness of every single variable.
‫You would just get the data and you want to have it in a certain format, but you can not always be
‫100% certain that you get it in the format you want.
‫So that's something that you will learn once you will become more experienced.
‫And we are going to look at data basis in a later chapter as well.
‫All right.
‫That's it for this video.
‫So now you should have a better idea of how to use variables and data types specifically int, float
‫and double.
‫See you in the next video.