Java variables ❌

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going everybody it's your bro hope you're doing well and in this video i'm going to teach you guys all about variables in java so sit back relax and enjoy the show if you find this video helpful please remember to like comment and subscribe your support will help keep this channel running all right guys and gals let's talk about variables now a variable is a placeholder for a value and it behaves as the value that it contains do you remember from either elementary school or middle school when we were working with algebra we usually had to solve for some sort of variable like x or y and x or y contained some sort of numeric value and for all intents and purposes this variable behaved exactly as this value well with programming we can perform something similar to that but we are not limited to just numbers we could also store words whole sentences and these things called boolean values which hold either true or false but if we're going to store a value within a variable we have to list the data type of what we're planning to store within that variable is it going to be a number a word a boolean so we need to discuss data types there are eight primitive data types and a special reference data type called a string anything that i have noted with a star is particularly important so i would pay special attention to these our first data type is boolean this has a size of one bit so it can only hold two values that being true or false if we're attempting to sign a boolean value we would type either equals true or equals false something similar would be let's say we have a light switch program well if the light switch is on we could say that the light switch has a value of true if it's off it has a value of false so this is binary that's why it only uses one bit it only needs one bit of memory to function next we have byte this isn't as important as a few others but with one byte we can hold an integer number between negative 128 to 127 because a byte only has one byte of memory a short has two bytes of memory so can hold a larger number between negative 32 000 and some change to 32 000 and some change so integers integers are important these use 4 bytes of memory and they can store a number to just under 2 billion to just over 2 billion because they use 4 bytes of memory and a long they use eight bytes of memory so they can hold a very large number in fact they can hold a number between just under negative nine quintillion to just over positive nine quintillion now a float they can store a fractional number specifically up to six to seven digits what makes floats different from these data types on the top here is that bytes shorts integers and longs can only store a whole integer they cannot store this decimal portion so if you're working with a program or a variable that uses a fractional number you'll need to use either a float or a double and a double has more precision it uses eight bytes of memory and it can store a fractional number up to 15 digits so in comparison with a float this has less precision than a double and for an example i just listed a few of the digits of pi with this example we can only store six to seven digits of pi but with a double we can store up to 15. there is one strange convention with floats if you're going to assign a value to a variable that's of the float data type you need to follow the value with the letter f with double variables you actually do not need to do so so that's one major difference when assigning values between these two now let's move on to characters pronounced char for short think of charizard this uses two bytes of memory and this will store a single character letter or ascii value an example would be the letter f but a common convention with assigning values to a char variable is that you need to surround this value with a pair of single quotes and our last data type is the string data type the size really varies because these are reference data types they store a sequence of characters like a word or a sentence you could store a single character within a string but chars and strings behave differently because chars are primitive data types and strings are reference data types so let's distinguish the difference between primitive and reference data types here's a super quick description between the differences of primitive and reference data types primitive data types there are it and we just discussed them they are boolean by short integer longs all those cool things that we just discussed reference data types like strings well there's an unlimited amount because they are user defined primitives store data reference data types store an address primitives can only hold one value reference data types could hold more than one value primitives use less memory compared to reference data types which use more memory and primitive data types are faster compared to reference data types which are slower now you're probably thinking cool story bro but how do we create a variable well i'm glad you asked that question so the first process with creating a variable is that we need to declare the data type of what value that this variable is going to store so come up with a variable name like x and we will precede this variable with the data type of the value that we're planning to store within this variable and then with all statements we follow this with a semicolon at the end the next step is called assignment we will take our variable and assign it equal to some sort of value of the data type that we declared this variable to be but you could combine steps one and two together and this process is called initialization we would take the data type followed by the variable name and set it equal to some value and then add a semicolon at the end to finish the statement so you can either do this in two steps with declaration and assignment or combine them both together which is initialization how about we create a few variables does that sound good to you guys so let's begin with creating an integer variable let's say int x this step is called declaration we are declaring the data type of what value is going to be contained within this variable the second step is called assignment we can assign our variable a value let's say x equals 123 and this step is called assignment or we could combine both of these steps together and this process is called initialization intex equals 123 and this would be initialization so we can do stuff with this variable it will behave as the value that it contains we could print this to the console window so within a print or print line statement we could print the value of x so make sure you do not write this within quotes right now this will display the value that is contained within x which is 123 because this variable behaves as the value that it contains if you were to surround this with quotes what we are doing now is printing a string literal you can also print text as well as a variable together let's say we have a sentence a string literal that states my number is and then if we want to do some string concatenation with a variable we would add plus and then the variable name make sure this is not within quotes so this will display the sentence my number is plus our variable and in the console window it states my number is 123. so with integer variables the largest number that you can store within an integer variable is just over 2 billion let's say we are working with an extremely large number like the amount of student debt that i owe well this number is too large to store it within an integer variable we would probably want to use the long data type and one convention with assigning values to a long variable is that you need to follow this number with a capital l for some reason so we can now work with extremely large numbers so this might be useful if you're working with numbers like the speed of light or something so we now have a long variable and we can display this value a few of the other data types that we mentioned were bytes and shorts they have a lesser number that they can store so with bytes you can only store up to i believe 127 so we could store like 100 within here and this would be fine but 130 would be a little too much though so we don't tend to use bytes and shorts too much as a beginner because it's just way more convenient to work with integers um but you might use longs every once in a while too but as beginners we're mostly going to be sticking with integers now a double can store a number with a fractional portion with integers we cannot store a decimal portion so if this was 123.01 well we cannot store this decimal portion we can only do so with a float or a double so with a float you would type in float for the data type let's create a new variable like y float y equals 3.14 and a common convention for assigning numbers well values to float variables is that you have to follow this with f so you can store a number with a decimal portion within a float or a double and then we could display whatever this value is so y is equal to 3.14 but people tend to use doubles more because they have more precision and then you do not need this f at the end so this will store up to 15 digits after the decimal portion so we also have booleans let's say boolean z equals this holds either true or false and then we can display what value is within this boolean so if we print our variable z this will display false or we could hold true and this will display true but we can't display anything else besides those two like we cannot hold the word pizza because booleans only hold true or false we have characters char for short char and we don't necessarily need to come up with a variable that's only one letter we could have a name or something that's descriptive for this let's say we have a variable called symbol char symbol equals and then place a character within single quotes let's say we want the at sign so we now have a variable called symbol that contains the at sign so if i were to display this variable symbol to the console window it will display the value that is contained within which is the at sign and lastly we have strings so with strings these start with a capital s because they are of the reference data type anything that's a reference data type begins with a capital letter and let's say we want to store our name so string is the data type let's say the variable name is name equals and to store a string it works similar to a string literal we're going to use a set of double quotes and display or add a bunch of text like my name and then i can now display my name to the console window or i could do some string concatenation too and display the word hello plus my name and within the console window it's now going to display hello bro so that is everything you need to know to get started with variables in java if you would like a copy of all this code i will post all of this in the comments down below don't be afraid to smash that like button drop a comment down below and subscribe if you'd like to become a fellow bro
Info
Channel: Bro Code
Views: 16,030
Rating: 5 out of 5
Keywords: java variables, variables java, java variables tutorial, variables tutorial java, java variables and data types, variables and data types java, java variable declaration, variable declaration java, java variable initialization, variable in java, variable initialization in java
Id: so1iUWaLmKA
Channel Id: undefined
Length: 12min 32sec (752 seconds)
Published: Sun Oct 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.