Alright so we're all set up now so
it's time to start learning Kotlin,
so start Android studio and then open the
Kotlin tutorial project so I'm gonna
open up this by clicking on Kotlin
tutorial and just a reminder to ignore
the outdated Kotlin runtime or messages
of that nature for this particular
project because we've had to hack things
together to get it to work, so if you
do that you'll find that things won't
work properly. Alright now open up the
main dot kt file by double clicking it
if it's not already showing on the
right-hand side of the screen, in my case
it was already open and by the way if
you want to close it at any time you can
come over here and click on the X to
close it and then we'll click it to open
it again so that's what you do when you
want to work with different files you
can just choose which ones to appear in
the tab list above the editor window
here and you can have multiple ones on
that list and click on to select the
different ones now unlike Java you don't
need to create a class to hold your
Kotlin code and if I keep referring to
Java in this tutorial things are going
to get very confusing for students who
don't understand Java.
Now there's one example I've just talked
about classes and we haven't even
discussed what they are yet so for most
of this tutorial section I'm not going
to keep comparing things to Java I won't
promise not to mention Java now and then
but I won't do it very often we will
start by looking at that simple function
on-screen here that just prints out
hello world that you saw running in the
previous video. Now that looks a bit
horrible but this particular function
has to follow that format now the reason
is that the computer needs to know which
function it should start with when you
run your program so you have to start
with a function called main that has a
declaration exactly like we've got here
on line one now things are a bit
different when creating an Android app
rather than a Kotlin program but at the
moment we're learning Kotlin so we have
to use a main function in this form now
the keyword fund defines a function and
our functions called main you everything
between the opening and closing braces
as these here at the end of line 1 and
the style of line 3 that's part of this
function now inside the function is the
code that's executed when our program runs
make sure you put your coat in between
these two curly braces so you can see
here on line one on line three when you
want it to be executed as part of this
function so this function prints out the
message helloworld which is what this
print line statement here on line two
actually does. Now Kotlin programs are
normally a lot more complex than this
they contain a lot more classes and
functions and can also repeat bits of
code and so forth but this is a very
basic Kotlin program so what our
function is doing is calling another
function called print 'ln and that's
short for print and start a new line and
the functions part of the Kotlin library
so that just means it comes with Collin
to give us a way to print things so we
tell it what to print by putting what's
called an argument inside these
parentheses as you can see here on line
two so that's the argument that's in
between those two in between the
parentheses or inside the parentheses
and here the argument is the text hello
world in double quotes and by the way
because we're dealing with text rather
than numbers we enclose hello world in
speech marks all these double quotes as
we call them in Australia so our main
function uses another function called
print 'ln to print the text that we've
given it. Now arguments are values that
you provide to functions to give them
the information that you want them to
work with so the print line function
needs to know what to print and we tell
it by giving it a string of characters
enclosed in these speech marks or double
quotes so let's just run the program
again to see if it's doing what we
expect so what we can do we can also
right-click in here within the editor
window to get the context menu right
click it and I'm going to select main
Katie if I select that you can see down
the bottom bottom left-hand corner we've
got holo world showing on the screen now
the console should have opened up
automatically but if it doesn't you may
need to click on this run tab at the
bottom of the screen over here and say
you've got to turn it off and turn it on again
so if it's open it's it'll close when I
click it if it's closed
I'll open it'll open when I actually
click it again alright so there's our
holo world text printed out now when a
program runs without any errors
we'll see that the message at the end
says price has finished with exit code
zero. Now normally an extra code other
than zero means that something's gone
wrong but you'll normally get an error
message as well so you'll know if that
hasn't worked.
So let's actually print something else
out so they'll come down here and type
in print 'ln they're going to type my
first Kotlin program and we've got this
red squiggle at the end of the line as
you can see over here and over to the
right-hand margin is a red dash over
here. Now Edward studio does that to
indicate errors and warnings we'll be
seeing some warnings later they'll
appear in a different color
normally orange or amber and you can
choose to ignore those it's a good idea
that one to at least read them though
because they'll often highlight problems
with your code the warnings won't stop
the code from running but they could
mean that it won't do quite what you
expected but these red arrows Oh can't
be ignored the code just won't run when
you've got errors like this so to see
what the actual error is you can hover
the mouse either over the school over
here or over here in the right margin
the arrow is just saying expecting and
then in single quotes our right
parenthesis so it's just telling us that
the line must end with the closing
parenthesis now computers are very picky
and it's important that your code
follows the syntax of the programming
language exactly you'll pick up the
rules by reading and writing code so
don't think that you have to remember a
whole lot of complicated grammar before
you can program if you do make mistakes
which you will because it's a natural
part of learning to program then Android
studio or the Kotlin compiler will give
you errors to indicate what's wrong now
there's nothing actually wrong with
getting errors it's a natural part of
learning how to program you know I wish
I could claim that I never get errors in
my code but even after over thirty years
of programming I still make mistakes so
don't worry the more mistakes you make
though that probably means you're
learning faster so unlike human
languages computer languages are very precise
you have to stick to the syntax of the
language and if you think about it even
human languages need correct punctuation
though being intelligent we can often
work out what so really meant if the
punctuation isn't quite right but
punctuation is still important have a
look at this slide now here's exactly
the same text but punctuated in two
different ways and if you haven't read
of both of those you'll see that there's
a big difference just by adding
punctuation in appropriate places so if
you do find yourself getting frustrated
remember that that it's only a computer
he can't think for itself and you have
to compensate for that by being very
precise in how you write your code so
that the poor computer can understand
you. Alright so back to Android studio
so it's telling us there's something
wrong here,
so I'm going to add the closing
parenthesis to the end like so and
notice that the red - the has
disappeared as well as the red lines in
the right side of the screen the right
margin, so now that we haven't got any
errors we can run this again and now
that I've already run the program for
the first time by right clicking I can
just actually come up here to the green
triangle up in the toolbar I can click
on that now that doesn't always work
until you be the right clicked and
chosen the function you want to run or
use the run menu now you can see on
there there's already been different
options for running so I'm click on that
you've got different options there but
we've only got the one file here they've
only got may end up Katie so if you end
up running the wrong program later in
the course either right-click on the
code well it's in the editor that you
want to run or use the menus to come up
here and then choose the actual menu but
in our case we've just got the one menu
of the one file because that's all we've
worked on up until this point in the
course so after that the green button
should work for whatever map function
you're trying to run but you will have
to tell it to run it the first time,
so if I just click on that now you can see
we've got hello world there my first
Kotlin program. So then you've got a
function containing two lines to execute
and unless we do something to tell it
otherwise, Kotlin will just start at the
top and obey the instructions on each
line until it gets to the end,
we can't have a look at what we can do
to make it behave differently shortly
but before that we need to have a look
at variables or properties we'll do that
in the next video.