Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,880 --> 00:00:05,960
We can use variables to store values in the getting started section, you learn to create and run a
2
00:00:05,980 --> 00:00:10,750
job, a program you ran, a fairly simple program that prints Hello, Java.
3
00:00:12,100 --> 00:00:16,120
Now, one more useful program is able to store information, and that's why in this lesson, you're
4
00:00:16,120 --> 00:00:19,030
going to learn to store values inside variables.
5
00:00:21,940 --> 00:00:27,610
First thing I'll need you to do is create a new Java project by yourself and call it Section two, then
6
00:00:27,610 --> 00:00:28,840
launch it from voice code.
7
00:00:31,700 --> 00:00:37,190
And inside the project, critical name variables Jebba and make sure the variables class has the main
8
00:00:37,190 --> 00:00:37,580
method.
9
00:00:44,870 --> 00:00:46,340
All right, what is a variable?
10
00:00:48,040 --> 00:00:49,780
A variable can store value.
11
00:00:52,060 --> 00:00:57,790
Think of a variable as some kind of box, each variable has a name, so I'll call this one food.
12
00:00:59,240 --> 00:01:02,990
This variable, this quote unquote, box can store values.
13
00:01:04,310 --> 00:01:05,720
This one store is a tax value.
14
00:01:07,360 --> 00:01:13,690
But it can also store an integer value, by the way, integer means hole number, the take home message
15
00:01:13,690 --> 00:01:19,000
is that a variable can store any type of value instead of make a variable in Java, you need to include
16
00:01:19,000 --> 00:01:25,870
three pieces of information, the type of value we can store, the name of the variable and the value
17
00:01:25,870 --> 00:01:26,410
itself.
18
00:01:27,650 --> 00:01:34,400
And to this code creates a variable called people, the variables of type int so we can only store numbers
19
00:01:34,400 --> 00:01:35,600
like the number two.
20
00:01:38,870 --> 00:01:45,140
Some inside main making into variable named people and set it equal to 20 different people.
21
00:01:46,250 --> 00:01:50,210
Is equal to 20 and don't forget your semicolon.
22
00:01:50,870 --> 00:01:53,780
Remember that every line of code in Java needs a semicolon at the end.
23
00:01:53,930 --> 00:01:55,640
Otherwise, your code is not going to run.
24
00:01:56,900 --> 00:01:59,030
And so now this variable stores the number 20.
25
00:02:02,730 --> 00:02:07,500
Now, I want to talk about the convention to naming variables and there's a specific way to name them,
26
00:02:07,500 --> 00:02:09,810
and it's called Lower Kemel case.
27
00:02:12,670 --> 00:02:15,760
In Java, the variable needs to start with a lowercase letter.
28
00:02:17,700 --> 00:02:23,430
The variable people starts with a lower case P, but what if we have more than one word like people
29
00:02:23,430 --> 00:02:24,070
on bus?
30
00:02:24,930 --> 00:02:29,970
In that case, you'll start with a lower case letter, but the words that follow must be upper case.
31
00:02:31,140 --> 00:02:33,510
Notice that people start to they lower case P.
32
00:02:34,350 --> 00:02:36,810
And on bus, half capital letters.
33
00:02:40,120 --> 00:02:45,150
This right here is the lower camel case format, and that's how you're supposed to name variables,
34
00:02:46,180 --> 00:02:51,310
if in my code I decide to rename this variable to people on Busse, I would have to use the lower Camil
35
00:02:51,310 --> 00:02:56,920
case format, people on bus, yet people on buses.
36
00:02:56,920 --> 00:03:02,380
Dulong I prefer saying passengers, so I'll rename the variable to passengers.
37
00:03:03,840 --> 00:03:10,470
Remember to write class names in Camil case, where each word is capitalized and joined together and
38
00:03:10,470 --> 00:03:16,530
always write variable names and lower camel case were the first word is lowercase, but the words that
39
00:03:16,530 --> 00:03:17,970
follow are capitalized.
40
00:03:18,690 --> 00:03:23,310
I highly encourage you to follow these conventions because it's what the professionals do and it makes
41
00:03:23,310 --> 00:03:25,080
your code clearer and easier to read.
42
00:03:28,620 --> 00:03:33,450
OK, so we talked about variables being able to store values, but you can actually print the value
43
00:03:33,450 --> 00:03:34,320
inside a variable.
44
00:03:35,520 --> 00:03:39,240
You created a variable called passengers and it stores the value 20.
45
00:03:40,020 --> 00:03:44,430
So using print line, you can print the variables value to the console system.
46
00:03:48,360 --> 00:03:50,310
Print line passengers.
47
00:03:55,170 --> 00:04:00,930
Now, you can go ahead and open up the terminal, compile your code using job C variables Java.
48
00:04:02,680 --> 00:04:06,100
And run the compiled code using Java variables.
49
00:04:06,970 --> 00:04:09,700
Sure enough, the value inside passengers is 20.
50
00:04:11,620 --> 00:04:16,420
Now, here's a similar code where I didn't name passed in your stores the value five instead of 20,
51
00:04:17,230 --> 00:04:19,600
then you printed the value inside passengers.
52
00:04:24,180 --> 00:04:30,600
Java is strongly typed once you define the valley type or verbal can only store values of that type.
53
00:04:31,560 --> 00:04:37,680
For example, you cannot stop text in an integer variable that's illegal, the value that you store
54
00:04:37,680 --> 00:04:39,930
needs to be consistent with the variable type.
55
00:04:41,370 --> 00:04:45,400
You try to store value that is different from the variable type, you will get a type mismatch.
56
00:04:46,540 --> 00:04:48,820
In other words, your codes not going to compile.
57
00:04:51,390 --> 00:04:55,800
So back to my code, I'm going to try to set passengers equal to some.
58
00:04:57,140 --> 00:04:58,700
Text value.
59
00:05:01,470 --> 00:05:02,840
Let's try to Kampala's.
60
00:05:08,200 --> 00:05:13,810
And I get an error in line three of my code and it says that string cannot be converted to it.
61
00:05:15,040 --> 00:05:18,700
This error is a result of trying to store text in an integer variable.
62
00:05:20,130 --> 00:05:24,900
And invariable can only store integer values like five, you cannot store text.
63
00:05:27,600 --> 00:05:33,900
So compiling this code and running it were good, by the way, in case you forgot, the reason I'm able
64
00:05:33,900 --> 00:05:39,180
to run these terminal commands so quick is because I'm using the up key to access previous terminal
65
00:05:39,180 --> 00:05:39,810
commands.
66
00:05:40,680 --> 00:05:44,850
You would have already seen this in the workbook one point one, as well as your very first quests.
67
00:05:46,750 --> 00:05:52,360
In any case, Java is case sensitive, which means Java discriminates between uppercase and lowercase
68
00:05:52,360 --> 00:05:57,610
letters, for example, passengers is not the same as capital passengers.
69
00:06:00,510 --> 00:06:05,730
I try to compile this, I would get an error in line four of my code and it tells me that Java does
70
00:06:05,730 --> 00:06:09,310
not recognize the variable capital passengers because you never made one.
71
00:06:09,720 --> 00:06:12,320
You must always stay consistent with letter cases.
72
00:06:12,990 --> 00:06:15,340
So we're going to fix this variable back to what it was.
73
00:06:16,470 --> 00:06:17,070
All right.
74
00:06:20,940 --> 00:06:26,700
Finally, you can set variables equal to each other if you set a variable equal to another, the variable
75
00:06:26,700 --> 00:06:30,240
copies of the value inside the first one, just like you're seeing here.
76
00:06:33,650 --> 00:06:38,960
So make an integer variable name bus tickets, remember that you need to write variables in lower camil
77
00:06:38,960 --> 00:06:44,760
case lower case B in the words that follow must be upper case bus tickets.
78
00:06:45,920 --> 00:06:49,650
Now, let's assume to enter the bus, every passenger needs one bus ticket.
79
00:06:50,330 --> 00:06:57,560
So there are as many bus tickets as there are passengers and bus tickets is equal to passengers.
80
00:07:01,760 --> 00:07:07,300
Now we're going to check the value inside bus tickets by printing it system dot out, dot print line
81
00:07:07,300 --> 00:07:15,040
bus tickets, I would expect this to print five, but let's double check Gervasi variables that Gever.
82
00:07:17,650 --> 00:07:23,720
Job of variables and as expected, it prints five bus tickets and five passengers.
83
00:07:24,790 --> 00:07:25,490
Great.
84
00:07:25,510 --> 00:07:27,160
Here's the code we have so far.
85
00:07:30,470 --> 00:07:34,970
By setting the bus tickets variable equal to passengers, it copies the value inside.
86
00:07:40,570 --> 00:07:46,210
In this lesson, you learn to store values inside variables, you stored the integer value five inside
87
00:07:46,210 --> 00:07:49,990
passengers and then you print it out using the print online function.
88
00:07:52,200 --> 00:07:55,320
You also learn that variables can only store values of their type.
89
00:07:56,220 --> 00:07:59,250
For example, you cannot store text in an integer variable.
90
00:08:00,330 --> 00:08:03,570
The last thing you learned was the set variables equal to each other.
91
00:08:04,350 --> 00:08:08,640
You set the bus ticket variable equal to passengers and it copied the value inside.
9687
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.