Boolean Java Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in Java there's this term you may have heard before called boolean it confused me and it may have confused you but in this video I'm gonna show you exactly how to use boolean in Java and if you watch all the way through you'll have a fully working program using boolean in Java hey you it's Alex back again and helping you learn Java on this channel I make Java tutorials just like this every week so if you're new here then consider subscribing so we're in Eclipse and we'll just start off our project by going to file new Java project and we'll name our project we'll name it something like boolean in Java and hit OK next expand that and we're gonna make a file for our Java code so right click on source and go to new class class just means Java file so click that will name the Java file call it boolean in Java again hit this first check mark and hit finish now everything's set up for us I'll just make this text a little smaller here a boolean is a variable just like any other variable like int for integer string double long float boolean it's just another kind of variable so to make a boolean well let's see how we would make an inch to make a integer we in Java we would just type int that's a little keyword it turns purple and then we'll say we want to name our integer a and set it equal to zero that's how we would make an integer to make a boolean is the exact same thing except your type boolean like this this weird word I think they made up I've never heard it before before Java and say B equals let's try 0 we get an error here because we can't declare a boolean as a number it has to be either true or false it can only be those two things true or false nothing else so we can type true and now B is stored as true the other option was false so we can type false and everything works great if we want it to print out B we can do that like this be and we'll save it and run it and we'll see that false gets printed out we want to print something we want to print B we go up here we see that B is equal to false so we print false if you get one thing away from this video I want it to be this boolean can only be true or false well what if it's in double quotes like this can it be like that it actually can't because these double quotes turn it into something called a string literal which just means like the literal words not the keyword FAL se so it can be true or false it can also be anything that results in too true or false so it can be like if a is equal to zero this works it looks really confusing but what we're doing here is we're setting a boolean be equal to the results of checking if a is equal to zero that's what the two equal sign means I'll go over this in a comparison operator video on the screen now if it's supposed to get but it's kind of like if we wrap these in parentheses is equal to zero looks like it is and so that's true and so B is now true if we saved it and ran this it's true little sneak peek into comparison operators for you so what do we use boolean for Alex there this funny word what do we use them for in Java in my seven years of coding I would say boolean z' in Java are mostly used for if statements I'll go over what if statements are but first I'll write it out you type the keyword if and then parenthesis and curly braces there's a little red underline here because inside these parentheses you have to put something that's true or false sound familiar that's a bully so if statements always have a boolean in the parentheses right now we'll just pick one of our two options true or false we'll just pick true and whenever what is in this parentheses is true this code will get run the code that's in these curly braces so we'll do something simple like printout we are in the if statement like that course we'll finish it off with a semicolon zoom out a little more here okay good save it run and see what happens and look we are in the if statement if I change this to false save it and run it nothing happens and we actually get these yellow underlines and if we check out the warning it says dead code that means it never gets reached because it will never get run since it's false so the flow here is we have our class we named it boolean and Java we go inside and we see this main method the main method always runs code when we click the green run-button we see that the first piece of code in the main method is this if keyword an if statement works when what's in this parenthesis is true this code gets run if it's false it skips it and doesn't do anything so it looks like this program does nothing but if we change this to true like earlier save and run it we see that it is true and we do run this and that's mostly what you use boolean Xin Java for is passing through if statements and things similar to if statements now let's write a program together using boolean angel we're gonna write a program where we pass three different doors we go through the first door and then the second door and then the third door and those will be represented as if statements so we already have our first if statement we'll say we passed the first door like that save and run it we have our first door next we'll make a second one by just copying and pasting this since we're lazy change this to say we passed the second door and we'll copy and paste this one more time and say we passed the third door save it and run it and we get first second dirt now let's make this a little more interesting let's say we can pass a door or we can miss a door true we passed the door false we missed the door so we can just represent those as bullying's remember to make a boolean is just like any other variable except we use this funny word bullying will name it past door and that'll be equal to true next we'll make a second one called missed door so we'll name it missed door and said that one equals four false the only two options of bullying can be so now we can have a little fun with it let's say we want to pass the first and third door but not the second we could say passed door and this is the exact same thing as true so we didn't really change anything logically we're just replacing the true key word with our boolean variable passed door well skip the second one we'll say missed door and we'll pass the third door and type passed door save it and run it and you'll see that we only go through the first and third because this is equal to false so we skip this code entirely and I'm just gonna zoom out a little more here on the program so you can see everything I've been doing a lot of zooming out my bad let's make this even more interesting let's say if you pass all three doors then you win you win the program so we'll keep track of how many doors we passed and if we passed three doors then we'll print a message saying you want so the counter would go up one if you're in here you go up again if you're in here and it go up again if you're in here so we can use an integer for that and then add one to it each time we go through a door so to make an integer is just like a boolean just like any other type of variable we type int and then we'll say door count as the name of it and we'll set that equal to zero right now because we haven't gotten you through any doors yet if we go through the first door we will take our door counter and set it equal to whatever the door count currently is plus one this is the same thing as saying our new door count is equal to the previous door count which is zero plus one we'll do the same thing for the second door we'll take our door counter and we'll take our previous door count which in this case would be one and we'll add another one to it and we'll do that exact same thing down below but I'll just copy and paste that the fourth step is if you passed all the doors if our door count is equal to three then print the congratulations new one message but why do that when we could use boolean in Java make a variable saying did we win let's think of a clever name for it it's a boolean and say let's say I'm passed all doors good and what's that equal to false cuz they haven't done that yet but we know that they passed all doors if the door count is three so we'll say passed all doors is gonna be equal to true once you've passed all three doors in the door count now we'll just demo our little program here I'll make this a little bigger we want to make sure we pass all the doors remember past door is just the same thing as true so the code will get run and it looks like it'll get run for each if statement here since they're all true our door count will keep track of all the doors we entered and if it's three we'll have passed doors equal to true so we'll say that and click this being run button you'll see we passed the first second and third like we did before but it's not actually doing anything because we never printed the you've one message but we can use our boolean to see if we passed all of them so we could say if we passed all doors if passed all doors is true that can only happen if the door count is equal to three and that can only happen if you go through of these doors will say if you pass all the doors and then I'll print out and gradual I can't spell I'm the worst speller there's anyone else a bad speller leave a comment below congratulations you won the program you won the game Frick yeah man Frick yeah well save it run that and yes you see if you go through the first door and you pass the second door and you pass the third door then we get the message congratulations you won the program if we miss one of these say we missed the first door and change this to mist door save it and run it we don't get the congratulations message because the only pass to but sorry you gotta try again I'm going to expand this for you because it's a pretty long program but we're using a lot of boolean concepts and Java here a boolean can only be two things it can only be true or false nothing else you can do little tricks that results in true or false and then store that into a boolean like what we did before where we could say like is 1 equal to 1 make that a little neater with some parentheses take the result of that 1 is equal to 1 so that's true and then sort true into past or and that's what how it would happen there so we know bullying's can be only 2 or false and so we can use them in if statements and that's what they're used a lot of the time for whether we should go inside of an if statement or just skip right over it in this case it's skipping right over it because miss door is equal to false we'll go to the second one you see that past door is a variable that holds true so we'll go in here we'll print this we pass the second door and then we'll go to our counter which takes the previous door count and adds one to it do the same thing down here for the third door if door count is three then you passed all of them and we'll store true into the boolean variable pass all doors if that's true then we know we won the game and we know we can walk through we made these jokes on us cuz there's a key and we had to ninja our way through the doors and that's why it's so hard if you enjoyed this video or you think it might help someone else with boolean and Java please share it and also like that click that like button you could be doing anything in the world but you're here on this video with me learning how to program and I appreciate that so much I'll catch you next week [Music] [Music]
Info
Channel: Alex Lee
Views: 100,271
Rating: 4.9801865 out of 5
Keywords: boolean java, boolean in java, java boolean, using boolean in java, how to use boolean in java example, bolean in java, boolean in java example programs, boolean variables in java, booleans in java, boolean in java example, how to use boolean in java, java booleans, boolean, boolean variable in java, java boolean true false, java boolean method example, boolean variable, boolean data type, java boolean tutorial, java boolean operators
Id: CHVVEGRGiJU
Channel Id: undefined
Length: 13min 52sec (832 seconds)
Published: Thu Feb 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.