Java Tutorial for Beginners 1 – Basic keywords explained

Java Tutorial for Beginners 1 – Basic keywords explained

Hi, welcome to my free Java video course Lesson 1. 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! We will start with a briefing which is
basically a lot of theory in the beginning. I’m sorry! Second we will do a practice
session where we will actually write our first
Java program. Finally we will do a short you briefing where we will recap what we learned so far and add some more theory where needed. In this
first lesson I’m going to introduce you to the
following 15 Java keywords or concepts which you will need to
understand in your first coding session later on. Please
note: specially if you already know some
programming I really focus on not using any term before I officially introduced it. Getting a keyword explained using
concepts I don’t know is something that always ticks me off in classes. So I aim to do better in this aspect.
Therefore intentionally I might explain a few
things not 100 % academically correct in
the beginning. Where necessary I’ll add the academically
correct later on. As I stated earlier my goal is to teach
you Java in the easiest possible way. If I can
achieve this by looking just a bit stupid, that’s fine with me. Last but not least this is really only a first introduction. It’s okay if you
don’t directly grasp everything. We will go in more detail and recap
the important parts later on. A package or better package structure is like the file structure
on your computer. It helps to organize different files of your program. The top or root folder of a package structure is usually the
organization’s domain name in reverse order. In my case that would
be com.marcusbiel for example. If you don’t have a domain for the program you writing it’s OK
to invent your own domain name but make sure it’s a unique name. The
reason for this is that the name of a program file is actually a combination of it’s own
name plus the full package structure of the package it resides in. There are
millions of other programs out there and chances are high that someone else has already used exactly the same program file name before you. As Java code is heavely shared worldwide a strategy was needed to come up with a
unique name to prevent name clashes. Each domain
name can technically exist only once. Тherefore using a domain name as the prefix for your package name is the default. Think of the package name like an absolute file name of your program file. Using a package name is optional but I highly recommend you to
do so even for the simplest program. When used the package must be declared in the first line of your Java program. The package name is starting with a domain name in
reverse order. As stated before. The reason for this is that the package name from left to right
gets more and more specific. Using lower case and singular for package names is the recommended
default. Last but not least the package name also serves as a
high-level description of all the program files related to a
certain topic. Code written for lesson 1 of this Java
course would end up in a package called com.marcusbiel.javacourse.lesson1 for example. Please choose package names thoughtfully. It’s not just a
stupid name. The package names reflect the internal
structure of your entire program. Last but not
least I recommend you choose packages names
related to your specific business topic. Rather than just using technical terms, good names are key. To simplify programming every program file will resuse existing
code. To do so you have to import the code into your program file. In our program file the import statement or import statements will follow directly after the package
declaration. Here you can also see how the package
name must be included in the import statement. As it is actually part of the program’s file name.
You may import specific files or all files of a certain package using the
star symbol. Besides regular import statements. There are also
static imports which we will cover in detail at a later
time. A simple program can easily consist of thousands if not
tens of thousands of code lines. It’s very easy to get lost in such a big pile of information. Therefore it’s really
important to impose a structure on the program code. Besides
using packages as explained before in Java the program code is classified into different units of code. Such a unit we call a class. The programmer is the one who may decide how to name and structure packages as well classes. One aspect of Java is to improve cooperation between
business clients and programmers. The client is able or should be able to tell what he wants. The programmer is able or should be able to know how this
can be expressed in code. The closer this code is related to the
business the easier the cooperation between
programmers and business people will be. Forming a common language related to the business and used
throughout the code is therefore of utmost importance. By
convention the name of class in Java should be a noun, starting with a capital letter. The opening
curly brace defines the beginning of the definition
of the class where we will later write all our code related to a ‘Car’. The closing curly brace defines the end of the definition of
the class. A class should be focused on only one
topic. As a recommendation keep your classes as small as possible to improve reusability as well as
readability. A class usually consists of one or more
methods. As the name implies it defines a method of how to achieve a
certain thing. Like a method ‘double’ could be defined to
calculate the double amount of an input given. Methods are sometimes also called
functions. For now while not 100% academically correct it’s okay to use the terms
interchangeably. By convention the name of a method should be a verb that best describes the purpose of the method. Methods define the actions you can execute on classes. So they further refine the structure of our program code. Methods can do pretty much everything
for us. Methods can operate not only in numbers but also on text, colours, sounds – you name it. A book would have chapters. Every chapter would have paragraphs and every paragraph would have sentences. Structure your Java code in a similar way. Package name would be like a chapter, a class would be like a paragraph and a
method would be like a sentence. For example
methods internally can call other methods which will then
form a hierarchy of methods calling each other. As a programmer it’s up to you of how you want to
structure your code. How long you want a method to be, which method will use which sub methods. I would recommend however to always think of the current
level of abstraction just as if you would write a book, for
example a method ‘prepare dinner’ could internally call a
method ‘pepare appetizer’, followed by method cаlled ‘prepare main course’, followed again by a method called ‘prepare dessert’. The
‘prepare main course’ method for instance could internally call a method called ‘boil water’. Mixing the levels of abstraction would for example
mean to have a method ‘prepare dinner’ internally calling a method ‘prepare appetizer’, directly followed by a method called ‘boil water’, without having a ‘prepare main course’ method surrounding it. Coming up with clean structure is key in
programming. Make your code speak to you. Today with problems of millions of lines of code the most important part of programming is structure and readability for humans. As soon as you start to lose focus and have difficulties in understanding
what a program does chances are high you will misunderstand some concept of the code and so you might introduce an error.
Keep your methods as short as possible. As a rule of thumb I would recommend one to three lines, with twenty lines as a maximum. The
definition of method follows a similar concept of that of a class. First we define the name of the method. As stated before use a verb and start
with a lowercase letter. The name of a method is followed by an
opening and closing bracket. This is the place where we may define the names of zero to an unlimited amount of input fields. When using the method later on you, will
need to choose a value for those values. These values are called method parameters. In order to further improve the
structure and readability of your code I recommend you to use as few as possible method parameters with three to five parameters as a maximum. Everything more
than that will not be readable. The opening curly brace defines the beginning of the method definition. The closing curly
brace defines the end of the method definition. Later we will define the actual code of the method that will go into the
space between the curly braces. As the name implies a variable is a variable value, a placeholder for a value that you can set. The value of a variable may change while the program is running. For example
when a certain value is calculated over the lifetime of your
program, variables can be used as input or output values of methods. For example variables can be of different data types, like numbers or
text. Data types we will cover in detail at a
later time. Just for example possible variable names for a ‘car’ could be
‘myOldCar’ ‘myPorsche’ or ‘mumsCar’. There are different types of variables like: “instance variables”, “static variables”, “local variables”, “parameters” or “arguments”. We will cover the
differences in a later session. “public” is an access modifier that defines
that any class from any package may use the class or method with public
access modifier. Besides public there are also other access
modifiers like private, default or protected. Here we are only
skimming the topic. In a later session we’re going to cover
access modifiers in detail. Here you can see the access modifiers “public” in action.
The class as well as the method now both have
“public” access defined. For every method you actually have to define a return
value, ironically even when you don’t want to return any
value at all. In this case you will set “void” as the
return value of the method to explicitly define that the method will
not return a value. Besides “void” a method can
actually return any kind of data type. Here you can see the “void” return type in action our ‘drive’ method is supposed to make the
car drive which is expected to execute an action but not to return any value to us. As a
rule of thumb it’s good design to differentiate
between query methods, which return a value but do not alter
the state and command methods which alter the state but do not return a value.
In respect to this best practice rule “drive” is a command method and therefore does not return any value. The “@” symbol indicates an anotation. We will cover anotations in detail in a later session. Just for now I want you to know that with ‘@Test’ we indicate that a
given method will be used as a test. When a class, a variable or a method name consists of more
than one word in Java you use an uppercase letter to
indicate the beginning of a new word. This is called CamelCase as the ups and downs of upper- and lowercase letters look like camel humps. In other
programming languages an underscore is used to separate two
words from each other inside a variable name. In Java however CamelCase is used instead. To indicate
the end of a sentence we use a “.” in English sentences. In Java however the “.” has a completely different
meaning. The “.” between the variable “car” and the method “drive” indicates that we want to execute – or we would alternatively say
call the method “drive” on “car”. As “.” is already used
to indicate a method being called, the semicolon is used to indicate the end of a
statement in Java. Here we can see an example of a test
method that is expected to test that our car drives. The method “drive” is executed on “car”with a value of 100.
Semicolon says: “I want this to be executed” – period. I
talked about classes already I said that a class is a way of structuring code in Java, a unit of code. This is perfectly true. However it’s only part of the magic.
A class is like a template, like a definition of what you want the class to do for you later on. When the program is running the class “Car” for example defines the behavior of a car. However when your program is running, we say, at runtime, there will usually be number of cars. Each car may have its own unique set of values.
One car might leak oil. So the value of oil could drop to zero while the program is running. So a class is like the human DNA a definition
of how something should be created when the
program is running. When a Java program is running it would usually create a
certain number of objects that will exist as a set of values in
the computer’s memory and those values might change while the
program is running. Each program language follows a certain concept. Java is an
object oriented language. Everything is focused around objects
in Java. In Java writing classes is a way of defining a
model which focuses on necessary aspects to solve a specific problem. It needs the brain powers of a clever Java programmer to fully understand the problem and to
extract a theoretical model from it and
transfer this model into classes. The skills of the
programmer will have high impact on how precisely a
problem is transferred into a class model. Simply speaking the more accurate this is done, the less code has to be
written and maintained, the faster the program will run, the less errors it will have. The power of Java is to be able to define an executable model, like a virtual machine on your computer, with concepts and names taken from the real world, which heavily improves the communication of the programmer and the business client. Programmer and business client can both talk of a “Car“. While the one is talking of a real car and the and the other one of the representation of this car on the computer, they will understand each other. Here we see a simplified definition a class Car. Now imagine a game where each player creates a car when the game starts. The program in this example might create three different cars at runtime. Now I talked about classes, and objects, that will be created from those classes. But how will these objects be actually created? We will need a method that will create
the objects for us. This is a very special method it is
called on a class and will create an object each time we
call this method. But as soon as the object exists we cannot this method anymore. This special method is called a “constructor”, as it is used to construct objects from class files. To create a new object of type Car, you write “new Car”, for example. The constructor in this case, just like a method, is given two different numbers, one for the age of the car, one for the maximum speed in our example. When the program is running and executes this line a new object will be created in your
computer’s memory. We talked about variables already.
However before we can use a variable, we need to define it. beforehand. Car myPorsche; – is an example of a variable declaration. We declare the variable which we called “myPorsche“, of the type Car. Now that we have a variable declared, we have to allocate the object we
created Variable declaration, object creation and allocation can all be done in one single line: So this lines reads as: When the program is running and when this line of code is executed, create an object of type Car, with first value “1“ and a second value of “320”. Than create a variable of type “Car” and give it the name “myPorsche”. Last but
not least assign the “Car” object to the variable named “myPorsche”. After this line whenever we
use ‘myPorsche’ we actually reference the object created in memory. Also you might notice when looking at
this line of code we put two values into the constructor of
“Car”. But without looking into the code of the
class “Car” we wouldn’t even know what their
meaning actually is. This is one reason why you should try
to limit the number variables used in a method, as well as in a constructor – the less the better. 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 will answer every email and I 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!

