Primitives Data Types In Java - All the Primitives And What They Do

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to talk about all of the primitive data types in java that's bite short and long float double boolean and char we're going to talk about what primitives are and what each of those primitive types can do and what they can hold if you're new to the channel my name is john and i do a java tutorial video just like this one every single week either a video on a java tutorial like this one or a full walkthrough tutorial of a complete java program so be sure to like and subscribe to get the new video every week again thanks for taking the time to like and subscribe is the only way these videos get out to more people and so i really do appreciate it now first just what are primitive types or they're like the sticks and stones data types in java they are welded right into the java language itself and every other type in java is built on top of these primitive data types there are eight primitive data types in java that's byte short int long float double boolean and char to declare any of these primitives that we're looking at you just type the data type that you want to create byte short and long whatever and next the name of the variable you can give it whatever name you want and then equals and then the value that you want to give that variable the kind of value that you put here of course can change depending on what data type that you have but that's the general pattern that you're going to use every single time you make a primitive bytes are the first of four data types that hold whole numbers meaning not decimals or anything like that just whole numbers one two three whatever so first is byte what byte technically is is just um eight bits essentially eight ones and zeros basically what that means is it can hold a very small number from negative 128 to positive 127. it can also technically hold things like uh characters so if you notice i can change this to like the character c and this is valid but generally if you need a character it's a lot easier to just use the char data type that we're going to talk about later so because of the limited size of bytes and the fact that if you need a character there are easier ways to do it generally you can just ignore the byte data type at least while you're getting started learning java and short is the next step up in size short still holds whole numbers again not decimals now a short can hold about from negative 32 000 to positive 32 000 these are the exact values here so because of the increase in size there it's a little bit more useful than a byte you know you can do a lot more things with 32 000 than you can with just 128 but still just because the limit is so low you'll find yourself just not using shorts very much i find myself not really using either of these first two values but it is important to know that they exist and as you get way more advanced in your java career you'll probably run into them from time to time but while you're learning they're just easier things to use next one is one you've definitely heard of and probably used already and that is an int and that's because this is when the limit of the numbers starts getting actually useful it goes from about negative 2 billion to positive 2 billion now most numbers that you're going to work with in your everyday programs fall within that range and so that's why you find yourself using an int pretty often but if you know you're going to have to write something where you are likely to go past 2 billion then you're going to want to use long now long goes to i don't even know what this is this is thousands millions billions trillions quadrillions quintillions i think that's right so negative nine quintillion to positive nine quintillion so this is the largest whole number java primitive type that you have if you're doing anything with whole numbers and you know you're likely to go well past 2 billion then just use a long don't worry too much about it once you get into really large numbers just use a long now the trade-off of course is that when you use a long or anything as you get from the smaller data types to the larger ones you do use a little bit more memory but nowadays this memory is so cheap and there's so much of it on every computer unless you're doing something ridiculous you generally don't have to worry about memory too much and so just whenever you're picking one of these values just be on the safe side and pick the one that you know is going to fit the number that you need to use and keep in mind that if you try to store something that is too large and outside of the number range for each of these values that we've looked at so far you could run into all kinds of weird behavior from exceptions to what are called overflow errors so maybe your program will silently just have the wrong value if it tried to fit a large number into a little bitty box it may have just cut off the part that didn't fit and kept the rest of it and after that happens who knows what your program's going to do so always be sure to use the data type that you know will fit for your particular situation for example this is what you'll see if you try to assign a value that is out of range for what you're making so here i'm trying to assign a very large number as an int but i get an error here in eclipse that says this literal of type int is out of range for an int so it won't let me do it but also if i actually take this exact same value and try to assign it here you'll notice i get the same error so it says this literal of type int is out of range well it is out of range for int but of course it's not out of range for a long so what do you do all you need to do is put an l either uppercase or lowercase doesn't matter just a personal preference there i tend to like lowercase but either way works that just indicates to java that i want this value to be a long and you can see it's quite happy with this now and you can assign very large values to your long so that's all the whole number so you can see the first four out of the eight primitive data types are just for storing whole numbers generally you'll use ins and longs just depending on what size of numbers you need to deal with so let's move on down here next we have floats and doubles floats and doubles are used for decimal numbers and you can kind of think of them as well you have ins and longs for whole numbers and you have floats and doubles for decimal numbers they're the same type of thing they just hold different sizes of values now for decimals instead of just size the difference in these is kind of their precision floating point math gets kind of weird in the programming world but generally you can think about it like this a a float gives you about six decimal digits of precision and a double makes that much larger and you get about 15 decimal digits of precision so again it's just a trade-off of memory versus having enough space to hold what you need so just use the one that fits best for your particular situation if you know you're going to have just a very small decimal number maybe you have shoe sizes or something and you're only going to be up to maybe like a 14 and a half then you know that float is going to work just fine for your purposes but if you know you're going to be in a situation where you need a lot of decimal numbers then you're going to want to use double something to note here is you'll see that i have an f where i'm assigning my float value here and if i don't include it java is assuming that this is a double and it can't put a double value into a float it can't put 10 pounds of you know what into a five pound sack so you just have to put the f in there again either capital or lowercase just to indicate to java that this is a float and for doubles you don't need it because java does assume that this number is going to be a double you can put a d there if you want to be super cool but you don't need it even though there were four different data types for different sizes of whole numbers there are only the two for decimal numbers just float and double so it's a little easier to remember there so now we're done with primitive types for numbers next we're going to move on to booleans we've probably used booleans quite a bit already as well a boolean either holds true or false that's it a boolean has a truth value and it is either true or is false so when you create your booleans you just assign it the value true or false not in quotes or anything like that just true or false if you put it in quotes it thinks it's a string and it's a whole different thing so that is how you assign a boolean literal so if you wanted to start out a boolean and you want to tell java that this is true or this is false you can do that like this but more often when programming you want it to hold the result of some comparison that you don't know while you're programming whether it's necessarily going to be true or false so you might do something like this where you create a boolean here i call it more than a thousand where i assign it the the truth value of my int greater than a thousand so this my int greater than a thousand is either true or it's not my int is either greater than a thousand or it's not greater than a thousand so in this situation my boolean variable will hold the result of this comparison so at this point in the code if mayan is greater than a thousand this will contain true and if not it will contain false in this particular program my int up here is 2837 so this more than a thousand variable would contain true after this part of the program runs because a boolean just holds either true or false it's probably the simplest of all the primitive data types but booleans are everywhere in every programming language so it's probably one of the most important to get a good understanding of now finally we have char or car some people like to pronounce it car because it's characters i'm gonna call it char and you can call it whatever you want you can be wrong and call it car or be cool and call it a char anyway all a char holds is a single character and you put that character in single quotes if you try to put it in double quotes you're going to get an error because java considers anything in double quotes a string and it can't put a string into a char variable char is again just one of those data types that i don't find myself using very often usually in 99 of the use cases that i have i just use a string which can of course hold any number of letters and you probably use string before it looks like this string my string equals hello but the reason we're not talking about string in this lesson is because string is not a primitive data type you can even notice by just the syntax highlighting here in eclipse all the primitive data types are highlighted in one color orange for me and all other data data types are highlighted in blue like string so string is not a primitive data type that's why we're not talking about it but i will say that whenever i want to use anything that involves a character i'll usually just use a string anyway because if i have to do something else with it later add to it or anything like that it's easy to do with a string whereas with a char i just have to throw it out and create a string anyway so i've used char in some very limited circumstances like in my tic-tac-toe program here where of course in every position in that tic-tac-toe board it's either an x or an o well it's simply to use a string there because we know we're only ever going to need one single character so in that instance i did use a char but most of the time i do just use a string but this is one that you're likely to run into from time to time so it is good to know about if you enjoyed this video or learned something please let me know with a like and if you'd like to see more videos like this every single week either a java tutorial on a concept like this one or a full tutorial walkthrough writing a java program from scratch please be sure to subscribe so you get the new video every week and again thanks to those of you who do take the time to like and subscribe it may seem like a little thing to you but it's the only thing that helps get these videos out to more people so i really do appreciate that see you next time
Info
Channel: Coding with John
Views: 419
Rating: 5 out of 5
Keywords: java primitive data types, primitives in java, java primitives, java primitive data types vs objects, java primitive, java tutorial, java programming primitives, java primitives for beginners, codingwithjohn, coding with john, java programming
Id: WQ7mvQFSmYc
Channel Id: undefined
Length: 10min 23sec (623 seconds)
Published: Wed Mar 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.