Hello there, and welcome back to the Java development course. So last time we went over working with variables, and you had some homework to do, your job was to create three variables of type integer, assign them just some values, and then print the sum of those values. So this is what you should have done. So here I have three values A, B, and C, I find them the value 1210, and 10. And then I just print the sum of those values a plus b plus c. And let's run this application. And there we go, we get 32.
Now, if we change a to 13, and must have run this application, we get 33. So it automatically changes according to what how it changes these values. So that is essentially how we can add three numbers together in Java. So if you got this that's very good. If not, don't worry. This is that like the first Java homework that you've ever done, most likely, so Don't you know, don't beat yourself up about it understand what this code does.
And if you can understand what it does that then you're good to go for the next lessons. So without further ado, then let's get into this lesson. Alright, so today we're going to be going over variable types in Java. And we actually already went over our very first date variable type, which is called integer. And we're going to go over exactly what that means in just a minute here. But first of all, let's go.
Let's go and create a new class. All right, so the class name will be variable types. There we go, as always, public static, void, main. And finish. And there we go. Now we have a variable types Java.
So now let's go ahead and delete this comment here. And actually, let's very quickly go over comments. So comments are things just something that Java will ignore. So if you go to actually write some anything here, we can write anything. And then we actually we could actually just create an multi line comment by doing this. There we go.
And right we can write anything here as well. And so this will this will just be ignored by the Java compiler. So we can now run this application. And as you can see, nothing happens since this is all commented. So even if we put, I don't know, maybe system out print ln, print ln hello world into here, nothing will happen since it is commented. So this is just a single line comments, everything we put after double slashes will be a comment.
And then it's just a multi line comment. So comments are actually pretty important since they help us you know, write code to ourselves, messages to ourselves, sort of or other developers. So if you have a very big application we can do, we can actually write a comment that's like this application, or this part of the application does something so it adds two numbers together, for example, and then later on, another developer will stumble upon what you wrote written, and he'll just look at it and he'll Be like, Oh, that's what it does. And so yeah, so comments are actually considered a very good programmer will write good comments. So that's just, you know, usually that's usually considered the truth sort of. Okay, so without further ado, then let's get into today's lesson, we're going to be going over data types.
So Java actually has seven or eight data types. I can never really remember them number I know them. Let's take a look at them. So let me go to write a muslin common here. There we go. Okay, so first of all, we have int, then we have long then before we actually have short, and then before short, yeah, bite.
Okay, and then we also have float and float and double. And don't worry, we're gonna go over exactly what they mean in a second here. And then we also have Boolean and chart as well. So I guess I guess this is eight. So it's eight after all, not, not, not not not seven. Okay, so these are the primitive data types in Java.
So primitive data types just mean that, you know, they're sort of built in, let's just let's just call it, let's just call it like that for now, you'll understand exactly what that means in a couple lessons here. But for now, just understand these are built in. So what is the difference between them? So why do we need a bunch of different data types? So first of all, let's go over these four symbols to understand and we've already gone over it. So an int is just a number so we can actually go out and do int a equals 12.
Okay. Short, essentially also a number. So we can do short, b equals 13. And then a bite, same thing. Bite is also a number. It's a whole number.
And then we're actually no we got we actually need to do 12. And then we actually need to add this B right here. B What's the problem? Oh, right. I didn't put a name to the bite. What a Dumbo.
Okay, there we go. Bite k equals 12. And then I was thinking, What's the problem? What's the problem there? I didn't print it. Okay, bite shorten and then long.
So all of these all of these are just numbers, whole numbers. So why in the heck do we need four different types of whole numbers? Well, I'll tell you why, because of memory. So these four numbers have both at all of them can hold different amounts of numbers. So different maximum number so we can actually print System dot out dot print ln. Print ln, byte dot max value.
Let's actually just do the same thing for all four of them. So byte short, long. No, it's gonna be integer, integer and long. So Elisa does, you're gonna put the maximum value that all of these data types can hold. Let's go and take a look. So there we go.
So a byte can hold 127 values. So what does that mean? So that means at the maximum, it could hold at 127. If we do 128, here, we get an error. So 127 is the maximum number of bytes can hold, the maximum number of shorts can hold is 32,767. There we go.
The maximum number an integer can hold is 2 billion as we went over it already. And the maximum number along can hold is actually this right here. Or no, for type int, is that a range? Okay, so we'll go over exactly why this why we're getting this error in a second here. So this is just the different amounts that a different data types can hold. So a byte can hold a maximum of 127 shorts can hold them Some 32,207, a integer can hold a maximum of 2 billion and so on, and so on could hold a maximum of this number.
I don't really know how to describe it. But yeah, so just this number. So that's essentially how we work with different variable types and how that's how different variable types work and why we have different variable types. Now, I'm sure you're wondering, why in the heck Can't we just have one variable type that has the maximum so just long, so why do we need all these? Well, in short, is because memory, so in memory, essentially, is just the thing that holds the numbers themselves. And in memory, the higher a number is, the higher it sort of, the higher it can hold.
So if we have let's just say, I don't know, maybe 80 slots of memory, it can hold a certain numbers, let's say like a billion, it's a lot higher, but let's just say a billion and so on. If we put the number two into the ad slots, then what's the point of you know, having the ad slots, we don't need them when we need to. So as a result, we have this these different data types. So it doesn't really work that way that we can, you know, maybe assign 40 data, data 40 memory slots, and then just, you know, get take more as needed, it doesn't really work that way, we would then have to delete the 40 and then copy that number into a completely different memory slots. So that's just sort of how it works. When result we have these four data types, they can hold different amounts of memory.
So byte can hold a byte short can hold, I think two bytes into can hold four bytes, and then long could hold eight bytes, I think. So that is essentially how it how it works. Now, why are we getting this error here? Well, because we actually need to cast this down to one Right now it's actually trying to say that this right here is an int, integer and not a long. So in order to make it a long, we just have to do l at the end here. And it'll understand that this is actually a long and not an integer.
So if you ever get an error like that, just put an L at the end of it. And so that's essentially how we work with numeric data types. Those are the number data types integer data types at least. Okay, so what if we need to hold decimal points? So numbers with decimal points, what do we do then? Well, for that we need this float and double as well.
So a float and we'll both hold just decimal points, so number of decimal points so we can have float, G equals 3.4. All right, there we go. Of course we get an error since it's actually trying to create a double so when we create a number with a decimal point in Java, it will automatically think that it is a double so Need to just put an F at the end? And that will just tell it you know, this is not a double, this is a float. And then same thing with double, so Double D equals 4.5 4.5. Okay, so I'm sure wondering at this point, why do we need to separate two separate types for decimal points?
Well, for the same reason that we need to separate types, for separate types for numbers, because of memory. So a float can hold a maximum of a certain number of values. And then a double can also hold a maximum of a certain number of values. So we're going to get the maximum exponent so system, dot out dot print ln float dot max exponent. So what does the max exponent mean? Well, that just means that it's the maximum number of values we can have on both sides.
So in this case, we have two values on both sides. And if we add two more, three more now we have five values 12345 Five values, that's just the maximum number of exponent it can hold. So we can actually do float dot max exponent. And then we can do the same thing for double as well. double dot max exponent, let's run this. And as you can see, so the maximum exponent for float is 127.
So that's the maximum numbers, it can hold. amount of numbers you can hold. And then the maximum exponent for double is 1002 23. So that is essentially how we work with different data types. So we'll actually go over Boolean and char in the next lesson. Since in this lesson is about it's best to just keep it with numbers, right?
So we're going to go over one last thing about how we can add different numbers together. So what we can actually do is we could do float. Let's just say sum equals 4.5 plus 656 plus, let's say G. Okay, and so right now we're going to get an error. So Type mismatch cannot convert from double to float. So what has happened? Well, you know, here how we, when we do this, it automatically has, you know, tells you that this is a double and not a float.
Well, that's because that's just the way Java works. So whenever we create a number with a decimal, it automatically created as a double. And so that's Central, but to do it what it's doing here. So for that, we actually just need a casting as well. But we can actually just do F at the end here, because that just just won't really work. In this case, what we actually need to do is we just need to do, actually, Eclipse has a very handy here add cast to float right here.
So there's this we'll just solve our problem. And there we go. So we just added the cast of float. So it's going to add this together, and then whatever result it gets, it's just going to change it to float. And so now we can actually print the sum. System dot out dot print ln sum.
All right, there we go. And let's run this There we go. And so it prints a sum as 63.959656. And there we go. Okay, so that's essentially how we can work with different types of numeric data types in Java. So, without further ado, then let's assign you some homework.
Okay, so for homework, your job is to create a variable of every single type. So of int, short of a single numeric type, I mean, so int, short, byte, long, float and double. And then finally, create one last double, which will contain the sum of all those variable types. And then finally, just print that sum of all those variable types. So just assign some random numbers to it. It doesn't actually have to be anything.
So just like the last lesson pretty much except with that different variable types. So without further ado then I wish you luck and I'll see you next time soon.