24 thoughts on “Java Tutorial for Beginners 1 – Basic keywords explained”

  1. In this first lesson am going to introduce you to the following fifteen Java keywords or Java related concepts:
    variable declaration
    object allocation
    It’s okay if you don’t directly grasp everything. We will go in more detail and recap the important parts later on…

  2. I'm learning programming from scratch, and I have chosen Java as a 1st language. I found this video to be one of the best beginner videos out there. What may have helped me is I have been exposed to some java code at work over the last week, and this video helped me put the pieces together in terms of how to read a method (among some other basic concepts). I liked the pace, and the simplicity. You explain things very well for the beginner. I want to thank you for taking the time to put this together and for your open welcoming of feedback.

    I became a bit unclear during the discussion about variables, especially at the end around the relationship between objects and variables. I will go back and re-review several times so I can get it.

  3. Thank you Marcus. This is very informational, and good for those who are beginners, who need extra detail. This helps to compliment the classes that I am taking as well. Thank you.

  4. Very nice tutorial. Important topics are being described in detail. Even if one knows about classes and methods in Java, is good to watch this video, to get a better understanding of them.

  5. VERY good, I have tried multiple learning channels and also,
    you managed to explain things in a better way than all of them,
    thank you very much and keep up the good work 🙂

  6. Hi , Marcus, ty for your hard work and generosity. I will follow u since this course seems to be one of the few that incorporate Intellij.
    My goal is to figure out how to implement APIs from external sources, such as Yahoo finance in order to "catch" real time forex data and plot it in candle stick form. I have a workable charting program from the web, as open sourced by the author; the only part missing is connecting it to Yahoo finance api in order to feed it real time market data. If you have some suggestion as to how to begin and research this topic in order to make it a reality i would be enormously grateful.

  7. My Java teacher just throws code at me without thoroughly explaining what it means. This video really helped me understand the keywords a lot better, thanks.

  8. This video is amazing. No other video actually explains any of this, they just tell you to "put this here and that there". It feels good to actually make a functional Gui, but it doesn't help not knowing why it works the way it does.

  9. thank you so muck for making this video .
    I'm a programmer also ,but the way you explained the topics i have never seen such teachers.
    you are great.

  10. I'm learning from scratch and I like it. The fact that you better defined it as an Object Oriented Language helped me the most. I have a tendency to think in the abstract, so this helped me think about using concrete language specific to the job.

Leave a Reply

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