We can use Ent. and long variables to store whole numbers.
You've already used in variables to store small hole numbers, but what if I told you that in variables
have a limit on the range of numbers that they can store?
They can only store numbers below the two billion range, two billion and hundred and forty seven million,
to be exact.
So in this lesson, you're going to learn the difference between Entin long as well as when it's appropriate
to use as opposed to long.
The first thing I'll need you to do is create a new class by yourself inside the section to project
create a new file named Hole Numbers, Dot Java and make sure the whole numbers class has the main method.
The invariable type stores, whole numbers you've already used into our first lesson on variables,
but it never hurts to review, you need to include three things when defining an end variable, the
variable type int such that this variable can only store in values.
You need to include the variable name and the integer that you want to store.
This code creates a variable called people.
Which store is an integer value of six.
You've seen this stuff before, but there's another thing you should know about in variables, they
have a range in variables.
Can store numbers between negative two billion and positive two billion.
So when the whole numbers class, we're in to create variable named no offense.
And number of fans.
Is equal to 5000.
We're going to print the number of fense system, dot out, dot print line, no offense.
And you know, what we're actually printed as part of a sentence will say there are break the string,
insert the no offense value into the string and we'll reconnect the string to more text.
Fans at the Quidditch Games.
Don't forget that white space where necessary.
And now we can compile our code.
And run its.
And there are 5000 fans at the Quidditch games, the variable number of fans stores, the Valley 5000,
and we printed the value from within a drink.
Now, five thousand is a relatively small number, what happens if we try to store the global population
as an it so into global population?
Is equal to seven billion.
That would be nine zeros.
And I know this code's not going to comply right now, but let's print the value anyway as a part of
a string system that our print line.
Break the string, insert the global population value.
And reconnect the strength to say people on earth.
I'm going to compile my code, and to no surprise, the code doesn't compile the cynosure value is too
large to store, remember, and variables can only store numbers inside the two billion range beyond
this range.
Java throws an error.
So if it can't handle large numbers, what do we do now?
Well, it's for this exact reason why we have the long type, the long type can store a large hole numbers.
And we need to include three things to define a long variable, the long type, of course, and the
long type gives a variable the capacity to store very big numbers, the name of your variable, as always,
and a long value.
Notice the L the reassures Java that you're using long to store big numbers.
This code creates a variable called population in the variable stores along value of seven billion eight
hundred million.
But like int even long variables have a range.
The long type can numbers that reach up to nine quintillion, that's more than enough space to store
the global population or any number of things, really.
It's a very big number.
Nine quintillion is a lot.
So we'll rewrite the global population as a long variable.
Long global population is equal to seven billion.
Now, if you were to compile this code, you'd still get an error.
The compiler isn't sure how you intend to store the big number.
So don't forget to add the URL.
This basically tells Djavad, don't worry, I'm going to use the long type to store this big number
and it seems like that's going to work.
The warning is gone.
So when I compile this code and run it and awesome.
Now you know how to store large numbers in Java.
The variable global population stores the value seven billion which you printed from within a string.
And you might be thinking, spare me, when will I ever need long?
Honestly, I barely use it because the numbers I work with tend to be inside of a reasonable range.
But, you know, it can be relevant.
Let's assume for whatever reason, you want to store the number of daily Google searches, which happens
to be five billion.
So if I tried to write in daily Google searches.
Is equal to five billion.
In this case, five billion is not a number we can start with, and so job is going to force you to
use long.
So we're going to change the data typed along and add the L to reassure Java that, yes, we are going
to use long to store this very big number.
And while we're at it, let's print this number from within a string.
There are break the string.
But the number of Google searches value daily, there are X amount of Google searches.
OK, compile the code.
And we're looking good.
Now, you might be asking, why not always use long, the long type can store a bigger range of numbers,
so why not always use it?
Once again, this goes back to performance and memory.
Let's say you're selling a house and you need to move your stuff.
And one box is the size of a few bucks while the other box is the size of a truck.
If you want a place, a couple of T-shirts in one of the boxes, which box do you put your T-shirts
and.
Well, a sensible person would choose the small box because the less space a box takes up, the easier
it's going to be to move around.
And in the context of code, saving memory can make your application perform better.
So into variables only take up four bytes in memory because they store a smaller range of numbers.
But long variables need the capacity to store bigger numbers, so they take up eight bytes.
So if you want your Java program to perform better, use it to store small numbers inside the two billion
range.
And in the very rare case that you need to store very large numbers, then use long and for the same
reasons you would use car to store single characters, not string.
So in this video, you created a and long variables into variables can only store values inside the
two billion range, whereas long variables have the capacity to store much larger numbers.
So we asked ourselves, why not always use long while enough variables are faster and take up less memory?
The number is small enough to use and then use int.
First, you made an into variable that stores the value 5000 and you printed the value from inside a
string.
Then you tried to store of value of seven billion inside an invariable Angela through an error when
the number starts to exceed the billions we have to use long.
So we ended up using long to store the global population and everything worked out accordingly.