Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,510 --> 00:00:03,780
We can use Ent. and long variables to store whole numbers.
2
00:00:05,510 --> 00:00:10,550
You've already used in variables to store small hole numbers, but what if I told you that in variables
3
00:00:10,550 --> 00:00:13,070
have a limit on the range of numbers that they can store?
4
00:00:13,860 --> 00:00:19,400
They can only store numbers below the two billion range, two billion and hundred and forty seven million,
5
00:00:19,400 --> 00:00:20,360
to be exact.
6
00:00:22,380 --> 00:00:27,000
So in this lesson, you're going to learn the difference between Entin long as well as when it's appropriate
7
00:00:27,000 --> 00:00:29,070
to use as opposed to long.
8
00:00:30,280 --> 00:00:35,140
The first thing I'll need you to do is create a new class by yourself inside the section to project
9
00:00:35,620 --> 00:00:41,440
create a new file named Hole Numbers, Dot Java and make sure the whole numbers class has the main method.
10
00:00:48,210 --> 00:00:53,240
The invariable type stores, whole numbers you've already used into our first lesson on variables,
11
00:00:53,250 --> 00:00:59,130
but it never hurts to review, you need to include three things when defining an end variable, the
12
00:00:59,130 --> 00:01:03,660
variable type int such that this variable can only store in values.
13
00:01:04,190 --> 00:01:07,300
You need to include the variable name and the integer that you want to store.
14
00:01:08,100 --> 00:01:10,200
This code creates a variable called people.
15
00:01:10,200 --> 00:01:12,270
Which store is an integer value of six.
16
00:01:13,300 --> 00:01:16,960
You've seen this stuff before, but there's another thing you should know about in variables, they
17
00:01:16,960 --> 00:01:19,660
have a range in variables.
18
00:01:19,660 --> 00:01:22,930
Can store numbers between negative two billion and positive two billion.
19
00:01:26,690 --> 00:01:30,890
So when the whole numbers class, we're in to create variable named no offense.
20
00:01:31,790 --> 00:01:36,050
And number of fans.
21
00:01:37,340 --> 00:01:39,590
Is equal to 5000.
22
00:01:43,510 --> 00:01:50,830
We're going to print the number of fense system, dot out, dot print line, no offense.
23
00:01:51,850 --> 00:01:58,240
And you know, what we're actually printed as part of a sentence will say there are break the string,
24
00:01:58,870 --> 00:02:04,480
insert the no offense value into the string and we'll reconnect the string to more text.
25
00:02:06,030 --> 00:02:08,250
Fans at the Quidditch Games.
26
00:02:13,700 --> 00:02:16,010
Don't forget that white space where necessary.
27
00:02:19,340 --> 00:02:21,380
And now we can compile our code.
28
00:02:26,280 --> 00:02:27,450
And run its.
29
00:02:33,050 --> 00:02:39,910
And there are 5000 fans at the Quidditch games, the variable number of fans stores, the Valley 5000,
30
00:02:40,400 --> 00:02:42,560
and we printed the value from within a drink.
31
00:02:43,610 --> 00:02:48,950
Now, five thousand is a relatively small number, what happens if we try to store the global population
32
00:02:48,950 --> 00:02:52,190
as an it so into global population?
33
00:02:56,740 --> 00:02:58,580
Is equal to seven billion.
34
00:02:59,410 --> 00:03:01,120
That would be nine zeros.
35
00:03:03,920 --> 00:03:08,420
And I know this code's not going to comply right now, but let's print the value anyway as a part of
36
00:03:08,420 --> 00:03:12,110
a string system that our print line.
37
00:03:21,950 --> 00:03:25,580
Break the string, insert the global population value.
38
00:03:27,200 --> 00:03:30,070
And reconnect the strength to say people on earth.
39
00:03:38,410 --> 00:03:45,790
I'm going to compile my code, and to no surprise, the code doesn't compile the cynosure value is too
40
00:03:45,790 --> 00:03:52,510
large to store, remember, and variables can only store numbers inside the two billion range beyond
41
00:03:52,510 --> 00:03:53,180
this range.
42
00:03:53,200 --> 00:03:54,450
Java throws an error.
43
00:03:55,090 --> 00:03:58,420
So if it can't handle large numbers, what do we do now?
44
00:04:01,800 --> 00:04:07,800
Well, it's for this exact reason why we have the long type, the long type can store a large hole numbers.
45
00:04:09,870 --> 00:04:15,630
And we need to include three things to define a long variable, the long type, of course, and the
46
00:04:15,630 --> 00:04:22,079
long type gives a variable the capacity to store very big numbers, the name of your variable, as always,
47
00:04:22,079 --> 00:04:23,490
and a long value.
48
00:04:24,240 --> 00:04:29,400
Notice the L the reassures Java that you're using long to store big numbers.
49
00:04:30,180 --> 00:04:35,940
This code creates a variable called population in the variable stores along value of seven billion eight
50
00:04:35,940 --> 00:04:36,740
hundred million.
51
00:04:37,560 --> 00:04:40,890
But like int even long variables have a range.
52
00:04:42,060 --> 00:04:47,520
The long type can numbers that reach up to nine quintillion, that's more than enough space to store
53
00:04:47,520 --> 00:04:50,750
the global population or any number of things, really.
54
00:04:50,940 --> 00:04:52,110
It's a very big number.
55
00:04:52,710 --> 00:04:54,350
Nine quintillion is a lot.
56
00:04:55,170 --> 00:04:58,230
So we'll rewrite the global population as a long variable.
57
00:04:59,180 --> 00:05:02,220
Long global population is equal to seven billion.
58
00:05:03,380 --> 00:05:06,260
Now, if you were to compile this code, you'd still get an error.
59
00:05:06,540 --> 00:05:09,800
The compiler isn't sure how you intend to store the big number.
60
00:05:10,340 --> 00:05:11,870
So don't forget to add the URL.
61
00:05:12,470 --> 00:05:17,480
This basically tells Djavad, don't worry, I'm going to use the long type to store this big number
62
00:05:18,620 --> 00:05:20,030
and it seems like that's going to work.
63
00:05:20,090 --> 00:05:20,980
The warning is gone.
64
00:05:20,990 --> 00:05:26,420
So when I compile this code and run it and awesome.
65
00:05:26,420 --> 00:05:28,880
Now you know how to store large numbers in Java.
66
00:05:29,780 --> 00:05:35,330
The variable global population stores the value seven billion which you printed from within a string.
67
00:05:37,470 --> 00:05:40,660
And you might be thinking, spare me, when will I ever need long?
68
00:05:41,550 --> 00:05:47,390
Honestly, I barely use it because the numbers I work with tend to be inside of a reasonable range.
69
00:05:48,030 --> 00:05:49,950
But, you know, it can be relevant.
70
00:05:50,610 --> 00:05:56,760
Let's assume for whatever reason, you want to store the number of daily Google searches, which happens
71
00:05:56,760 --> 00:05:57,780
to be five billion.
72
00:05:58,650 --> 00:06:02,760
So if I tried to write in daily Google searches.
73
00:06:08,730 --> 00:06:10,680
Is equal to five billion.
74
00:06:11,810 --> 00:06:17,330
In this case, five billion is not a number we can start with, and so job is going to force you to
75
00:06:17,330 --> 00:06:18,080
use long.
76
00:06:19,140 --> 00:06:24,600
So we're going to change the data typed along and add the L to reassure Java that, yes, we are going
77
00:06:24,600 --> 00:06:26,630
to use long to store this very big number.
78
00:06:27,900 --> 00:06:31,530
And while we're at it, let's print this number from within a string.
79
00:06:33,330 --> 00:06:36,120
There are break the string.
80
00:06:38,020 --> 00:06:44,050
But the number of Google searches value daily, there are X amount of Google searches.
81
00:06:47,280 --> 00:06:49,860
OK, compile the code.
82
00:06:54,080 --> 00:06:55,340
And we're looking good.
83
00:06:59,150 --> 00:07:04,550
Now, you might be asking, why not always use long, the long type can store a bigger range of numbers,
84
00:07:04,550 --> 00:07:06,160
so why not always use it?
85
00:07:07,010 --> 00:07:09,900
Once again, this goes back to performance and memory.
86
00:07:10,730 --> 00:07:13,280
Let's say you're selling a house and you need to move your stuff.
87
00:07:14,090 --> 00:07:19,210
And one box is the size of a few bucks while the other box is the size of a truck.
88
00:07:20,180 --> 00:07:25,250
If you want a place, a couple of T-shirts in one of the boxes, which box do you put your T-shirts
89
00:07:25,250 --> 00:07:25,460
and.
90
00:07:26,490 --> 00:07:34,470
Well, a sensible person would choose the small box because the less space a box takes up, the easier
91
00:07:34,470 --> 00:07:35,700
it's going to be to move around.
92
00:07:36,360 --> 00:07:41,470
And in the context of code, saving memory can make your application perform better.
93
00:07:41,880 --> 00:07:47,670
So into variables only take up four bytes in memory because they store a smaller range of numbers.
94
00:07:48,330 --> 00:07:54,640
But long variables need the capacity to store bigger numbers, so they take up eight bytes.
95
00:07:55,500 --> 00:08:01,440
So if you want your Java program to perform better, use it to store small numbers inside the two billion
96
00:08:01,440 --> 00:08:01,860
range.
97
00:08:02,520 --> 00:08:09,720
And in the very rare case that you need to store very large numbers, then use long and for the same
98
00:08:09,720 --> 00:08:13,800
reasons you would use car to store single characters, not string.
99
00:08:16,370 --> 00:08:22,070
So in this video, you created a and long variables into variables can only store values inside the
100
00:08:22,070 --> 00:08:27,410
two billion range, whereas long variables have the capacity to store much larger numbers.
101
00:08:28,130 --> 00:08:33,950
So we asked ourselves, why not always use long while enough variables are faster and take up less memory?
102
00:08:34,669 --> 00:08:37,250
The number is small enough to use and then use int.
103
00:08:39,230 --> 00:08:44,420
First, you made an into variable that stores the value 5000 and you printed the value from inside a
104
00:08:44,440 --> 00:08:44,990
string.
105
00:08:47,430 --> 00:08:53,280
Then you tried to store of value of seven billion inside an invariable Angela through an error when
106
00:08:53,280 --> 00:08:56,570
the number starts to exceed the billions we have to use long.
107
00:08:57,180 --> 00:09:02,630
So we ended up using long to store the global population and everything worked out accordingly.
10876
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.