thanks friend
.
its a long explanation
.
i read all the matter
.
Now i understand what is Boolean
.
.
now question is how to use it
So, Does my example belongs to Boolean
.
and what method will be for doing that
Boolean is not an action so it can't actually "work", it's a type of variable. There are various types of variables (of data types) where the most common are - let's say - string, integer and boolean. Let me explain you all of them, that should make you understang boolean better.
String is a text type variable so you can store some text f.ex:
- "Elvis is the only one real super hyper mega king of whole music history!" - this is some long text
- "a" - this is a single character, but still a string, still a text
- "1" - this is still a text, you see a digit but within the quotes it is treated as a string/text
- "" - this is an empty string, like a text without a text but still engine treats it like a text if this is a string variable
Integers are simply a digits like 0,1,2,3,4...
Now as you can see strings and integers can have many different values. Boolean is a type of variable which can have only two different values: true or false. It is not a text "true" or "false", true or false it's just a logic assumption for the condition result. You can also think of it like the boolean may have only a value of 1 or 0. Switch is on or off.
Now you probably wondering: "Why do we need a boolean if we can use Integer and just set it to 1 or 0, or even empty or not empty string?". Well that is a good question, and yes you can do it with integers and strings as well, but here are the reasons why you should use boolean if possible:
Reason 1. To save some memory and what goes after increase performance. Depends on the programming languages, booleans may be a really tiny variables, but in other langs they are treated exactly the same like integers but just "wrapped" with the boolean functionality (something like "enum" in SQL).
Reason 2. Safety. If you have a variable which should always be a digit, then you use integer, if a text then string if it's a switch/flag then boolean. If you use the proper variable type then you protect yourself from unwanted code results (calculation etc.).
Reason 3. Because semantic code is the good code. This means that every functionality you build should be built using tools/elements which are dedicated for the particular functionality.
If you are not an experienced develper then I understan those informations might be confusing or even sensless. Well but doing so is simply a good practice so I encourage you to stick to those rules, you will understand all the good things it brings once you'll get more and more andvanced developer. Especially once you start working in a team.
I hope you get the meaning of boolean now, and as you see it doesn't "work", boolean is simply a flag which can hold one o two possible values officially accepted as true or false.
If you write a game where you can turn on or off the lamp in the dark room, then you can use boolean to remember lamp's current state - isTheLampOn (true or false) etc.
It is also worth to mention that if you think of it more closely then boolean is usually the answer for some question (is the lamp on? yes/no). Therefore a good practice is also naming boolean variables starting with "is", so the variable name itself tells you what question it's value answers to... like: isTheLampOn, isOnAir, isShooting, isMoving, isVisible etc.
ALRIGHT! I was just about to write a short explanation and here's the "short" one haha.... but that is beautiful in development that even such a small thing as boolean is worth a discussion cause of its usability! .