English subtitles

← Magic Number - Intro to Java Programming

Get Embed Code
6 Languages

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

  1. Now, there's just one little blemish to the program that we've just written. It
  2. uses what programmers refer to as magic numbers. Look at this line over here,
  3. and it begs to question why 25, why 255? Those look like values that a magician
  4. picked out of a hat. Instead of using magic numbers, it's a good idea to use
  5. constants. Here is how you do this in Java. The keyword, final, indicates a
  6. number that can never be changed that has assumed it's final value. Or like we
  7. would say in math, it's a constant value. So here, are the 25, I'll call it
  8. added red and the 255 here, called max red. I defined these as variables just
  9. like you would define any other variable, except for the keyword final in
  10. front. And also, it's a convention amongst Java programmers, that constants
  11. should be written in upper case. Maybe with an occasional underscore. Now, go
  12. ahead and rewrite the statement here, using these two constants instead of the
  13. magic numbers. And put your answers into this box.