English (United States) subtitles

← Boolean Data Type

Get Embed Code
14 Languages

Showing Revision 4 created 09/24/2016 by Udacity Robot.

  1. If I check this box in the app
    then it'll add the whipped cream
  2. topping to our coffee.
  3. >> That will make
    the coffee more delicious.
  4. >> Yeah.
    >> And perhaps more expensive.
  5. >> True.
  6. Eventually we're going to have to learn
    how to create an order summary and
  7. then send it off.
  8. >> Oh, so when we send the order
    summary, that's when we get the coffee.
  9. Right?
    >> Yeah.
  10. >> That's good.
  11. >> The problem is we don't exactly
    know if the box is checked or
  12. not
    >> Wait, you just checked the box.
  13. I know you checked the box.
  14. >> Well it's easy for a human to see,
    but it's hard for the phone to see.
  15. >> Who you calling a human?
  16. >> Anyways,
    when you add the CheckBox to your app,
  17. then it's actually
    creating a Java object.
  18. [SOUND] Like this.
  19. >> How many of these
    boxes are out there?
  20. >> Many, because in fact, inside
    the CheckBox object we have a variable.
  21. >> Okay.
  22. >> A Boolean variable.
  23. >> Boolean?
  24. Haven't heard that word before.
  25. >> Well a Boolean just means
    that there's two states here.
  26. >> Like New York and California?
  27. >> No.
    It's more like a light switch.
  28. [SOUND]
    >> Damn it.
  29. How do you do this?
  30. Anyways.
  31. So you're saying when we created
    a CheckBox, we had a Java object.
  32. Inside of that we have
    a variable of type Boolean.
  33. >> Right.
  34. >> And the type of that
    variable is either on or off.
  35. >> The value of it is yeah, on or off.
  36. >> Okay, so
    I can turn the value from on to off.
  37. >> I can't see anything.
  38. Can you turn it back on?
  39. We're not done yet.
  40. >> Oh, okay.
    >> Okay, so
  41. Boolean can have two states,
    true or false.
  42. And that represents the checked
    state of this CheckBox.
  43. >> Whoa, that's a lot of
    checks in that statement.
  44. Let's just back up.
  45. I'm going to summarize.
  46. Tell me if I got this wrong.
  47. So when we create the CheckBox
    we created this Java object.
  48. Inside of that Java object
    we have a variable.
  49. Its type happens to be Boolean, and that
    type of variable can hold two values,
  50. either on or off, one or
    zero, true or false.
  51. >> Exactly.
  52. >> Okay.
    So let's play with the Boolean.
  53. [SOUND]
    >> Okay.
  54. it's time to take that away from you.
  55. [LAUGH]
    Let's look at what the documentation
  56. says about Booleans.
  57. I'm going to Google search for
    Java data type.
  58. That's because Boolean is one of
    the primitive data types in Java.
  59. Primitive, meaning basic data types.
  60. I'm going to scroll through
    this list of primitive
  61. data types until I find Boolean.
  62. And here it is.
  63. It says the Boolean data type only has
    two possible values, true and false.
  64. It's used for simple flags that
    track true and false conditions.
  65. Then it says that a Boolean
    takes up one bit of information.
  66. This is about how much space
    it takes up on a computer.
  67. Anyways, the important part is that
    a Boolean has two possible values.
  68. You might think that true and
  69. false are actually string values
    because they're text, but
  70. actually, they're not in double
    quotations, so they're not strings.
  71. They're actually fixed literal values.
  72. Do you remember how fixed literal
    values are zero and one or
  73. specific strings like hello or today?
  74. That means that when you create
    a Boolean variable you can set it to one
  75. of these two values.
  76. Going back to our box analogy, if we
    have a variable called isChecked and
  77. it stores information on whether
    a CheckBox is checked or not,
  78. then inside this Boolean variable
    we can store the value true.
  79. Then we can interpret this as
    the CheckBox being checked.
  80. Or we can store the value
    false inside this variable.
  81. Then we could interpret this as
    the CheckBox not being checked.
  82. A Boolean is great for
    having two possible states.
  83. Then it's up to you as a developer to
    interpret what true and false maps to.
  84. If you need more than two possible
    states then you can't use a Boolean.
  85. You need to use something else
    like a string or an integer.
  86. Declaring a Boolean variable is
    very similar to declaring an int.
  87. It's not as complicated as
    creating an object variable.
  88. It follows the format of
    Boolean hasWhippedCream = true.
  89. We have the data type, which is Boolean,
    followed by the variable name, which
  90. can be anything we want, and then the
    initial value which is true or false.
  91. Here are a couple of other examples.
  92. This is where I set the same
    Boolean to be false.
  93. And here's a different
    Boolean called isRegistered,
  94. and I just initialize that to false.
  95. For example, this variable could
    represent whether a user using our app
  96. actually has a registered
    account with us.
  97. Here's another example.
  98. Boolean isOrderForPickup = true.
  99. This variable could represent whether
    someone putting in an order for
  100. some coffee wants to pick it up at
    the store or have it delivered.
  101. While our app doesn't support these
    features, you can imagine how using
  102. Booleans could be useful because these
    things have two possible states.
  103. Also notice the way I
    named these variables.
  104. A Boolean variable name has to
    follow the same conventions as other
  105. variable names.
  106. However, it's common to start with
    something like has something or
  107. is something.
  108. When you see names with has or is,
    it's likely a Boolean but not always.
  109. What I'm about to show you is
    a little more advanced and optional.
  110. So if you're interested
    you can follow along.
  111. This is the documentation page for
    the CheckBox class.
  112. I'm going to click on view source
    to look at the source code for
  113. the CheckBox class.
  114. This code is saved in
    the CheckBox.java file.
  115. I want to show you that there's
    a Boolean variable inside of this
  116. CheckBox class.
  117. But I don't see it here, so
    let's check the super class.
  118. If this value is true then
    the CheckBox is checked.
  119. If it's false then
    the box is not checked.
  120. You'll notice that this name doesn't
    contain the word has or is, but
  121. if you go down here you'll see
    that the other Boolean variables
  122. do have the word has in it.
  123. It's all a matter of preference for
    the developer.
  124. In this case, it's probably pretty
    clear that this is a Boolean for
  125. the checked state.
  126. And while this Boolean is not found
    directly in the CheckBox.java file,
  127. since we're inheriting from
    the CompoundButton class,
  128. we get this checked Boolean for free.
  129. In general,
    looking at the source code for
  130. the Android classes
    isn't required at all.
  131. To develop Android apps,
  132. all you need to know is how to
    look at the documentation pages.
  133. Okay, so in this video we learned
    about how Booleans can store
  134. a value of true or false.
  135. In this quiz,
    practice creating a Boolean variable and
  136. updating it by typing out the lines
    of code in the boxes provided.
  137. Type this line exactly in here and
    same for the other boxes.
  138. Then I want you to think about
    other good use cases for
  139. storing information in a Boolean.
  140. Think about what has
    two possible states.
  141. Do you ever use alarm clock
    on your phone to wake up?
  142. Well chances are that when
    you turn your alarm on or
  143. off, that that's actually getting
    stored as a Boolean variable.
  144. Another possible Boolean
    variable is whether you want to
  145. repeat an alarm or not.
  146. In the Gmail app, if I really want to
    star this special message that I got
  147. from the Gmail team in 2011,
    then I can star it, or I can unstar it.
  148. Since this has two states, it could
    be stored as a Boolean variable.
  149. A possible Boolean variable name for
    that could be isStarred.
  150. You could also use a Boolean variable
    to keep track of whether an email
  151. is read or not.
  152. This email is unread,
    whereas this email is already read.
  153. Speaking of YouTube let's
    go to the YouTube app.
  154. In the YouTube app,
    when it displays a list of videos,
  155. if I've already watched it,
    then it shows this little text box here
  156. that says watched in the top left
    corner of the thumbnail image.
  157. The fact that I've watched or
  158. not watched the video could also
    be stored as a Boolean variable.
  159. I just showed you some use cases for
    Booleans, so
  160. try to come up with your own here.
  161. You can browse existing Android apps for
    some ideas.