English subtitles

← Add Your First Variable

Get Embed Code
13 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. This line of code's responsible for
    creating a variable.
  2. Remember that a variable is like
    this box, number of coffees,
  3. with a value of two inside.
  4. We'll talk about each part
    of this line of code and
  5. then we're going to create
    more variables in this lesson.
  6. You may have noticed there's specific
    rules for how to declare a variable.
  7. When I say declare a variable it means
    the same thing as defining a variable or
  8. creating a variable.
  9. This is the line of code
    that we've seen so far.
  10. It follows a very specific format for
    how to declare variables in Java.
  11. You could think of it
    almost like a formula.
  12. First, we need to say what type of
    data can go inside the variable.
  13. In this case, it's an integer,
    which we call int for short.
  14. Next is the variable name.
  15. In our case,
  16. the variable name is numberOfCoffees
    followed by the initial value.
  17. In this case it's 2; for
    2 cups of coffee ordered, and
  18. this code statement ends with a ;
    according to the rules of Java.
  19. Let's break down this
    line one piece at a time.
  20. The first word is Data type,
  21. this is a type of data that's allowed
    to go inside at this variable.
  22. In this case we only
    let integer numbers.
  23. Gets stored inside the variable.
  24. As I mentioned earlier int is short for
  25. An integer is a whole number.
  26. Basically any number including zero and
  27. negative numbers that are not
    fractions or decimals.
  28. 0, 5, 145,
    all of these are examples of integers.
  29. The number of coffees ordered should
    only be a whole number like one,
  30. two, four, et cetera.
  31. We don't want the customer
    to order 1.5 cups of coffee.
  32. If they want more than one cup,
    they're going to have to buy two,
  33. then they can give the remaining
    half to a friend or something.
  34. As for negative numbers, it doesn't
    make as much sense in our case.
  35. There's no data type in Java that
    only allows positive numbers.
  36. So later in the next lesson,
    we're going to learn how to add code
  37. that prevents negative numbers
    from going inside the variable.
  38. The next part of the declaration
    is the Variable name.
  39. In this case it's numberOfCoffees.
  40. Notice that it's spelled camel case.
  41. It starts with a lowercase letter and
    since there are multiple words,
  42. it capitalizes the start
    of each other word.
  43. You can think of it as putting
    a name tag on the variable.
  44. You can refer to this
    variable with this name.
  45. And here I've drawn it
    out like this as well.
  46. This variable box has this name.
  47. If you want to know what this value is.
  48. Or change it, you need to use the name.
  49. Choosing an appropriate variable name
    is actually up to you the developer,
  50. depending on what
    the variable is used for.
  51. In this case, since the variable is
    storing the numberOfCoffees ordered
  52. I just picked this variable name.
  53. You could have picked other names,
    like coffee count or
  54. number of coffees ordered or
    ordered coffees.
  55. Really it could be anything,
    as long as it's easy for
  56. other people to understand
    what this variable represents.
  57. When I say that the variable
    name can be anything,
  58. there's actually a couple of rules
    that Java has about variable names.
  59. Let's look up those rules now.
  60. Let me type in variable names Java.
  61. This first link looks good.
  62. This is official Java
    documentation from Oracle.
  63. This top part talks about variables,
  64. which you can read in
    your infinite free time.
  65. Down here is what we're interested in.
  66. It talks about naming for variables.
  67. It can be any length, but a really
    long name is not very practical.
  68. A really short name,
    on the other hand, can be good, but
  69. if it's only one character, like N,
    you may not know what n stands for.
  70. So you want to strike a balance
    between a descriptive name and
  71. one that isn't a pain to
    type out every single time.
  72. There are a bunch more details here
    that you can read if you want.
  73. But the main point is that if your
    name only consists of one word
  74. then spell out that word
    in all lower case letters.
  75. Like cadence, speed,
    gear, things like that.
  76. If the variable name consists of
    more than one word then you want to
  77. capitalize the first letter
    of each subsequent word but
  78. it needs to start with
    a lower case first.
  79. So gear ratio is an example or
    current gear.
  80. Our example of number of
    coffees also follows this rule.
  81. The next part of the declaration is
    Assignment operator or the = sign.
  82. In math class, you're used to seeing
    things like one plus one equals two.
  83. But in Java,
    equals means a different thing.
  84. Equal means assigning
    a value to a variable.
  85. In this case, we're assigning the value
    2 to the variable numberOfCoffees.
  86. Using our box metaphor,
  87. we put the number 2 inside the variable
    that represents number of coffees.
  88. So whenever you see the equals sign,
  89. think about grabbing what's on
    the right-hand side of it and
  90. putting it inside the variable
    on the left-hand side.
  91. We already talked a little
    bit about this, but
  92. this two here is the initial
    value of the variable.
  93. So you put the 2 inside this variable.
  94. Remember that this initial
    value must be of a valid type
  95. according to this data type.
  96. In this case only integers
    are allowed in this variable.
  97. 2 is a valid integer so it's okay for
    it to go inside this variable.
  98. And last but not least,
    we have this ; at the very end.
  99. So to summarize,
    when you declare a variable,
  100. you need to specify the data type,
    followed by the variable name,
  101. and then an equal sign, and then
    an initial value, and then a semicolon.
  102. In this case, we're creating
    a variable called numberOfCoffees.
  103. The data type is int, so only integers
    can be stored in this variable.
  104. And we're assigning the value 2 to be
    the initial value of this variable.
  105. Or you can change the variable name
    according to the rules we talked about.
  106. And you can change the initial value
    as long as it's the same data type.
  107. You can't change this data type,
    it needs to be spelled int, I-N-T.
  108. We're going to be learning
    about other data types as well,
  109. and those have to be
    spelled a specific way.
  110. Now any time you need to use a variable,
    just refer to it by it's name, and
  111. then it will be swapped out for
  112. the actual value later when
    the app is actually running.
  113. Now I want you to go ahead and
  114. add these lines of code to your app,
    please don't copy and past though,
  115. because I want you to practice typing
    out, creating and using a variable.
  116. Then go ahead and
    run the app on your phone.
  117. Make sure that when you hit the Order
    button, this is what you see.
  118. After that, I want you to do
    a little bit of experimenting.
  119. Try to assign a new initial
    value to the variable.
  120. Then go ahead and
    try to assign a new variable name.
  121. Instead of numberOfCoffees,
  122. try something else like quantity for
  123. A common beginner mistake is to think
    that you typed it out correctly
  124. because your eyes are scanning over
    the whole thing really quickly.
  125. But if you look individually
    at every single character you
  126. would notice that there
    might be a problem.
  127. This will take you some time to
    finish but take your time and
  128. pay close attentions to each
    character that you type.
  129. If you run into problems,
    try reading the error message.
  130. For example, this error message says
    cannot resolve symbol numberOCoffees.
  131. Java doesn't recognize
    what numberOCoffees is
  132. because the variable name was
    actually numberOfCoffees.
  133. Hopefully you can figure out from
    the error or you can hit undo and
  134. that sometimes resolves
    the error as well.
  135. Or you can go back to code that you know
    works, like the code that we gave you.
  136. When you're done,
    check these boxes and continue.