Tim: So we talked briefly about Java keywords
in the previous section.
And we saw some common keywords,
as you can see showing in this new project file
that I've created to in IntelliJ.
And I've called it KeywordsAndExpressions.
So you can see some keywords here
public, static, void, class, and so on.
Now notice how IntelliJ helpfully colours those keywords
in a different colour to make it clear
that they are indeed keywords.
But to be clear, again, what are Java keywords?
Well, a keyword is one of around 57 keywords
in the Java language that have a specific meaning.
Now I say around because from time to time
new keywords are added to a new version of Java.
You can't use a keyword as a variable name.
And the same rule applies to methods, class names,
or other identifiers in Java.
In other words, you can't use them
to name any of those things.
And, as I mentioned a few times,
we're gonna be discussing methods, class names
and other identifiers later in the course.
So keywords are reserved words,
not to be used for any other purpose.
And there's a list of Java keywords available
at the link I'm showing on the screen there below.
But let's swing back to our browser
and go to that link and check out some of these keywords.
All right, so scrolling down on this list over here,
we can see a list of the various keywords.
And if go down and have a look,
we can see some of the keywords we've used before,
we've got boolean, byte, we've seen class before.
Here's float, the if, so on and so forth.
Now there's a lot more and by the end of the course,
you'll know what pretty well,
all of these keywords are used for
and more importantly, how to use them.
All right, so let's get back to IntelliJ.
So we've established that we can't use a Java keyword
as a variable name.
But let's try to do that anyway, just to see what happens.
So I'm gonna come into this code,
on line six I'm going to type double
int equals 50.00d, semi-colon.
Now ignoring the fact
that trying to declare a variable called int
as a double, being extremely confusing,
you can see that we actually get flagged
with an error when I try and do that.
We've got identifier expected Unexpected token.
So we can't use a Java keyword as a variable name there.
But we can use a partial name.
So I could change int to, say, int2.
And that's perfectly acceptable as a name.
So you've seen me use variable names
like myInt earlier in the course.
So I think right here is a good time
to start discussing naming conventions.
So what is a naming convention?
Well think of a naming convention
as generally a great scheme for naming things.
If you follow a convention it means
that you're using a consistent approach to naming things.
And if other programmers did the same,
it then makes the code easy to read
because everyone is using a similar way to name things.
So Java naming conventions.
So Java is a flexible language and it does allow you
to use a range of naming conventions,
or in fact, no convention at all
for your variables, methods, class names, and so on.
However, not following a convention is a bad idea though.
You should follow a convention
and get into good habits from day one
when learning Java or any language for that matter.
But since we haven't really discussed methods
and class names yet, let's just start here
by discussing naming conventions
for variables at this time in Java.
So while you can use names like myInt or variable1,
or even MYNUMBER6, in uppercase, they're not great names.
Java does have naming conventions.
And it's a good idea to adopt these conventions,
since employers will assume you know them
and will use them in practise.
So I've got a video on YouTube
which talks more about code conventions,
so check out the YouTube link in the resources section
for this video for a discussion
on code conventions in general,
which is useful for you to know.
Right, so let's swing back to our browser,
we're gonna do a search for Java code conventions.
Now, there's lots of different conventions or companies
that have put out their own conventions
in various languages.
I think a good one for you to look at,
would be to use this one down here,
this Google Java Style Guide, so let's click on that.
I found this in general to be a pretty good guide,
even if it's not the official Oracle resource.
And what I suggest you do
is bookmark this page and come back to it.
And we will be coming back to it in future videos as well.
And keep in mind that right now,
a lot of it won't make sense
because you haven't used a lot of the Java language.
But as you progress through the course,
more and more of what you're seeing here, will make sense.
For now though, have a look under Naming to the right there.
And let's click on rules common to all identifies.
So identifies can only use ASCII letters and digits,
and in small number of cases, noted below, underscores.
So you've got package names there, class names,
method names, local variable names,
we're gonna ignore all of those for now.
I wanna come down here where it says lowerCamelCase.
In fact, what I can do is click any of those references
to lowerCamelCase.
So I'm not gonna go through and read section 5.3,
you can do that at your leisure.
But the ones that you really want to pay attention to
is just the examples down the bottom of the screen there.
I'd like move it up a bit so we can see it a bit better.
So we've got, to the left-hand side,
for example, new Customer ID in double quotes.
Notice how the word new is in lowercase
and then each word after that has got a capitalised letter.
Likewise for inner stopwatch, supports IPv6 on iOS
or YouTube importer.
Now the YouTube importer example
is actually got an uppercase first letter.
So that's using uppercaseCamelCase,
but other ones we're interested
in at least three examples here, as you can see.
So let's swing back to our slide
and talk about this a bit more.
So essentially you want to use proper LowerCamelCase
when naming variables in Java.
Ensure the variable name also gives an idea
on the variables purpose,
that's really quite important there.
So let's look at some examples here.
So a bad name example would be int myInt equal 50.
Now this does follow the lowerCamelCase,
which again is the first word is in lowercase,
then each subsequent word is in lowercase
other than the first letter, which is capitalised,
so it does follow that.
However, it doesn't signify
what the variable is being used for.
Now you might be clear on its intent right now,
but imagine coming back to this code,
looking at this in 12 months time,
will you still remember what its purpose was?
And worse would be if you're looking at someone else's code,
that variable name really gives no clue
as to the purpose of what that variable was being used for.
So it makes it harder
for you to get up to speed with the code.
So contrast that bad example
to a better naming example, int studentsAge equals 50.
So note here we're still following
lowerCamelCase convention, but now we can see at a glance
that the variable is used to record the student's age.
So make sure you do get into a habit of doing this,
habits, good or bad, are formed when learning something new.
So it's definitely worth the time to learn a good habit.
So we'll talk more about code conventions
for methods, classes, etc.,
when we start to exploring them in more detail
later in the course.
So let's finish the video here.
And in the next video, I've got a quick challenge for you.
See you in the next video.