 Hey everybody, this is Brian. Today we'll be talking about casting variables, narrowing and widening. Okay, what is casting a variable? No, it's not like Dungeons and Dragons casting a magic spell or something. What it is is you convert a value from one thing to another. So we'll say we have our integer of zero and we will make a floating value float. And we will say this is 4.125. Now, if you're wondering what that little f at the end, that just means we're denoting that as a float. So if we take that out, it doesn't like it. It says drumroll cannot convert that. So we have to change either to a double or we have to add a cast of float. That's also legal, but it's just easier to use the shorthand and just put a lowercase f behind it. So we have two primitive types. Now how do we know they're primitive? Remember we talked about wrapper classes with boxing and unboxing. How do we know these are primitives? Well, they're lowercase. If you wanted to do a wrapper class, you would do the uppercase. That's a wrapper class. That's a primitive. So let's just stick with primitives to keep this simple. Now let's say we want to convert this floating point into an integer. Now, what's the difference between a floating point and an integer first off? Well, an integer doesn't have a decimal point. A floating does. So anything past this decimal point is not an integer. That is an integer. That is not. So to convert this value, what we're going to need to do is call it a cast. And the syntax for this is really simple. We're going to say m int equals and then in parentheses type the type that you want to cast to, in this case an integer, and then the value you're casting from. And let's review. You have two primitive types, an integer and a float. This part of the float is an integer. This part is not. So when we cast, we're saying take m int is going to equal the value of, and we're casting to an int. m float. So basically let's read this in reverse. So it's easier to understand. m float is being converted to an int and assigned to the value m integer. I should say the reference m integer. Now let's do a system.out.printline. I still can't type today. This is the third video and I still can't type. So bear with me. m int. Now when we run this, what do you think is going to happen? We're spitting out m int, this guy. And you see we've assigned it the value of m float, but we cast it to an integer. Let's find out. There's the four. Where'd the rest of it go? It should be 4.125. No, you shouldn't. Because remember, an integer does not have a decimal point. That's called narrowing. The problem with casting from one type to another is you have narrowing and widening. What you've just seen is narrowing, meaning we are taking this float, casting it to an integer, meaning we're stripping off this extra information. Now widening is the exact opposite. Widening would be when we say m float equal and we're going to cast this. That's called a widening conversion. So let's actually just narrowing, widening. Space these out a little bit so they're not so confusing. So this is a narrowing conversion. We're going from a bigger number to a smaller number. And this is a widening conversion. We're going from a smaller number to a bigger number. So let's just spit out m float and see what's going to happen. Now if you start at the top here you see we've got two variables, m int, m float. One's an integer, one's a floating. We're going to take our floating value, convert it to an integer, assign it to the integer reference. That's a narrowing conversion, meaning it's getting smaller. And then as a cruel twist of fate, we're going to turn right around and widen it. So we're going to say our integer. So at this point it's 4. It's going to be widened to a floating point and assigned to the float reference. When we spit this out, it's probably still going to say 4. Do you believe me? Let's find out. 4.0. You see how it added that extra zero in there because the floating point always has a decimal place. That in a nutshell is casting. Another handy little trick I can show you real quick here is the two string method. And for that what you really need to do is create a wrapper class. So we're just going to say int, integer, m test, we'll just call test equal 2-3-4. Now let's say we want the string representation of this. Well with wrapper classes you can do dot two string and as you can see it converts it to string. You can also do two binary, two hex, two octal. You can do two string with a certain formatting. There's many many things you can do. So let's just kind of test this. Do our handy copy and paste. And when we run it you see there's 234. We have our 4.0 and our 234. So if none of that was apparently clear to you you should probably rewind the video and rewatch it. This is not an easy topic so don't beat yourself up. What you just need to know is that we are converting. You're converting from a float to an integer, from an integer to the float, and then you have narrowing and widening. You really need to understand the difference between those. Well this is Brian. I'm running out of time so I thank you for watching and I hope you found this educational and entertaining.