Tim: All right, so let's introduce two other data types
or, more correctly, primitive types in Java,
the byte and the short.
And we'll introduce a third one later on.
So I'm gonna start by closing down the run window.
You can click on this little option down there to do that.
And I'm going to start typing after line 16,
byte this time, B-Y-T-E,
and we'll call it my min byte value.
We're gonna do the same thing we've done for integer,
but for byte.
We're gonna make that equal to, this time
instead of integer, we're going to use the word byte,
with a capital b, with a period,
and min underscore value, semicolon.
We'll do the same for the maximum value.
My max byte value is equal to byte,
with a capital b, period.
This time max underscore value.
That's in print that matters, we've done above,
and we'll put byte,
minimum value is equal to closing
double quote plus then our my byte min value.
And we'll take a copy of that line again.
Paste, and we'll just change this to maximum.
Then we need to change the variable on our line 21
to be max as well.
All right, so let's run this,
and we can see what the minimum
and maximum ranges are for a byte.
Now you can see that in that case for a byte,
the range is quite small.
The minimum value you can go down to is minus 128,
and the maximum value is 127.
So the range is quite small,
and, frankly, you probably won't be using
the byte data type a lot.
Now maybe if you had a requirement to store
a lot of numbers that are within that range,
and you wanna save memory,
but also could be a good idea if you were doing
that to use the byte instead of, say, an int.
If you wanted the fact that it were
a small range documented.
So using a small data type is actually code documentation,
in the sense that someone would read the code,
and then note that you're using a data type,
in this case that is quite small,
and that there was a specific reason for it.
But with that said, today's modern computers mean
that you perhaps can be a little bit lazy with memory,
and not worry too much about space,
and how much it's being used.
But keep in mind, there can also be
performance reasons that come into account.
A smaller data type takes up less space,
and can be quicker to access.
But, in general, another thing that probably won't affect
you much due to the speed of today's computers,
but certainly was a factor when Java was first created.
All right, so that's the byte data type.
So let's create another one now.
The next one we're gonna look at,
the next primitive type is our short value.
So let's do the same thing with that.
We're gonna select that.
I'm taking a copy of the full lines there,
and I'm going to paste the code into there.
All right, now we've got an error there,
and that's because we're reusing
the same variable names.
So let's actually use the Search and Replace functionality
built into IntelliJ to make these changes
for us automatically.
I'm gonna go to Find, Replace.
All right, so first things first.
I'm gonna change byte with a capital b.
I'm gonna change that to short with a capital s.
So I'm gonna make sure that to Match case is checked there,
so we're only updating the ones that match
precisely with the case, replace all.
Then what I'm going to do is come back here,
and replace byte with a lower case b,
with short with a lowercase b,
and do a oops, and do a replace all for that as well.
And that should be our code change.
So basically we've just swapped out the word byte
for the word short.
And we should be able to now print out those values.
So let's do that and see what range
a short can contain.
We can see the short minimum value is minus 32768,
the maximum value for a short is 32,767.
So both the byte and the short
have the same overflow and underflow issue.
But obviously with their own range of numbers.
So let's have a brief talk about how much
space each of the data types we've talked to take
up in memory.
All right, so I wanna talk about the size
of primitives types, and what width means.
So a byte occupies eight bits.
Now a bit is not directly represented in a primitive type.
We have a Boolean, which is not really the same thing,
and that we'll discuss in a future video.
But we could just assume that a byte occupies eight bits.
Now we can say, therefore, that a byte has a width of eight.
Now a short, on the other hand, we saw that it can
store a larger range of numbers,
and for that reason it occupies 16 bits,
and has a width of, therefore, 16.
Now an integer has a much larger range,
as we saw again, compared to the byte and the short.
While that occupies 32 bits, it has a width of 32.
So the point here is that each primitive type
occupies a different amount of memory.
We can see that an int now needs four times
the amount of space that a byte does, as an example.
Now it's not particularly relevant
for you to know these numbers,
but suddenly they could come up
as an interview question,
and it's useful to know, in general,
that certain data types take up more space than others.
All right, so there's one more data type,
and that's of use for you where you've got a number
that you wanna process
that's larger than the amount for an int.
So we call that one the long.
So let's go ahead and add some code for that.
So we just type long my long value is equal to 100.
Now technically what I've done there is wrong
because when you're assigning a long value,
you need to put the letter l on the end of it,
and that tells the computer that it's a long value.
So I can go ahead and do something like this,
we can put an l there,
and I can see that if you hover over it,
"Long literal ends with lowercase l."
And that actually tells that could be confusing
as lowercase l looks very similar to a one.
So for that reason, when you're doing it,
I suggest you always use the uppercase l
to make it clearly stand out, as we've done here.
So a long has got a width of 64,
which is obviously double the size of an int,
which we established had a width of 32.
So it means it's double the width of an integer.
So the actual number we can store
is huge, though, in a long primitive type.
It's actually two to the power of 63.
So let's grab the code we've done for short,
and do a similar thing that we've done previously.
We're gonna copy it.
I'm gonna paste it under our long definition,
our declaration for my long value, on line 28.
And we'll do a copy, a Search and Replace there.
And it's Control + R we saw last time with the shortcut.
So firstly I'm gonna do it with short,
replace that with long with a capital l,
making sure Match case is selected, replace all.
And we're gonna do the same for short as well,
so short to long, same thing there, replace all.
All right, so now that've done that,
let's just close down that Search and Replace,
and right click and run this.
And let's see how big the range of numbers can be.
Now I just may need to scroll this up a little bit,
contrast to the size of an integer,
and it's significantly larger, as you can see.
It's a pretty big range of numbers there.
Now, yes, it has got the same type of overflow
and underflow problem we've discussed
for the other primitive types.
Now just minimise this again,
we're gonna go down, and we'll close it off.
So, by default, Java considers a number you type
as an integer, unless you add a letter on the end,
as we did on line 28.
This forces Java to treat that number as a long.
But you might be wondering, though, why the long
declaration worked without an l.
After all, when we first started, we didn't put an l there.
There you can see clearly we haven't got an error
on line 28 by removing the l.
Well the reason is that an int fits fully into a long
because obviously a long is twice the width.
So Java is smart enough to know that,
and just converts the int to a long for us,
instead of giving us an error.
However, if we try and type in a literal value
for a long without the l, we will get an error.
So we know that previously we've used
the maximum range there.
So if I copy this maximum int test number there,
which we know was the maximum number.
So I come down here and create a new variable,
we'll call it long big long literal value equals...
Firstly if we do that, we put a semicolon on there,
and that works, we know that that's the maximum
integer number, and so IntelliJ is
quite happy to accept that.
But if we change that, and put in underscore 234,
suddenly we've got an error there.
And we can see there, that's the hint,
"Integer number is too large."
So clearly Java is telling us that it's still treating
that number as an integer.
And you can see that's why the default whole number
is an integer in Java.
So we've put an l on the end there to treat it as a long,
and, you can see, suddenly, that number has been accepted.
And just to confirm that, we can then print that out.
Let's just go ahead and do that, do an s out,
big long literal value.
And if we run that code,
see down the bottom, our largest number
clearly is correct.
It's got the 234 on the end,
and I'm not gonna attempt to read that number out.
Then just before we finish the video,
Java's also smart when it comes to converting
numbers from int to, say, a short.
So it knows if the literal value we're going to use
is not going to fit into a short.
So to see what I mean there,
if I add this code, if I type in short,
big short literal value is equal to 32768.
We established that 32767 was the maximum number,
and we could have a look at our code there,
short maximum value is 32767.
So clearly we've got an error there,
and it's saying, "Incompatible types, required
"a short, found an int."
But if we change that to a seven,
which we know is valid, the error is just disappeared,
and Java is quite happy with that
because it knows even though the number is an int
which potentially is larger than the maximum
that can fit in a short,
in this particular case, the literal number
we've entered doesn't exceed that,
and it can actually fit directly into a short.
Right, so let's finish the video here.
And in the next one, we'll start talking
about a bit of arithmetic using these data types,
and then we'll talk about another important
concept called casting.
So I'll see you in the next video.