 And welcome to Intro to Java and AP Computer Science A, which is your edtech that is me. In this video, we'll be looking at primitive types and math, basically what we're looking at today is numbers. So here are the coding concepts we'd like to cover here. First is data types. Data has a type, which I'll explain here in a little bit. In the previous lesson, we learned how to print Hello World. So now we're going to learn how to print with variables, which are represent numbers or stored numbers when you want to look at it. And then we'll also talk a little bit about the variable naming rules, because there are rules. We'll talk about how to do math on various numbers. We can add some numbers, we can subtract some numbers, et cetera. Another thing you'll find very common in coding is where we change the value of a number. So we add to it, or we subtract from it. It could be one, it could be two, it could be more, depending on the problem we're trying to solve. And then we also have something called the math class, which has some built-in methods that we can use to do some math here later. Let's go ahead and get started. So just to review from last time, as you may recall, hopefully, we need to create a class, and we need to give it a name. And the name is capitalized. The first letter is capitalized. Internal words are capitalized. And this name must match exactly the file name. So primitive types and math.java. So the class is primitive types and math. And if we're going to execute our program, we need a main method. So just to memorize that, it's public static void main. String, square brackets, args. And we have our curly braces. So this is our standard Java program style. So before we get started, so let's take a look at the different data types. Now I have this website here, W3Schools. I highly recommend it. Check it out. It's got a lot of great information. And you can see over here, it's got all kinds of stuff about Java. It's got Java, it's got basically everything. This is one of the best websites, I think, for learners to use. At least once you have a little bit of knowledge to see, actually, up here, all the things that you need. So you'll see here, data has a type. And today, we're looking basically at numbers. So you can see the data type called byte. Each one has a size. And this is the amount of memory that it takes up. So obviously, smaller the memory, the fewer values it can hold for lack of a better term. So you can see it ranges here from one byte, two bytes, four bytes, eight bytes, four bytes and eight bytes, one bit and two bytes depending on the type. You can see over here, so a byte is one byte. Go figure. And it can store whole numbers, so not fractions, so not decimals, from negative 128 to 127. Let's go ahead and see what that would look like in coding. So I'm gonna go ahead and say byte. And I'm gonna say age. That's definitely under 127 in my case, likely, I guess. And I'm gonna go ahead and compile that. And it compiled successfully. Now, if I run this, of course, nothing happens because we didn't print. But this is the pattern you need to understand. So it is the type, and you'll see this over and over and over again, is the variable name and equals, and then whatever the value is that you want to give. So this type is bytes, variable name is age equals, and the value is 51. Now, if you recall from a few seconds ago, the maximum value is 127. So let's see what happens if we try to make it 128, which is too big. Pilot, and you see here, we've got a error, incompatible types, possible lossy conversion from int to byte, and it's pointing at 128. So it's telling us that there is a loss of information here. The reason it says something about int is that by default, it tries to reconcile this or interpret this as a byte. Let's put that back down to the, or sorry, as an int. Let's put that back down, and let's go ahead and print this. So system.out.println, we'll go ahead and print h. We'll go ahead and compile that. Compile successfully, we're gonna run it, and see here is the 50. To view. So this shows us that we can print whatever values that we want to print with our system.out.println statement. Now, what I can also do is I can add some text here. I can say, hi, my age is, quote, plus age. Let's see what that looks like. So it's kind of weird here because we've got a plus, but we're not adding numbers, we're adding text and a number. So when we're using println, printline, println to mean how you wanna say it, it will add, combine those together as text. You can see here, hi, my age is 51. Now I'm like a little bit strict about this sort of thing. I don't like that because it's a sentence and sentences should have periods at the end or whatever makes me points. So you can see here how I've added plus age, plus period, and if I run that you can see my age is 50. Couple things, just to show you here, the spaces here don't matter. I just put the extra spaces in because it makes it a little bit easier to read with that as perfectly acceptable. However, the spaces inside the quotation marks do matter that there's no space here. Okay, run that, you'll see that there's no space here. I'm sure that that looks nice. So anyway, so byte, let me go back to this. So we have byte, short, int, and long. These are our whole number values. So you can see that each of them has a slightly different range. Now if you're on, if you're doing AB Computer Science you only really need to use int. They don't use bytes, shorts, or longs. And now there is one thing you gotta keep in mind. So I'm gonna go ahead and try and make a long. So I'm gonna say long, I'm gonna say num of atoms. Let's go ahead and do one, two, three. See what happens here. See if I can compile that. That one compiled. Let's keep going, one, two, three, four, five, six, seven, nine. See how big a number we can do here. You can see here, it says integer number is too large. So let's see that's, that's hundreds, thousands, millions, billions, trillions, quadrillions. And hundreds, thousands, billions, billions, trillions, quadrillions, quintillions. So you can see how it's kind of weird. This goes up to nine quintillion, but we don't have, we have less than that. We have quadrillions. So kind of a weird thing. So what's going on here? This is one of those things that I mentioned earlier. It interprets the, this as an int for some weird reason by default. So what we can do is we're gonna actually make a long. We add an L to the end. And that tells the compiler that, hey, this is gonna be a long and we wanna make sure it's a long. So if you're going above the size of an int, you'll need to do that. It's a little thing. Again, the same thing here. We can go ahead and print out. We do system.out.println, say num of atoms. You can see how that number is printed out as we expect it. So going back, we also see now that there are two types here, float and double, that store fractional numbers. These are numbers with decimal points. Let's go ahead and do a couple of examples of that. Let's say, float, I don't know, say 123.456, and let's put length correctly. H, I can do system.out.println, incompatible type, it's possible lossy conversion from double to float. So you can see how it's thinking it is a double. I'm gonna do a print F here. Not sure. And that, yeah. So same thing with floats. So with longs and floats, you gotta be really careful. You gotta tell it it's a float, or you gotta tell it's a long, sorry about that. But again, if you're doing AP Computer Science, you're gonna be using ints and doubles anyway. So I'm gonna say double, and we'll say, and I usually use gigawatts, my example, 1.21, say system.out.println, float, plus gigawatts, plus float. So again, you have to tell the computer what data type you're using. And if you're coming from Python, or if you're coming from, say, JavaScript, you don't necessarily have to do that, but Java is what's called strongly typed language and you gotta tell it up front, what the type is. And once it's a certain type, you can't change it. That's it, it's done, it's set, or good. So those are our main data types with numbers. There's also a couple others here. There's Boolean, which I'll talk about right now, and there's a CARE type or CHAR type, which has to do with strings, which you'll see in the next unit. But let's take a look at Boolean since they are super-duper important. So Booleans, Booleans have two values. They are either true or they are false. So for example, Boolean is sleepy. That's true, so are you sleepy? Sam, system.out.print.ln is sleepy. And then compile it, run it, and you'll see fruit. So Booleans can have true value or they can be false. That's it, they'd have no other values that is all they are used for. Just looking at the naming, and you can see here how I said is sleepy. This is a common, okay, this is a common style for Booleans that has something like, has a gun, that example, but has money, true. Or false. Has permission, true or false. Okay, so you'll see that it's kind of like a question. Is sleepy or is not sleepy, there's two possible values. The other thing with naming rules, with these variables, so if we take a look at the ones you see up above, you can see that age equals, you see num of atoms. Notice there's no spaces in here. So if I went ahead and put a space in, it's a num of atoms, this is a common beginner mistake. I try to compile it, we'll get an error. So it tells you line 10, kind of points right here, it tells you where the problem is, and it just doesn't like it. So variable names cannot have spaces. The other thing with variable names is you can have gigawatts, you get gigawatts too, you can end with a number, gigawatts too, as well. That works just fine, but if you put a number at the beginning, you can't do that, you'll get an error, it will not compile. It says error, not a statement, and double to gigawatts equals 1.21, it doesn't like it. Okay, so just keep that in mind when you are coming up with variable names. So no spaces, capitalize the internal words. So it always starts lowercase, but of is capitalized, atoms is capitalized, and this cannot start. So those are some of the variable naming rules. There are a few other things that there's something called a final static value, but we'll get to that some other time, we'll see that other places. So those are the basic variable types, and those are the basic variable naming conventions. So next part, we wanna take a look at math. So let's go ahead and do, let's say int x equals five, int y equals 10, and say int z equals x plus y. And if we do system.out.printlnz, we should hopefully compile, we should hopefully see 15. Now we could have printed out like this. Have a x plus, this is kind of a weird one, plus, plus quote, plus y, quote, plus, plus z. Now the color really helps here. So we're taking the value of x, and then we're taking the value of, oh, this should not be in what they, sorry, value of y, and we're adding the plus sign, the equals sign equals z. So if we run that, see five plus 10 equals 15. So this is x here, and plus string plus, and we see y, and we see the equals sign. So be careful with that, it's a little tricky as you're trying to type. So we can also subtract, correct, minus, and run, compile it and run it. If five minus 10 equals negative five, we can multiply. It's not a times, it's not an x, it is an asterisk. So like a keyboard, it's shift eight, five times 10 equals 50, and division is this little slash, forward slash. And now this is where it gets a little bit weird. So five divided by 10, so if you're good at math, you should say to yourself, well that should be 0.5. But the problem is that these are integers. z is an integer. Integers don't have a decimal place. So in this case, what happens is the decimal is removed. So we have, so it would be 0.5, but the 0.5 is not included because it's an integer. So be really careful with that. So if you wanted to do this correctly, if you wanted to get the correct result, these would have to be doubles. So if I compile that and run it, you see 5.0 divided by 10.0 equals 0.5. And there's one more thing called a modulus, which gives us the remainder of a value. So if I compile that and run it, so this is five divided by 10. So does 10 go into five? No, it's zero times with five left over. So if I did 10 and five, piled it and ran it, it also gives us zero because five goes into 10 two times. There's nothing left over. I could change this to say six, piled it, run it. So 10% six. So six goes into 10 one time and there is four left over. That is the, that's kind of a quick, quick introduction to math. So incrementing and decrement. So if I go back to our sliding or our coding concepts, so we talked about all the different math operators, talk about naming rules. So increment and decrement. So increment means to add two and decrement means to subtract. So if let's go ahead and do, or we have x equals 10, so let's go ahead and do x plus, x equals x plus one. System.system.out. And l n x. So what this does is it adds one to x. So x was 10, now it is 11. This is a way to do it. We can also do x plus equals one. Get rid of that, compile it. Again, we get 11, x plus equals one. This is the short form of that. And there's a special one. This is, you'll see this in Java and C type languages. x plus plus, that adds one to 11. Which is pretty cool, okay. We can also do the same thing. We say x equals x minus one. This could be five, it doesn't have to be one, but we'll use one for this example. x is 10 and we subtract one, it gives us nine. We can do the short form minus equals one. And we'll get the same result. We could do, we also do like times. We could do x equals x times two. A lot of different types of things we could do here. That gives us 20. Same thing, we do x times equals two. And it gives us the same thing. And we also have decrement, which is x minus minus. And that gives us nine. Now just real quick thing, you may see this. This is not something you'll see in the AP, but you'll see it here. You'll see it in other Java places. So if I do this, the plus plus comes after the x. What do you think the result is going to be? Interesting, right? What happens here? So let's compile it and the output is 10, crazy. But if I go ahead and do this, you'll see that it's 10 here and it is 11 here. Kind of weird. So basically what's happening is it prints the x, then it increments. And so the order that it happens in is probably different to what you'd think. Now, if you wanna do what we wanted to do, which is increment first, we put the plus plus before the x. Again, AP computer science students, this doesn't come up with AP, they don't test for it, but it's something you will see. So if I run this, so it increments the 10 to 11, then prints it, and we just printed it. So yeah, so that's kind of an interesting thing that you can do with adding one subtracting. Okay, and finally, I'm gonna take a look at the math class. The math class at Java has quite a few functions or methods that we can use to do mathematical operations. So we're gonna take a look at a few of these, but there are a bunch of them. So we had x equals 10, well, let's go ahead and actually, it's kind of important, I'm gonna go ahead and comment that out, right, print those. And now just before I go ahead, I'm gonna go ahead and compile and make sure it's still running, okay? So what I'm gonna do is I'm gonna say double x equals math.pow, say 2.0, 3.0. And we'll go ahead and system.out.println. x, let's go ahead and see what that result is. That gives us 8.0, okay? There's 2.0 to the 3.0 powers, that's two times two times two is 8.0. Notice it is a double. So let's try to see if I do int here and see what happens. You can see where it says incompatible types, possible lossy conversion from double to int. Something we'll cover another time, but suffice to say, it has to be a double, okay? It's just the way it works, yeah. We also have math.square root, say double y equals math.sqrt, say 25.0 and we go system.out.println, compile that. And we know the square root of 25 is 5.0. But again, notice it is 0.0, so it is a double, it is not an int. Now there are other math functions such as sine, cosine, tangent, et cetera. So I'll just do a quick example. So double, s equals math.sin0system.out.println. File that and run it. It gives me 0.0, the sine of zero is zero. Now, if you're thinking, well, what about 90? Okay, which I think should be one. It gives us this weird number, 0.8939, blah, blah, blah. So if you're familiar with degrees versus radians, these are degrees, but you need to put radians in here. So what you can do is you do math.2radians. 90. So that converts 90 to radians. I think it's pi over two in this case. And then that becomes, that's passed on to the sine method. That should give us, I believe. Again, if you're not familiar with math and radians and angles, don't worry about it, but it is there. So we have sine, cosine, tangent. We can convert two degrees and two radians. We also have two other things you might find useful. So system, depending on what your focus is here with learning Java, we also have math.pi, which is pi. And we have system.out.println math.e, which is Euler's number. Yeah, if you don't know what those are, don't stress about it. System is spelled system wrong. System, yeah, a lot. Pilot, and you'll see 3.14159, blah, blah, blah, blah, and 2.718, blah, blah, that's the natural logarithm. Okay, so, and again, the math class has a lot more functions than that. Those are just the ones that you might run into or you might need. Especially, pow and square root are for AP students, but you might need sine and cosine depending on what you're doing. You'll probably need pi at some point, possibly Euler's number. So that is that for this video. Let me go back concepts here. So data has a type, depending on the value that you need. So typically the idea is that you want to be efficient with your memory. Now, again, modern computers have tons of memory. You don't really have to worry about it, especially as a beginner. Again, for AP students, you'll be using int, you'll be using double, and you'll be using the rest of those. But I will talk from time to time about the others in this video series. And then printing with variables, show you how to do that. So you use plus operator, talk about the variable naming rules, and then the various math operators. So additions, attraction, multiplication, division, modules, which we've never seen before. Talk about how to increment and decrement so we can use x equals x plus one, x equals x plus five, x plus equals five is a shortcut, or for incrementing by one, we do x plus plus, or plus plus x, depending on how the outputs come. And then finally, there is the math class, and you can figure out more about that online. Anyway, that is that for this lesson. Thanks for watching and I'm going to be back in a minute.