Primitive Datatypes In Java Made Simple | Int, Long, Float, and Double - Java Tutorial Series Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone my name is dean and welcome back to another java tutorial it still sounds wrong to say but here we are i'm just going to run through real quick what we're covering today and then we can jump into the code so we're going to start off by covering the primitive data types in java so this is going to be your integers your characters or your your booleans your conditions so like true or false um does your can your character use the scale at level 21 if it's true then yes they can use it if it's false then no they can't i will also cover the uh you know some of the bigger data types so let's say you need some really big numbers we'll cover those uh we'll cover how to get the biggest value a number can be or the smallest value we'll use some smaller third-party libraries so we're going to be using the math library in this this is just to sort of get you comfortable with using some of the tools that java gives you to make your life easier so you're not constantly looking stuff up uh then after we cover that i believe the next thing i cover is the basic operations so your pemdas your order of operations so parentheses exponents multiplication division addition subtraction uh we'll sort of cover how to do each of those to some extent then we'll finally cap it out with how you get like i don't know the absolute value of a number so we'll do that with the math library and we'll cover how to do the power of a number so 2 to the power n so like let's say 2 to the power 3 is 8. uh so we'll cover all that i'll show you where to go to learn more about this like where you go if you're coding on your own and you're like oh i need to use a math function i don't remember what it's called there's actually documentation for you that you can use for that so i'll cover all that hopefully things will go smoothly and you'll learn a lot but that's going to do it for me let's go ahead and let's jump into some code okay so we're going to start off by talking about some of the primitive data types and then we'll cover some of these pages once we get to the point in the tutorial where we need them we're going to start off with one we covered in the last episode which is your integer number and we'll just call this i guess my number and i'll say this is equal to 42. this is your standard number that you'll most likely be using most of the time this is just your i need a whole number to store this thing type of of data type so no decimals here the biggest this number can be is like 2 billion which is 2 to the power 32 minus 1 and the smallest it can be is negative 2 to the 31 i believe or 32 i think it's over here on this page let's see int the minimum value is negative 2 to the 31 and the maximum value is 2 to the 31 minus 1. so why is this one minus 1 when this one doesn't have that well the reason is your maximum value is what happens when you go to the max positive of course and when you start to go into the negatives one of your 32 bits needs to keep track of this negative symbol right here so you can sort of think of this as being the counterpart to the negative symbol because a bit can either be a zero or a one you have two choices one of the choices would be an actual value the other one would be the negative symbol so we do have to offset this by 1. now if you go above the 2 to the 31 minus 1 value and let's say you add 1 to that you'll end up with a negative number it'll be your absolute minimum if you're at your absolute minimum and you subtract one you can go to your absolute maximum value so they sort of loop around which means that's something you do need to watch out for and make sure you're not adding two really big numbers together now if you do have big numbers like that the long type it can also be very helpful so we'll just call this bigger number say this is equal to i don't know 3 000. now this can be up to negative 2 to the 63 in size or 2 to the 63 minus 1 for the same reason it's assigned data type so one of your bits is used for the sign and this is a really big number like this is borderline incomprehensibly large i don't even know how i would picture a number this big that said sometimes you do need bigger numbers and that's where something called a double comes in so you have a double which is a really really big number right and this is you know you could just set this to whatever it doesn't matter one thing to keep in mind though as as these numbers are used you might be adding one to the other so let's say you have your integer and your long and let's say you say i don't know we have like another int so we'll say int new number equals my number [Music] plus bigger number and this is going to throw an error and if we hover over this it says incompatible types possible lossy conversion from long to int and all that's saying is is that because this number is bigger than this number by trying to store this inside of an int which is a smaller data size this number could be bigger than 2 billion which could cause this to not work properly so ideally what they would have you do is have you make both of these along make this along or i guess you could even leave this as an integer and that's just because this number should be able to handle it now of course this could also completely go puts on you because if this is your maximum float value and this is one then this is also going to not work right so that is something to keep in mind now the other thing to keep in mind is that integers work just as you would normally expect them to in math but some of these other types like the long type sometimes requires you to put an l at the end of it to indicate it's a long this one requires you to have a d at the end of it to indicate it's a double and that's just so that if you're adding this number and this number together for example it knows whether you want like what kind of or i guess not really for this one and this one but if you're adding this one and this one so it knows that this one's supposed to be a long and this one's an integer this is very rarely used but it is something to keep in mind most of the time most of your math unless you're doing something very math heavy it's going to just be using integers or or longs and even when you're using longs you usually won't find yourself doing this all that much at least anecdotally from my experience so the last type to talk about is uh this double here can actually take decimal places and so can a float so we'll say this is my float i'll say this is equal to 3.14 because we like the number pi and you can see with this float it's immediately throwing a fit and that's because it's it thinks that the value here by default because it's a decimal is a double so in this case with the float you do have to explicitly say f and you'll do this almost every time you use a float you'll just get used to throwing an f at the end of it and just pretending like that's the way things you know that's just the way things need to be so now that we sort of have these numbers out of the way we can quickly go over some other types one of them is going to be a uh let's see i don't know if it's yeah it's boolean in java getting my languages confused and a boolean is just i don't know do i like pizza true a boolean is just a true or false so this is uh one bit in size and it's just you know a zero if it's false and a one if it's true i think uh by default it depends on the language but i think that's how it works in java uh so this you'll just use whenever you want to store a condition like you know does character have skill unlocked equals true then they can use the skill if it doesn't equal true then they can't use the skill for example you also have a short and this is just maybe you're working in a very memory intensive environment and let me actually move this above the integer and i'll try to order these in terms of the size of the value so if you're in like a pretty constrained environment but you still need to use a number that's you know anywhere from a thousand to thirty two thousand uh you can use a short so this is i don't know a smaller number say this is equal to seven thousand fifty six and this is just your um you know sort of your your conservative small number that you're going to use but you still need to to be able to store a decent amount of stuff now for those of you that have used or played minecraft before you might be familiar that at one point i don't know if they still do but at one point they actually stored all of their blocks in a byte array and this is uh no different so this number can be between i believe it's negative 128 and 127 same thing right here looks like that's true and this is just an actual byte so you can actually model this one out if you wanted to if you know your binary so because it's a byte it's going to look something like this and then we can just model out a few of these so let's say you have the number i don't know let's say you have the number one or you can do the number two or you can do the number three or you can do the number four and you can sort of see how this continues and basically covers all you know 256 different different numbers right so this is for your power 2 to the power 0 i believe to the power 1 uh or i guess to the power yeah 2 to the power 2 minus 1 i guess is what it would be and this would be 2 to the power 1 minus 1. and this goes all the way up to 2 to the power 8 minus 1 which should get you close to the number it doesn't matter but the basic idea is it's just a small byte that you can use to store your values so if you have a smaller value that you need to store bytes pretty standard to use for that but again it's all going to depend on if you if you're working in a very memory intensive environment in a lot of cases people just use an end because the the memory difference at this point with how bigger computers are most people just really don't care and then the last one i believe if i haven't missed any it's just going to be a char so we'll say my initial and this is just equal to the letter d now this one does go inside of single quotes and this is just a single character so you can sort of think of a char as how strings are built so in the last episode we had um we had string what did we say string initial plug right and this was just equal to some words here well that you can sort of think of this as like you know this plus this plus this plus this or something like that and um that's sort of how it's building the string now of course that's not exactly how it works but you sort of get the idea so we'll just say hello world so that's sort of all of our types here and again i'll have i'll have a link for this page in the video description it does also cover some additional stuff like what the default values are for each one so if you create a new one you know you can expect the byte to be zero the short to be zero the long to be zero l the float to be 0.0 f and boolean to default to false um but work what can we do with this so the next thing we can cover is sort of your operation so you've already seen me do plus you can probably reasonably uh assume what minus is going to look like so i'll say into result equals 10 minus 4 i guess and then let's just do a system.out.printline and we'll just print result and that let's actually do one more of these above it so we'll just also print this one to the command line and we'll just say my result was and we'll get rid of the print line and we'll just leave the space here so we get it a bit neater formatted we can run this and we can see what this says so my result of 10-4 was this right okay so let's actually do something else so we'll say int a equals zero into b equals zero and then instead of saying my result was i can just say whatever this operation is right here so in this case we had 10 minus 4 but let's say a is equal to 10 and b is equal to 4. then grab a and grab b and then we can actually just grab this whole thing paste it in here and then let's put a equals symbol after it with a space and then when we run this you can see we sort of have our expression right here being printed out so into result equals a minus b which equals six of course this you'll have to copy and paste this every time but if you're debugging and you want to see what this is doing this is a good way to see it for now i'll just say result is equal to whatever so just do this okay so we have plus we have minus we can of course also do let's say into c equals 32 and then don't forget your semicolon and then after a minus b we can then say plus c we can run this and it'll just do these in the order of operations which is a good thing to make a note of math operations follow pemdas so if you don't know what pemdas is i can quickly write that out for you so parentheses exponents multiplication division addition subtraction so this is the order that pemdas happens in i'll just paste this up here so we don't have as many comments and one final note if two things are the same level in pemdas operations happen left to right so let's say you have a times b times c it's not going to do all of this at once it's not going to do b times c first it's going to do a times b times c so if we run this we can figure out whatever this is because this is how you do your multiplication don't put x's here because x's could be a variable so in general they don't let you do x for your multiplication it does have to be the asterisk you can see here the result of that was 1280. okay so that takes care of multiplication addition subtraction maybe you want to do a bit more complex math here so maybe you want to say this should happen a times b and then after that happens we want to uh subtract c so you want it to happen specifically in this order you can put your parentheses around it and this will work just like in math just make sure you're actually using parentheses if you try to let's say use square brackets here it's going to yell at you it's going to say illegal start of an expression and then it's just it's not going to understand how to fix it so make sure you're using parentheses not braces or brackets and this should work exactly how you want it to now of course you can nest these so you can do this and then say plus 42 and this would also work but what happens if let's say we get rid of this and we just do let's do a plus b and b is now a double this is now going to be upset why is it upset again this is a incompatible type because it's trying to convert from the bigger data type of double to the smaller data type of int because this is a bigger data type this plus this could be a lot bigger than this and things could break if your result is a double this is no longer an issue but it's important to note that if this is one and this is the maximum value of a double then this could break and i can actually show that to you so we'll take our into result say a and b will make a equal to one and then for b what we can actually do there's a class library that we can use and for all of these i believe i have the page open somewhere maybe i don't okay basically for all of these types here there is a capital first letter version of the type that you can use to get information about the class or about the the type so we'll take integer for example in this case you have to spell out the word integer then you put a dot just like when we do the system.out.print or system.out.printline the first word is a class or a class library so that needs to have a capital letter so in this case it's capital letter i integer dot and then whatever you want out of it so i'll put a dot here you can see all of the stuff in here that you can use some of it won't necessarily make sense but some of it's pretty straightforward so if i give this if i use max here with an a and then in b so i pass in one number here comma another number it'll give me the max value of that number so that's pretty cool uh the one we're after though is our max value right here so you can see here a constant holding the maximum value an int can have 2 to the 31 minus 1. you can also see here bytes is the number of bytes in an int and it'll tell you there's 4 bytes in an int which means there's 32 bits because there's eight bits in a byte but for this we'll grab the max value because you don't need to know about bits and bytes right now so we'll say this is the absolute biggest number that can go in an int and this is one and we're going to take the biggest number that can go on an int and we'll add one to it and we'll see what that number is we can also do actually before we do this let's just print out b that way we can see what the biggest number we can possibly have is so you can see here it's 2.147 billion or 2 billion 147 million and if we undo this put the result in here and we run this this is going to be negative 2.147 billion and the last number here is going to be a little bit different than the last time we ran this but that's just because we had an overflow here now of course you can remedy this if you just do double of b and instead of doing the integer max value we just copy it up here so we make this a a bigger number add the d to it so that this stops yelling at us because without it it knows this is this number is too big if we actually move this down one it'll be fine but because eight makes it bigger than the integer max value we then have to add the d and then we can do double result a plus b we can print this and this will work just fine so you can see here it's actually returning scientific notation that's fine though the number still worked what we could actually do we could make a 10 actually let's make a i don't know a hundred thousand and then let's change this to a times b and see what number we get back and it looks like that is e to the 40 or to the 14. so you can see this can handle really really big numbers and that's sort of why you need to be conscious of which data types you're using and for what okay so now that we have all of that out of the way let's talk about the elephant in the room which is everyone's favorite topic in math when they're in grade school at least as long as you're doing the long version of it how do we do division so if i say int result is equal to a divided by b i'll make b another integer and let's use numbers that we can understand so i'll take 28 to be a and b can be seven which means that our result here ex expected result should be four so when we do a divided by b we expect to get 4 and we're not going to use this expected results just so we can read it so if we run this and see here the expected result is 4. what happens if we change this to an eight well i come over to google and i type 28 divided by eight i get 3.5 if i run a divided by b which is 28.8 that means this should be 3.5 which means this needs to be a float change this to a float give it the f now it's not complaining anymore this isn't going to give us 3.5 this is going to give us 3. and the reason is it treats both of these as whole numbers and treats the result as a whole number so when it does a whole number divided by a whole number there's no decimals there and the result has no decimals what we could do of course is we could make let's make this a float so float result equals a divided by b but this is still not going to work because both of these are integer values so you're going to get 3.0 because your result is of type float but both of these are of type integer and the way you can fix this is by making either one of these a float so if we make 28 afloat and we run this again i would expect to see 3.5 and we can also undo this and make b a float instead and i would expect to see 3.5 as well you can see there that seems to work what happens if this is an integer and we're using float math to divide it's going to say hey you can't do that because you're going to lose some precision going from a float to an integer so that's one thing to keep in mind is just that your you do need to use floats for both sides of this if you're going to be doing floating division and this does also work with doubles of course and you can go from a float division to a double just fine but of course if you have a double here and this is a float it's going to be upset because a double is a bigger type than a float which means you can lose some data so this is going to say incompatible types lossy conversion from double to float so that takes care of all of that and now the final thing i'd like to cover now that we have all of this out of the way what happens if i want to do exponents so there's two different ways you can do this the first if i just change this to an int and i leave this as a plus b oops a plus b and then i change all of these to an int again i think i just went into insert mode i get rid of this and i get rid of this and i get rid of this there we go actually i can just get rid of this whole line so if i have a and b i'll leave this as let's say the number three and the number two right so if i want to do 3 to the power 2 the first thing i can try is just doing a times a that gives me a squared because it's a times a which is a to the power 2. of course you can see why this doesn't really scale well what happens if i want a to the power 5 well then i have to do times a times a times a which means that this b variable is basically useless what we could also try doing in some languages you can do something like the star star operator to do your multiplication that's not supported in java at least as far as i know so the only thing we can really try is to use a third-party library and for this i have a web page open it's for the this is again built into java just like with the system.out or with the integer type or the float type so like for example you can do capital f float dot and then get its max exponent its min exponent its size its max value and stuff like that but with this one it's called the math class there will be a link in the description and we can type capital m math which is the name of the class library of course dot and then you'll see all of these wonderful things you can use in it and on this page that i'll have in the video description it also has all of them just listed out so you can see here i can get the absolute value of a double or of a float or of an int and all i have to do is just say math dot abs pass in a number negative six and then if i run this it'll give me the absolute value of negative six which is positive six and we got positive six the other thing we could try if we're trying to do exponents this is actually for a different different operation this is your e raised to a power operation so don't use exp but there's one in here for uh let me see where it is i think it went past it p-o-w for power so we can say math dot p-o-w we pass it our number that we're using as the base and then we're passing the number that we're using as the exponent now you can see here that pow takes in a double and a double and it returns a double and because this is going to give us back a double and a double is bigger than an integer this error is probably going to say no lossy conversion which it does so what we can do is we can change our result to a type of double and then this will work just fine and of course this makes sense integers are going to if you're raising them to powers they can very quickly become very large numbers so having this return or give back a double makes sense and you do need your comma here if you don't have your comma he'll also complain let's see what this error is it says it was expecting a parenthesis not a statement semicolon was expected either of those just means you need your comma there because this takes in two different arguments it takes in a double a and a double b so you need two two doubles here simple enough so if i run this 3 to the power 5 i don't really know what to expect but it's probably going to be larger than looks like it's 243. what other numbers could we try well let's try 7 squared which should give us uh 49 and it gives us 49. we could do 84 squared which should give us 7056 or we could try let's say 10 to the i don't know let's try 10 to the 100 see what happens that's a pretty big number looks like it handled it just fine it's 1.0 e to the 100 which makes sense in scientific notation and then let's try to break this i think the max value here is about 340 so let's make this 400 let's see what happens we run this and our result is infinity which of course isn't accurate but doubles can't go bigger than infinity so when you're playing those games where the idle games where your currency goes up into the bazillions or whatever number they use those numbers are often a lot bigger than a double can hold and in that case you use similar to this math library use something called a big number and that's usually just some code that someone wrote that you use like a library but it's not actually supplied by java so you can sort of see how having all of these tools here you sort of start to build your own tools when the language doesn't give you the functionality that you need so let's say you need like a really really precise big number library normally what i do i go to google java big number library i search for it this right here first link big integer it might even be supplied by java i don't know it looks like it is supplied by it arbitrary precision today i learned huh i guess i guess that answers that question so sometimes even you know even people that have been programming for years they just stumble across some of these things you know it's just it's just how the world works you're always going to be looking stuff up don't be afraid to experiment like we did in this episode uh you know changing some of these numbers like a float just to see what happens you know i think if i change it to a float it's going to be mad because it's taking a double and it's giving that back to me and the worst thing it's going to do is yell at you and say you know you you gave me the wrong data type right it's not like you're you're failing the class or you're failing to learn to program if you get the wrong result most of your learning comes from reading those strange errors and seeing what what it's yelling at you for so that is that is something to always be cognizant of is that errors are your friend as long as you understand them and you can figure out how to solve them okay so that's gonna do it for this uh java tutorial today's been a long day i'm pretty sweaty hopefully you learned a lot and uh you know it's not as hot where you are as it is here uh but that's basically going to do it for me next time we'll cover some conditional branching we'll cover how to use the booleans more effectively maybe do some loops and some function calls and all the other good stuff and hopefully soon we can have you writing your own programs that's going to do it for me hopefully this video was helpful if it was don't forget to subscribe and like and all that other youtube jazz that youtubers are always showing this video didn't help you don't forget to dislike it because that tells me i need to do better and that tells other people not to watch this tutorial because there's a better one out there never be afraid to dislike the videos as long as youtube allows you to because that that does actually help uh get the right videos to the top of the algorithm list so you know it's not like it's you saying i'm bad as a software developer maybe there's just something off with the video maybe i just did something wrong it's fine it happens don't be afraid to dislike the videos but that's gonna do it for me thank you so much for watching i will see you in the next video have a wonderful day [Music] you
Info
Channel: Deanin
Views: 63
Rating: undefined out of 5
Keywords: deanin, Primitive Datatypes In Java, Java Long, Java Float, Java Double, Java Doubles, Java Int, Java Ints, Java Char, Java Chars, Java Byte, Java Bytes, Java Boolean, Java Booleans, java floating point, java integer, java characters, java float vs double, java float and double, java float vs double vs long, java data types, java floating point numbers, java integers, java, learn java, Primitive Datatypes, java data types and variables, java tutorial for beginners
Id: y1YJXAbLkjg
Channel Id: undefined
Length: 33min 15sec (1995 seconds)
Published: Tue Jun 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.