We need to store values in our programs. These values can change based on the
input from the program. And since the values can change
we need a symbol to represent them. We call that symbol a variable.
So what I wanna show you with this Java variables tutorial,
is what are variables in programming and how to use variables in a Java program.
In this lesson, we're going to look at
what is a variable, and how do we create them. We've done this before but
we've never called it out. We'lll also introduce a very important concept the
you'll see often in this course,
immutability. We'll also look at constants,
how they differ from variables and immutable variables. And finally
we'll learn about casting variables to other types. So let's start with
what is a variable. We've already seen variables
although we weren't calling them by that name. When we define primitive values we
always use the pattern
type, name, and value. The name is a variable in Java.
Just like in algebra, a variable is a placeholder for the value.
In this case X is our variable. Just looking at it
we know X represents one. If
1 + X was 43, we'd know that X
represented 42. You can think of a variable as a mailbox.
Pretend a mailbox can hold one piece a mail. Every box has a number.
In Java, a mailbox address is called the reference.
You'll rarely ever need to know the actual reference value.
That's because we give variables a name. You just need to know the name of the
variable you create.
A variable can be created to hold any primitive. That isn't to say
"a variable" can "hold any primitive". When you define the variable with a
type,
that means but the life for the program variable is of
that type. So once created an int,
it is always an int. You can have a variable hold other references.
We'll cover that later on when we cover other Java types. Here
we have an integer named 'i', a double
variable named 'd', and a boolean variable named 'b'.
So the first part of any variable declaration is the type.
In this case, int is the type.
The name of symbol is for the variable. Now
'i' is not a very good name. You want to give your variables a descriptive name,
so it's clear
what the value represents. Most IDEs auto complete your variable names,
so you're not saving any typing by using short names. So
my 'i' variable here really probably should be something like countOfWidgets numberOfDays
or
mySpecialNumber. Just something that's clear and obvious to you.
As you saw in previous lessons, you can initialize your variable to a value
like this.
This is optional. You can provide a value later,
but you should never use a variable until you know that you have initialized
it.
For most cases, a variable will be initialized to 0
automatically. In some cases which will cover later
it's an error to use the variable before initializing it.
If you know the value when you're creating your variable, go ahead and
add it.
You can also set a variable to the value of another variable.
That looks like this. Ok
Another thing to do is, if you know your variable will never change will life of
the variable,
you want to make it 'final'. It's like the game show Who Wants to Be a Millionaire.
When you add the keyword 'final' you're saying this is your final value.
It's not going to change. When a variable can't change,
it said to be 'immutable'. You'll want to remember that word.
It's gonna come often come up often in this class. Immutable means
value cannot change once it's set. Normal variables can change is often as you
need.
So you're probably wondering
where's the value in this example? Well you can define it like we have already.
You can assign it to the value you want when you create the immutable variable.
You can also define a final variable and the delay the variable assignment to later
in the code.
In this course when you see a block like the blue in here,
that means there's a lot more Java code here,
but the exact code is not relevant to the discussion.
So in this example we're setting the variable value
much later after the definition. Still
you get only said it once. Let's try this in code.
We're going to define a variable called handCount.
We have two hands. Now look what happens when we try to change the value.
we get the error cannot assign a value
to the final variable handCount.
Now we may not know the number of hands in advance,
but once we do it shouldn't change. We can modify the code to declare the variable
then we'll add another dummy variable in between. Now we'll assign
two to the handCount. And that works!
Now I try to change handCount again on the next line
will get the error again. You'll want to use final
with every variable the you create that you know
will not change. This allows Java to make some optimizations
and possibly make your application much faster. Sometimes there's a case where
you know the variable will never ever change.
Like the constant pi. Its always going to be 3.1415... It's not gonna change
in your program ever. For this you'll want to use a constant.
Now it would have been nice if Java used a keyword like
Const, but they didn't. They
reserved the keyword but it doesn't do anything.
No, to declare a constant you need to use the keyword 'static'
with the keyword 'final'. 'static final' means
constant in Java. 'static' really means set
at compile-time, but in this case, think of 'static final'
as meaning constant. One thing to note
when you create a 'static final' constant, it must be created outside of a method.
It's something we'll go into a little more detail later, but
you can create a constant inside a method. If you know your variable value
will never change
ever, tell Java it's a constant. The difference between an immutable variable
and constant is, you know the value of the constant at compile-time.
An immutable variable isn't known until runtime.
Now I said earlier, once you give a variable type you can't change it.
That's because Java is a statically typed language.
This means the value is typed at compile-time. You cannot change it,
and if you want it treated as a different type, you have to explicitly tell Java to
do so.
The opposite of statically typed is dynamically typed.
That means a variable can change type at runtime. An example language would be
Perl. The reason Java chose to be statically typed
is because you can detect many stupid errors
at compile-time. I say stupid because you never want to change the type of a
variable.
If you change it, that's likely a typo error.
There are cases where you want to change the type.
For example, you have a short but
you trying to use it somewhere that expects a long. You can tell Java "Hey!
pretend this integer is a long" using something called casting. When you go from a smaller type
to a bigger type
you're making a widening conversion. This always works for the number types
so if you start with the type somewhere in these arrows, and move to a type to the
right
Java's going to do it without complaint. The other direction is a different story.
For example if you start with a long, and then try to put it into a short,
It doesn't matter if the number is small enough Java is going to give you a compile error.
You get the lossy conversion error we saw in previous lessons. Going from bigger to
smaller
is called a narrowing conversion. Let's look at this in code.
We'll create a long value and call it meaningOfLife.
will assign the value 42 and we'll create a short
called shortVersion and try to give it the value
meaningOfLife. And Java complains. We can't compile.
So what do we do? When you know it's okay, you can tell Java
"Hey man! I know what I'm doing here". We do that with a cast
To cast a variable, we put the type we want in parentheses.
So in this case, we're casting our long to a short.
Let's go fix our code.
So this won't compile. But if we add cast a short
it works. We're telling Java
we're in control. Now this also means Java is giving you enough
rope to hang yourself. Let's see what happens if we try to put a really big
number into a short.
And we run it and we get a result that
really isn't what we wanted. Basically Java accepted that we know what we're doing.
and
tried to stuff it in there anyway. I didn't work out. So we get the bits that did
fit.
Here's one other thing to show you. Remember that integer types
and floating point types are stored differently. Even though they both into
32-bits
they are represented differently and have different ranges.
so if we take a big integer like
1234567890
it works. Java won't even protest.
So let's print out the difference between the two. The answer should be zero.
and we have -46. Lesson learned.
Be careful with converting between integer and floating-point numbers.
So that wraps up variables. We've looked at how to define them,
how we can make them immutable, and how we can make them a constant.
Hey thanks for watching the video. There is a quick quiz for this on DeegeU.com
if you'd like to gauge how much you learned.
If you like the videos you are seeing, please let me know by liking the video and
hitting the subscribe button to the DeegeU channel on YouTube. I'd
really appreciate that! If you have concerns or questions please leave them
in the comments below or on DeegeU.com.
There's a poll on the front page of DeegeU.com so
you can let me know what topic is covered next. Thanks for watching and see you
in the next video!