0:07 [Music]
0:10 Welcome back to Code Chef. Ever wondered
0:12 how programs remember values and decide
0:14 where they can be used? Today we'll
0:16 explore variables, scope, and constants
0:19 in Java. The foundation of storing and
0:22 protecting data in your programs. Think
0:24 of variables like labeled jars in your
0:27 kitchen. You can put sugar in one jar,
0:29 flour in another, but you can only
0:31 access them where they're placed. On the
0:35 shelf, in the pantry, or on the table. A
0:37 constant is like a jar you've sealed. It
0:40 always holds the same ingredient.
0:43 In Java, a variable is named storage for
0:46 data. Scope defines where that variable
0:48 is accessible. And a constant declared
0:50 with the final keyword can't be changed
0:54 once assigned. Scope prevents mistakes.
0:56 A variable inside a block exists only
0:59 there. Try using it outside and your
1:01 program throws an error. This keeps code
1:03 safe and organized. Here's a simple
1:06 example. We declare a variable age and
1:09 assign it the value 21.
1:11 We also declare a constant pi with the
1:13 value 3.14.
1:16 Inside an if block, we calculate adult
1:19 years as age minus 18 and print it.
1:22 Outside the block, we print pi. Step by
1:24 step, the program first assigns 21 to
1:26 age, then checks if age is greater than
1:29 18. Since that's true, adult years is
1:34 printed. Finally, pi is displayed. The
1:37 output shows adult for 3 years and pi is 3.14.
1:39 3.14.
1:41 There are also some handy features. You
1:43 can declare multiple variables in one
1:49 line like intx= 5, y= 10. And you can
1:51 use constants in calculations such as
1:54 final int days equals 7 and then
1:57 calculate total as days x times 2 which
2:00 gives 14. These features make your code
2:03 cleaner and less errorprone. Now it's
2:06 your turn. Try the beginner friendly
2:08 code chef problem. Calculate age and
2:11 constants. Write a program that
2:13 calculates your age in months using a
2:15 variable and prints a constant value
2:17 like pi. Today you learned that
2:20 variables store data, scope controls
2:22 where they're accessible, and constants
2:23 keep values fixed with the final
2:26 keyword. Keep practicing and applying
2:30 these basics. Code, debug, repeat. Build