Java for Beginners 2 – Basic Java keywords explained – Coding Session


Hi, welcome to my free Java video course lesson 2. My name is Marcus Biel, I’m a software craftsman with 14 years of experience in Java. My goal is to teach you Java in the easiest possible way. You can
actually help me by sending feedback to my email address. I need your feedback for two reasons: First based on your personal feedback I’m going to continuously improve the
training, second your feedback will motivate me to continue and record further lessons. I will answer every email. Thank you! In session 2 of this free Java video course we’re going
to write our first Java program. When we write code we actually start by writing a
program which is actually testing the program or
class we’re going to write. We do this for two reasons – first, the test forces us to properly think our idea through. Which will improve
our code a lot. second, the main effort in the lifecycle of a program is actually not the first time effort of writing a program code but to maintain the program. Like
extending or improving the functionality or fixing errors, called bugs in your code. Whenever you write code there is a high risk you will introduce
defects in your code. When your program is written once with help of a test, this test will insure that whatever
happens your program will continue to function
as designed initially. To recap, in lesson 1 of this free Java video course, I explained the necessary theory to code our first Java program, which we’re going to do in a minute. We’re going to use these 15 Java keywords and concepts described in detail in the prior session.
If you haven’t done yet I would recommend you to start with session 1 before
watching this practice session. So now I’m in my development environment I’ve created a folder structure of com/marcusbiel/javacourse/lesson2 in a folder called ‘test’ and also here src/main/java/ com/marcusbiel/javacourse/lesson2. This is where we will write our tests. I’ve already
created the file ‘PersonTest.java’. This here is where later we will have our actual
program. So let’s start ‘PersonTest’ . As I described in lesson 1 we have to start with the package declaration. So I write – and you see here – I actually get support by my environment. About the development environment I’ll tell you later for now just see that it actually assist
you. Actually when you start learning to
program I would recommend you to start with a
regular text editor because you need to learn all these keywords, and so I guess it’s easier in the beginning, I mean not easier but it helps you to learn the stuff from
scratch. If you really have to focus and concentrate
and you don’t get assistance. But as soon as you managed all these keywords, as soon as you know
all them such an environment is really helpful. So I use ‘package’ and then now I have to define it ‘com. marcusbiel’ – see how it assists me again? It already knows the package because I have here the folder structure and the folder structure and the package
declaration in my file actually have to be the same, otherwise
this would be an error. So actually I don’t have to write it anymore. I can just follow. So this is my package and you see before we had an underline – a red underline which meant
something is wrong. Now our editor seems to be fine with what I wrote so far. Here we later will have imports as
described in lesson 1, but we will add them later. Normally you start defining your class first. See again I wrote ‘public’ and see how it’s underlined red, so something is
missing. So I say ‘class’ and now I have to define the class name. I say ‘PersonTest’ so I call my test ‘PersonTest’, it’s a test for our first program. Which is the class ‘Person’. And now I have to add the brackets. When I add the first one my editor already adds the
second one because for every opening bracket you always need a closing bracket . So this is the beginning and this is the end of my ‘PersonTest’ Now I add the annotation @Test, so that I define I want to write a test. And see? magically the import appeared because my environment already knows I want to write a test. The test also I mean is actually a class and it even has a package and this is ‘org.junit’ We’ll talk much more about Junit in later lessons. But for now we just know this is a test, magically somehow.
And we need a test method. The test method will be ‘public’ it won’t return any value so it will be ‘void’. And I’ll give it the name what I want it to do: ‘shouldReturnHelloWorld()’. ‘Hello World’ is actually like a running gag of programmers.
Someone I don’t know who – once started that the first program you write in a language should return ‘Hello World’. So we do that too. Now let’s first write the test. For my first ‘hello world’ program I expect my ‘Person’ to return ‘Hello World’ to me. And I’m going to test this. We haven’t written any actual code for ‘Person’ – but we’re starting reverse – so we’re starting with a test as I explained before. So now I can just
assume I was done you see it’s red, which means we don’t have
a class ‘Person’ yet but that doesn’t matter. Now for this ‘Person’ I need a variable name and as my name is ‘Marcus” just for fun I call the ‘Person’ Marcus. Now I have to create the Person ‘Marcus’. Remember? This was calling the constructor, creating the object and assigning it. This is
creating the object of ‘Person’ from the type or class of ‘Person’. And the equal sign assigns that object which is created in the memory of our PC
to the variable, to the reference variable ‘marcus’ of type ‘Person’. Because
this is a reference that we can later on use referencing the Person. Now this is red but we’ll take care of this later. We’ll just continue. So assuming we were done, what do we want? We want to test something and in testing we say we want to assert
something. We want to assert that a method we’re going to write is returning the ‘Hello World’ for us. So we
want to compare if our expectation is equal to what the method is going to
return. So we say ‘assertEquals’. This also is starting as red, because will have to
import more stuff but for now we just take it – I use ah – this was again “magic” – I add one double quote and my environment already adds the closing double quotes This is what we call a “String”. And I say ‘Hello World’ because I want my ‘Person’ to return ‘Hello World’ for me. So the first part here is the expected return value. Now I add a comma and now I have to
add what is going to be tested ‘marcus.’ for calling a method ‘helloWorld()’. So on the object ‘marcus’ of class ‘Person’. I want to call a method ‘helloWorld()’ with no parameters given. And what is returned here I want to assert that it equals to ‘Hello World’. Now this is not going to work because we still have some red stuff. For example here we don’t have
actually written the class. But here we will let our environment help us. You see? Now I just have to say: “Create Class Person” sorry again “Create Class Person” And here it already asks us destination package ‘Create Class Person’ com.marcusbiel.javacourse.lesson2 Sounds good. Because our test and the actual class of the test, will be in
the same package. This actually helps us a lot but more about this later on. I say “ok” and magically our first class got created. We see here the package – and the class. This is actually kind of the smallest class possible. Just with opening and closing bracket, but it
doesn’t do anything yet. But you see it’s not red anymore so let’s go to the next – assertEquals – ah it wants to import something
statically. As I said before I will later on tell you what means ‘static import’ but
for now, just believe me. We have to import this. This is another import from org.junit but this time it’s an assert. And so this is like a method we import the method ‘assertEquals’. But don’t focus too much on this. We’ll go in more detail later. For now what is important we want to write our
first test. So here we still need to define a method and it also tells us “cannot resolve method ‘helloWorld()’ ”. Okay, help us, please! Create method ‘helloWorld()’ it moved the method to the wrong place
but I’ll fix that. Of course we need to have it here. Now nothing red anymore, and we can start calling our first test. I say run and this actually executed our first test. Which
again executed our first class, that was
automatically written. And here we see, we have an error. It tells us “a missing return statement”. So I said we want to return a String – here you
see a String but as you see the method is empty we don’t return anything. So let’s fix that. ‘return’ – this is how we say it. We have to say ‘return’ and now I just
have to type “Hello World”. Exactly as I was expecting it I add a semicolon to say: “Yes, do it – return ‘Hello World’ “! Now I switch over to the test and I’ll execute the test again, and it’s green all tests passed. This is our first test, and it worked! Running our first class. Tadaaa! Okay. Thank you! That’s it. That’s it for now . Don’t forget to
send me your feedback. I’m highly interested if you were able to follow
my explanations. Please give me feedback what you did or did
not understand. Where I was too fast or too slow. I’ll answer every email and will try my best to
incorporate your feedback in the sessions. Actually I might even re-record a session based on
your feedback. Thanks for watching and hope to hear from you
soon!

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *