Java Programming Tutorial 9 - Primitive Data Types

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on guys today we're gonna be going a little bit more depth on the primitive data types and how to use those and the different versions and so forth and that's gonna be really cool because then we're gonna understand one side of the data type spectrum then the other side objects we're gonna be getting into throughout this series now the series probably wouldn't be in existence without the help of our incredible sponsor pram for those of you who don't know which you probably should because pram is awesome Gramp is a peer-to-peer interviewing platform and what that means as you get paired with another individual to do interviews with why mainly to practice your technical interviews not only do you get that experience getting interviewed but you also get to get that experience interviewing other people so if you're terrible at interviews or you're just hoping to get a job in the industry or you just want to solidify your knowledge definitely check out per amp I'll leave a link for you guys in the description cramp is a free platform and it will revolutionize your skills so there's absolutely no reason to not go give it a try so thank you pram for sponsoring this video and with that let's get into primitive types so the ones we've talked about so far are int I'm pretty sure that's all oh I think we use double to so you can declare these things really easily the different spin in a double is that the doubles can use a decimal with the value after that so you can have fractional values inside of a double there are actually six other primitive types we haven't talked about and they are right here we got boolean byte char short int long float and double all right so let's declare each one of these so a boolean is true or false and when you give them the values you just use the value true or you use the value false but you don't put it in quotes that's a string and like we talked about in previous videos something in strings and something outside of strings are two separate things so these are not the same values this is a boolean false and this is a string that happens to say false so make sure you don't put the quotes next we have byte so a byte in computer science is a sequence of eight bits and a bit can be either 1 or 0 so for example we could have this this is an example of a bite and you don't really have to understand this in order to use this bite data type it's basically just a really small data type that can store a very small range of numbers you can also store characters in here so for example if you want to store a character such as the letter C you use single quotes and then you just put the value of C in there and how exactly can you store numbers and characters inside of the same data type well the reason that works is because this is all based on the ASCII table so if you guys haven't seen the ASCII table let me show you okay so here's the ASCII table and basically there's a bunch of stuff that's super overwhelming but the important thing to look at is this decimal number right here this column right here and the actual character right here so for example if we wanted a capital C that would be right here and it would have the decimal value 67 so the two values are kind of interchangeable meaning the value C and the value 67 are going to be stored exactly the same way in binary but whether or not it's a sea or a 67 just depends on the interpretation of that binary so the next one is char and this works the same way so we could say char C equals hmm what do we want to give it let's give it capital Z and going back to the ASCII table for a second so this gives us a pretty good range of characters you know we got all the numbers we got all the capital letters all the lowercase letters and this is usually suitable for small English applications but if you want to start supporting different languages and different types of characters then you're going to need a bigger list than this and that's where a Unicode comes in here is the unicode list of characters and you can see it gives us much larger range of characters so we can do all kinds of different things so with that the char data type allows us to store a Unicode which means we could store different language characters inside of this variable next one we have here is short short is similar to char and that they're both 16-bit which is double that of byte but short is used for numbers so you could put in here 32767 for example and then that's the highest number available or we could use the negative version we can actually go negative and then we get one extra value there don't worry too much about the ranges just know that short is for small numbers next up we have int which you know really steps up the game this is a 32-bit number so we can put some pretty darn big numbers in here and note that you don't use commas or periods inside of these numbers so you don't space them out like this and if you're not from the US you don't use dots either so you want to do something like this it's not acceptable you just leave it plain like that and then lastly we have long and this is the same thing as an int it's just twice the size 64-bit meaning you can store bigger numbers in here so you can see all the ranges here on this table but usually I don't worry about it I know that if I use an int it's going to cover most cases and then if I know I'm gonna be working with something huge well then I might want to use a long alright so we've covered all the integer categories and integers and characters are kind of swappable in the sense that the way they're stored in binary can be interpreted as a character or an integer just depending on the context but once we get into floating-point that's a little bit different and that's what we're gonna be talking about now you'll also notice here that I'm getting an error and the reason that is because when you create a literal value like this along and you want to specify hey this is a long you have to put a capital L right there so when we hover over this it says it's out of range for an integer because it thinks we're trying to create an integer but if we put that L there it should work now if we're in the situation where we have a really small number and it's not outside of the range of a normal integer then we don't need to put that L there because it's just going to get converted to a long so if you just want to be on the safe side just keep an L on your values it's clear because when I look at this I know that hey we're trying to create a long value not an integer which are two separate things and this can take some time getting used to if you're new to programming because in real life we just think of numbers as like one thing but there's actually different categories created by these data types so the next thing we're going to learn about is float which similarly we need to end with an F just like that and then we have the double version which works the same way you just don't need to put anything right here so the difference between a float and a double is that a double is 64-bit and a float is 32-bit so what that means is the float can't represent numbers as precisely meaning if you're really particular on the math and you want to get as precise as possible you'll want to use a double and I would argue that there's almost never a case to use float at all just use double in all circumstances unless there's a specific reason you need to use a float for something usually that'll be the case if you're like super restricted on memory but you're usually not gonna find that when you're programming in Java maybe if you're doing something in C but for Java double was going to work 99.9 percent of the time now when you put a decimal point followed by some numbers but you leave off the F it just assumes it's a double so if I tried something like this where I say 20.5 I'm going to get an error because it's assuming this is a double and it's not going to let us convert the other way is okay so if I did double D D equals 20.5 F we're not going to get an error and there's something important you need to understand here and that is the float is a smaller container than the double think of them as backpacks right that you can put stuff in in this first scenario we're getting an issue because we're trying to store a giant backpacks worth of stuff in a small backpack float the other one is not giving us an error because we're trying to store a small backpacks worth of stuff inside of a larger backpack that can store twice as much stuff so we only get errors when we're trying to store larger things and smaller things now all of these data types can be used within expressions so let me just put that F back in here so you don't get that error so for example I could say and then I could set that equal to DD / s for example so we can mix the data types inside of expressions this here is a double whereas s is a short and it's not giving us any issues that's because like I said we're storing it in a bigger container right the double is a 64-bit container but if we did something different such as making this a flow well hey now we're getting an issue cannot convert from double to flow now there is something called typecasting which will basically force it but this is going to put us as at risk for a loss of information meaning if you're trying to store two backpacks worth of stuff into one backpack you're going to not be able to put it all in there and you're gonna have to leave some of it behind that's not always going to happen though because we might just be storing some really small value inside of this double and then it's able to fit inside of that flow so what I would recommend if you want to learn more about these go through this document here or just look up primitive types in Java there's a lot of examples here on how to do casting and so forth so if you want to get a lot of information you can go check that out or you could just keep watching this theories because I'm gonna be using these kinds of primitives all throughout so the main takeaways from this video is if you're just trying to work with literal values these extra characters are important F for float capital L for long and that when you're going from data type to data type you can only store smaller things and bigger things without an explicit cast like right here also all these yellow underlines those are not errors it's just saying we created this variable but we're not using it anywhere in our code so if you've enjoyed this content please consider subscribing I would really help out my channel trying to you know take over the internet and whatnot you can also get the link for the sponsor in the description as well as a link to the crash course and the blogs thanks guys I'll see you in the next one [Music]
Info
Channel: Caleb Curry
Views: 64,587
Rating: 4.9632063 out of 5
Keywords: java programming, java, programming, code, coding, tutorial, tutorial 9, primitive, data, types, data types, beginner, java programming for beginners, java tutorial for beginners, java (programming language), java tutorial, java data types, caleb, curry, calebthevideomaker2
Id: qUXbJziVs_o
Channel Id: undefined
Length: 11min 0sec (660 seconds)
Published: Wed Nov 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.