Alright, so moving on let's have a look
at some of the names that we've been
using for our variables, you might be
wondering for example why I use the
capital S on line nine in the variable
Tim salary, well when you create names
for things in column there are a few
rules that you should really stick to
you really must stick to it, firstly
names can contain letters digits and the
underscore character but they can't
start with a digit names are case
sensitive and that means that Tim in
lowercase isn't the same as Tim with an
uppercase T and we can see that if I
change line seven and put a capital T
for the Tim variable you can see we're
now got a red error and if we hover
over that and resolve the reference Tim
and that's because we declared Tim on
line six with a lowercase T so therefore
Kotlin is saying that the variable the
capital T has been declared
and as a result Kotlin doesn't know what
Tim with a capital T actually is now one
of the most common causes of errors when
you're starting to program is using the
wrong case for variable end function
names so watch out for that you must
type the names exactly and that includes
getting the capital and lowercase
letters correct. Alright I'll put that
back to where it was and let's have a
look at other names in our code so
starting at the top on line one we've
got a function main we called the
function main in lower case. Now the
convention in Kotlin is to start
function and variable names with a
lowercase letter it's important to stick
to that convention because otherwise
people will struggle to understand your
code and you'll struggle to understand
it as well when you come back to read it
again months later. Now I know I'm
starting with a capital letter indicates
that it's a name of a class or an
interface. Now we can quickly see the
classes in the program by looking for
the names starting with capitals and
we're going to be creating our own
classes soon so you'll see that
convention being used but variables
should start with a lowercase letter
like Tim and monthly in our example code
here if the variable name is made up of
two or more words strung together
then start each subsequent word with a
capital letter and that makes it easy to
read a bit like using spaces but you
definitely can't use spaces in Kotlin
names so Tim's salary like we've defined
on line 9 as a capital S and that makes
it easy to see that it represents a
salary. Now capitalizing letters in the
name is known as camel case because the
outline of a camel has a hump or two in
the middle and the outline of camel case
names look similar. Now the other thing
you should deal with names is try and
make them reflect the value that they
represent now I haven't really done a
good job of that really because people
tend to think of a salary as being
monthly or yearly and not usually weekly
so I should have been a bit more
specific so what I'm going to do is I'm
gonna rename Tim's salary to Tim's
weekly salary and monthly to Tim's
monthly salary now Android studio will
actually take care of renaming all
occurrences of something if we rename it
by right-clicking and then choosing
refactor rename from the menu so I'm
going to come up here to Tim's salary
first so click on it and then right
click it click on refactor rename and
we're going to change that to Tim's
weekly salary and notice what I've typed
that there are changes to Tim's as well
as 13 weekly salary Tim's weekly salary
it's changing it in the other places
that this particular variables being
used so press enter now that change has
been made and I can do the same to the
monthly variable it's called select it
right click refactor rename we're going
to call this one
Tim's monthly and the capitalized
monthly salary press enter. Now that was
a simple change but it suddenly makes
the code far easier to understand now
it's easy to really see what's going on
on line 10 it's multiplying the weekly
salary by 4 to get a monthly salary now
Collin doesn't actually care what you
call these things it converts them all
to memory addresses before using them
but it really helps us humans to make
sense of the code
I'm going to demonstrate that by
renaming them again I'm going to use a
cool feature of Android studio to rename
all instances again so they don't have
to do them all individually let's just
go ahead and do that so I'm gonna change
teams weekly salary
right-click that refactor rename I'm
gonna change up to X and for the monthly
salary right click refactor rename I'm
going to change that to Y so looking at
that code now it's very hard to work out
what this what this codes really doing
we can see that it's multiplying
something by four to give something else
and if I run it will still get the same
result that we get we got previously 32
128 but it's certainly not easy or less
easy to understand now I'm gonna put
that back to what it was using another
cool Android studio feature and do you
can get that or get access to that from
the edit menu click on undo but I can
also use the commands edge shortcut on a
Mac or ctrl z on a PC, I'm gonna do that
now making sure that I'm in the editor
window so we'll do that once it's put
the first one delete the monthly salary
back to what it was if I press it a
second time we get the second one
renamed back to what it was before so
now it's a lot clearer as to what the
codes doing. Alright so we've covered a
few things there we've looked at
variables and how they have to be
declared before you can use them and
we've looked at how you name variables
until Kotlin what type of information
they can refer to string variables can
store text and it variables can store
whole numbers you've also saw how you
can perform arithmetic or numeric values
we multiplied two numbers together and
printed the results but we can also do
addition subtraction and division let's
have a look at how they work so I'm
going to use print land without giving
anything to print to put a blank line in
the output just to make it clearer to
see what the codes but what codes
producing what and then here they're
gonna type print ln parentheses which
will have a next line and they're going
to start down here on line 16 Bell
apples colon space int equals
the next line vowel or inches : into
equals 5 next line
var not valid this time fruit : int
equals apples + oranges, print ln
fruit, so if you run this to make
sure that it works first you see we've
got a gap there we've actually got the
result of lever there which is 6 plus 5
so we're using the plus operator on line 18
to giving to get rather the value are
of 11. Now we can do subtraction in
the same way by changing the plus two or
more others as you would expect by
changing that to a minors running again
this time we've got the number 1 which
is 6 to take 5 which is what we'd expect
it to be they are taking five oranges
away from six apples is it a very
sensible thing to do perhaps but the
computer quite happily calculates 6
minus 5 and produces the result.
Now division works in the same way but we
can't really look at that until we
looked at another type of number now the
int type can only store whole numbers or
integers so if you try to set the number
of apples to six-and-a-half we'll
actually get an error so it shows up to
6.5 we actually get an error the
floating-point literal doesn't conform
to the expected type int basically
Kotlin saying that the types int and
double are incompatible, well it's
actually saying that error messages I
just wrote read out but that's what it
means you can assign the literal value
6.5 to a variable of type int now if we
want to store numbers with a fractional
part, we have to use a real number type
such as double. Now by the way the
literal a literal is just a term used
for an actual value rather than a
variable so 6.5 here
that's a double literal, these other
valued the other value 5 for example on
line 17 there in turn is an integer
literal and up there on line 6 Tim
equals Tim Buchalka, well that's a
string literal. Alright so we can
perform division on integers but the
results will be an integer
so we'll lose the fractional part
they'll set the int for the value for
apples back to six what I'm going to do
is come down here down here and have
another print 'ln line 21 print ln
parentheses through apples divided by
four if we run that notice that we get
the answer 1 instead of 1.5 that's
because the fractional part of the
answers just throw it away. Now to see
how to work with numbers having a
fractional part we can calculate how
many years are represented by one
hundred and thirty weeks, let's have a go
at doing that so what I'm going to do is
in a space there then start on line 23
and type in print 'ln the parentheses
and nothing else
it give us a bit more gap in the output
they're going to type val space weeks colon
int equals 130 then val space years colon
double with a capital D equals weeks
divided by 52 point 0 then the next line
println years and now if we run
that we correctly get the answer of how
many years are represented by 130 weeks
as 2.5. So if the calculation is going to
return a number with a fractional part
we have to store it in a variable of
type double. Now there are other
fractional number type such as float but
double is the most commonly used now one
thing to watch out for there is that
Kotlin will automatically try to handle
different numeric values numeric types
if you mix them in a calculation so a
years calculation worked fine because we
divided a double value stored in weeks
by the double value 52.0, you can see
that on line 25 but it was important
that we specified the point zero there
if I change that to just 50 to get rid
of the dot zero the fractional component
then we actually get an error type
mismatch required double found int
that's because it needs a double and
we've provided an int. Now it
doesn't matter which of the two numbers
in the calculation a double but
one of them must be otherwise the result
of the calculation is an int and we
can't assign an int to a variable of
type double so what's you out for that
and when you're using numeric values in
a calculation that could return a
fractional part always include the
decimal part even if it's zero so when I
put that back with an undo back to 52.0
if you run the program again just to be
sure and we're still getting the answer
2.5 which is the correct answer.
Alright so I'm gonna stop the video
here in the next video we're going to
see how we can tidy up our output by
adding labels to those bellies that
we're printing out, I'll see you in the
next video.