Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Hi, welcome to my free Java video course.\n
Craftsman with 14 years of experience in Java.\n
possible way. Okay, we will start with a briefing,\n
beginning. Second, we will do a practice session,\n
program. Finally, we will do a short key briefing,\n
and add some more theory were needed. In this\n
the following 15 Java key words or concepts,\n
coding session later on. Please note, especially\n
focus on not using any term before I officially\n
using concepts I don't know, something that\n
do better in this aspect. Therefore, intentionally,\n
correct in the beginning. When necessary,\n
on. As stated earlier, my goal is to teach\n
can achieve this by looking just a bit stupid,\n
is really only a first introduction. It's\n
We will go in more detail and recap the important\n
A package or better package structure is like\n
to organize the different files of your program.\n
is usually the organization's domain name\n
become Marco spiele. For example, if you don't\n
it's okay to invent your own domain name,\n
for this is that the name of a program file\n
plus the full package structure of the package.\n
programs out there, and chances are high that\n
same program file name before you. As Java\n
was needed to come up with a unique name to\n
technically exist only once. Therefore, using\n
name as the default. Think of the package\n
file. Using your package name is optional,\n
the simplest program. When used, the package\n
Java program. The package name is starting\n
before. The reason for this is that the package\n
specific. Using lowercase and singular for\n
Last but not least, the package name also\n
the Program Files related to a certain topic.\n
course would end up in a package called con\n
Please choose package names for fully. It's\n
reflect the internal structure of your entire\n
to choose package names related to your specific\n
terms. Good names are key. To simplify programming.\n
To do so you have to import the code into\n
import statement or import statements will\n
Here you can also see how the package name\n
as it is actually part of the programs falling.\n
of a certain package using the star symbol.\n
also static imports, which we will cover in\n
easily consist of 1000s if not 10s of 1000s\n
in such a big pile of information. Therefore,\n
on the program code besides using packages,\n
code is classified into different units of\n
The programmer is the one who may decide how\n
classes. One aspect of Java is to improve\n
The client is able or should be able to tell\n
be able to know how this can be expressed\n
the business easier, the cooperation between\n
a common language related to the business\n
utmost importance. By convention, the name\n
with a capital letter. opening curly brace\n
the class, where we will later write all our\n
defines the end of the definition of the class,\n
As a recommendation, keep your classes as\n
well as readability. A class usually consists\n
it defines a method of how to achieve a certain\n
to calculate the double amount of an input\n
For now, one at 100%. Academically correct,\n
By convention, the name of a method should\n
of the method. Methods define the actions\n
refine the structure of our program code.\n
us. methods can operate not only in numbers,\n
it. A book would have chapters, every chapter\n
would have sentences structuring your Java\n
be like a chapter, a class would be like a\n
For example, methods internally can call other\n
of methods calling each other. As a programmer.\n
your code, how long you want a method to be,\n
would recommend however, to always think of\n
if you would write a book. For example, a\n
a method prepare appetizer, followed by a\n
again by a method called prepper. Desert.\n
could internally call a method called boil\n
for example mean to have a method prepare\n
appetizer 30 followed by a method called warm\n
method surrounding it. Coming up with clean\n
Code speak to you. Today with problems of\n
part of programming is structure and readability\n
and have difficulties in understanding what\n
misunderstand some concept of the code. And\n
methods as short as possible. As a rule of\n
The definition of a method follows a similar\n
the name of the method. As stated before,\n
The name of a method is followed by an opening\n
we mainly find the names of zero to an unlimited\n
later on, you will need to choose a value\n
method parameters. In order to further improve\n
I recommend you to use as few as possible\n
as a maximum, everything more than that will\n
the beginning of the method definition. closing\n
definition. Later, we will define the actual\n
between the curly braces. As the name implies,\n
for a value that you can set. The value of\n
running. For example, when the third value\n
variables can be used as input or output values\n
of different data types, like numbers or text.\n
time. Just for example, possible variable\n
or mom's car. There are different types of\n
variables, local variables, parameters or\n
Public is an access modifier that defines\n
class or method with public access modifier.\n
modifiers like private default or protected.\n
session we are going to cover access modifiers\n
public in action. The class as well as the\n
For every method, you actually have to define\n
don't want to return any value at all. In\n
of the method to explicitly define that the\n
a method can actually return any kind of data\n
in action or drive method suppose to make\n
an action but not to return any value to us.\n
between query methods, which will return a\n
methods which offer the state but do not return\n
rule. Drive is a command method and therefore\n
indicates an annotation. We will cover annotations\n
I want you to know that with ADD test, we\n
as a test. When a class a variable or method\n
you use an uppercase letter to indicate the\n
case, as the ups and downs of upper and lowercase\n
languages, and underscore is used to separate\n
name. In Java, however, camel case is used\n
we use a thought in English sentences. In\n
meaning. The dot between the variable car\n
to execute or we will alter. Alternatively,\n
already used to indicate a method being called\n
in Java. Here we can see an example of a test\n
drives. The method drive is executed on car\n
to be executed period. I talked about classes\n
code in Java, a unit of code. This is perfectly\n
A class is like a template, like a definition\n
on when the program is running. The class\n
car. However, when your program is running,\n
number of cars, each car may have its own\n
so the value of oil could drop to zero while\n
the human DNA, a definition of how something\n
When a Java program is running, it would usually\n
exist as a set of values in the computer's\n
Each program language for certain concept,\n
is focused around objects in Java. In Java\n
which focuses on necessary aspects to solve\n
of a clever Java programmer to fully understand\n
from it and transfer this model into classes.\n
impact on how precisely a problem is transferred\n
accurate this is done, the less code has to\n
program will run, the less errors it will\n
an execute table model, like a virtual machine\n
taken from the real world, which heavily improves\n
business client, programmer and business client\n
of a real car, and the other of the representation\n
each other. Here we see a simplified definition\n
player creates a car when the game starts.\n
three different cars at runtime. Now I talked\n
from those classes. But how will these objects\n
that will create the objects for us. This\n
a class and we'll create an object each time\n
exists, we cannot call this method anymore.\n
as it is used to construct objects from class\nfiles
to create a new object of time car, you write\n
case, just like a method is given two different\n
for the maximum speed. In our example, when\n
a new object will be created in your computer's\n
already. However, before we can use a variable,\n
Colin is an example of a variable declaration,\n
portion of the type car. Now that we have\n
object we created a variable declaration,\n
done in one single line. So this line reads\n
line of code is executed, create an object\n
a second volume of 320. Then create a variable\n
Last but not least, assign the car object\n
line, whenever we use my portion, we actually\n
you might notice when looking at this line\n
of car. But without looking into the code\n
their meaning actually is. This is one reason\n
variables used in a method as well as in a\n
In session, to have this free Java video course,\n
When we write code, we actually start by writing\n
or class we are going to write. We do this\n
to properly think our idea through which will\n
in the lifecycle of a program is actually\n
code, but to maintain the program, like extending\n
called bugs in your code. Whenever you write\n
defects in your code. When your program is\n
test will ensure that whatever happens, your\n
initially. To recap, in Lesson One of this\n
theory to code our first Java program, which\n
to use these 15 Java keywords and concepts\n
If you haven't done yet, I would recommend\n
this practice session. Okay, so now I'm in\n
folder structure of calm Marcos build Java\n
and also here source main. Java. com Marcos\n
we will write our tests have already created\n
where later we will have our actual program.\n
in Lesson One, we have to start with a package\n
I actually get support by my environment,\n
you later. For now just see that it actually\n
to program, I would recommend you to start\n
to learn all these keywords. And so I guess\n
but it helps you to learn the stuff from scratch.\n
and you don't get assistance. But as soon\n
as you know all them section environment is\n
then I have to define it. COMM macros, we'll\n
the package, because I have here the folder\n
package declaration in my file actually have\n
error. So actually, I don't even have to write\n
is my package. And you see, before we have\n
something is wrong. Now, oh, editor seems\n
here we are. Later, we'll have imports, as\n
later. Normally, you start defining your class\n
how it's underlined red, so something is missing.\n
class name is a person test. So I call my\n
program, which is the class person. Okay,\n
the first one, my editor already adds the\n
you always need a closing bracket.
Okay, so this is the beginning. And this the\n
no at the annotation test. So that I define,\n
the input appeared. Because my environment\n
test also, I mean, it's actually a class.\n
unit test. We'll talk much more about j unit\n
this is a test magically somehow. Okay, and\n
be public. It won't return any value, so it\n
I want it to do should return hello world.\n
of programmers. Someone I don't know who once\n
language should return hello world. So we\n
program, I expect my person to return hello\n
haven't written any actual code for person.\n
with the test, as explained before. So now\n
red, which means we don't have a class person\n
person, I need a variable name. And as my\n
I call the person Marcus. Okay, now I have\n
calling the constructor creating the object.\n
from the type from the class person. And the\n
in the memory of RPC to the variable, the\n
a reference that we can later on use referencing\nthe person.
Now this is read, but we'll take care of this\n
we were done, what do we want? We want to\n
to assert something, we want to assert that\n
the hello world for us. So, we want to compare\n
is going to return. So we say assert equals,\n
have to import more stuff. But for now, we\n
I add one quotation mark, and my environment\n
is what we call a string. And I say hello\n
HelloWorld for me, so, the first part here\n
a comma. And now I have to add what is going\n
Hello, world. So on the object, Marcos of\n
world, with no parameters given and what is\n
to hello world. Now, this is not going to\n
For example, here, we don't have actually\n
environment help us, you see. Now I just have\n
create class person. And here, it already\n
person. COMM walkersville. java course lesson\n
and the actual class of the test will be in\n
lot. But more about this later on. I say,\n
got created. You see the package, and the\n
class possible, just with opening and closing\n
but you see, it's not red anymore. So let's\n
to import something statically. I said before,\n
import. But for now. Just believe me, we have\n
org j unit. But this time, it's an assert.\n
method assert equals. But, um, don't focus\n
later. For now, what is important, we want\n
to define a method. And it also tells us cannot\n
create method HelloWorld. We had enough it\n
fix that. Of course, we need to have it here.\n
start calling our first test. I say run. And\n
again executed our first class that was automatically\n
It tells us a missing return statement. So\n
string. But as you see the method is empty.\n
fix that return. This is how we say it, we\n
Hello, world. exactly as I was expecting it,\n
world. Okay, now we switch over to the test.\n
green, all tests passed. This is our first\n
Tara. Okay. Thank you. That said, lesson three\n
to our development environment, and have a\n
back in our development environment. And now\n
line of our program is the package declaration.\n
of your domain name in reverse order. So in\n
two, and we ended with a color. Actually,\n
I really recommend you to use a package name.\n
need to have some practice. But for now, just\n
And always define a package name. After the\n
import statements. This just for our lesson,\n
name. And this is an A sub package called\n
in the same package. So person has an a package\n
for all classes that are in the exactly same\n
to import them, Java will find them automatically.\n
want to use the name class that I just wrote,\n
class definition public class person. And\n
this says the class ends. Public means all\n
in any package can see the class person. What\n
now, just take it as it is, I don't expect\n
need some practice. And here for now, some\n
name, and I made it private, which is also\n
variable, because it is a reference to later\n
which means other objects that get created\n
See, or access this field here directly. If\n
this method I also just created this one is\n
they call person dot name, the method you\n
but you could also call it I don't know.
Return the name. So this can be anything um,\n
again we see return, which means I want this\n
and the colon which means do it execute it.\n
this stream, and that having a class, returning\n
for the first program that you do. So this\n
I mean, actually, it might not make sense.\n
going to the test, we learned, we actually\n
test, we hadn't defined the class person yet.\n
We did all this on the fly. In our development\n
it was able to create the class as well as\n
of programming. First of all, you define the\n
think about the property sign. And then you\n
actually even write it yourself. And when\n
execute the test. And when it returns, okay,\n
proper job. Okay, see, here, you'll see a\n
So when testing we often speak of green or\n
signal that you understand your test passed\n
speak of green and red bars. Okay, so we learned\n
an annotation. And I told you, you don't need\n
go into details later. Besides on top of input\n
to know that something static exists, but\n
that's it. It's okay, if you don't understand\n
to. Um, so we'll go in more details later.\n
fine. I mean, the start is really tough. I\n
in Java yet. Um, this is just an introduction,\n
we're back in my ID E. Id is actually the\n
And as this is much shorter, usually you just\n
far. And the first one, you have the package.\n
statements, you don't necessarily need something\n
And we use the classes short name, because\n
always the package name, so that the name\n
use its full name, including the package name,\n
But I mean, as you see, this is much more\n
handy. So this is what you normally do. There's\n
So I will just shortly mention it. Sometimes\n
classes of the exact same short name
but have different packages. And so to define\n
use one with the full package name. Okay.\n
with a curly opening brace and ends for the\n
say it's an instance variable actually. And\n
And instance methods could be, of course,\n
members of the class, because they belong\n
object. So of one class, you can create various\n
another person could be, could have the name\n
of class of objects, sorry. And each one of\n
instance variables, this means that each instance\n
each might have a different value does not\n
There's also another thing, which is called\n
we do when we add static to it, we can add\n
case, now when we create different instances,\n
this means that all other instances will also\n
is a bit more advanced and static. We should\n
shortly, just to be complete, wanted to mention\n
this is an instance variable, and this is\n
would want to add a constructor, I think I\n
Opening and closing curly brace. This is what\n
have any parameter here, not any variable.\n
we want to create an object of type person\n
called and everything that we would do in\n
is actually used to initialize an object.\n
Now let's add a parameter, prison name. So\n
because we could try to set the inner person\n
something is wrong. Variable person name is\n
our ID or Java know? What do you mean? Here,\n
Java, what we want is we want to set this\n
person name. One thing we can do to fix that,\n
works. Now we can automatically automatically\n
want that I mean, person name is better name,\n
have to add this to one of them. And as we\n
instance variable person name, we add this\n
mean this one here. And now, when someone\n
of type person, it'll also have to add the\n
directly gets set at the time the object is\n
know is now that we have created our own constructor\n
this class does not does not have a default\n
you before this one has gone. Java will automatically\n
by us. But as soon as we have defined one,\n
our test from the last version, we use the\n
will later on fail, but I want to show you\n
we had that method here HelloWorld, I told\n
language has like a running gag, you return\n
And here we say the method should return a\n
on, I would like to have a method, I can give\n
say hello, the person's name. Um, okay. And\n
with a test. So let's write a test for what\n
test? So we have to first of all at the end\n
it a notation. Much more. We don't know yet.\n
I mean, this is all confusing. So relax. I'll\n
understand that very soon. For now, let's\n
Okay, because this is what I want the method\n
brackets for the method. Okay, so we need\n
I called it, Marcos. This time, I want to\n
So it's called person, new person. So with\n
I call the constructor. And this is like a\n
the time when the object is created in our\n
I told you the default constructor is gone.\n
to our class. And we have to add the default\n
put it here. Okay, and now this is empty,\n
or an empty constructor might be confusing\n
this case, we leave a comment. To mention\n
this on purpose. So let's see how do we do\na comment actually.
So I say empty. On purpose, default constructor.\n
There's also also a single line comment, which\n
But I really recommend you should prefer the\n
will go in detail about comments in a later\n
show you. Okay, so we have fixed the test.\n
created an object of type person, let me copy\n
create various objects one after the other.\n
called person. So this will not work. So we\n
two. And so like this, we could continue.\n
so that you see. We have one class of type\n
the default constructor. And so we have two\n
will delete this again. I mean, this was just\n
what we want to achieve actually is we want\n
I was wrong. I want to say hello, Marcos.\n
I want Hello, Marcos. And I want to say person\n
we use a variable here, we call it an argument.\n
people use this interchangeably. But to be\n
it's all about variables. So we add a string.\n
is missing our ID, he tells us something right\n
So I want on the object of person we created\n
Hello. And you see it's red. And it also our\n
Hello. It doesn't exist. But we are programming\n
to create a method right now. Okay, I'm actually,\n
my array he created for us. Well, there are\n
later on. So for now, I just did this to speed\n
sure you could create that by yourself. Now\n
now this is something special. I don't want\n
I don't want to confuse you. But I also don't\n
other. And now, I would add well Marcus's\n
be any name, not necessarily has to be Marcos,\n
want is I want to add name to Hello. What\n
name. And something is missing. Oh, it doesn't\n
how you concatenate two strings together.\n
can do this any number of time. But for now,\n
the given name. So this should be fine. Actually,\n
see very soon. Which I did on purpose, because\n
Let's execute it. Ah, so you see expected\n
there's a space missing. So see, this is one\n
you to think about the stuff you code. But\n
then you will directly spot it and you're\n
and destroy something. And yeah, this can\n
go down to our method. And here was a space\n
executed again. And now the bar is green.\n
I think that's it for now. Lesson five. So\n
first of all about visibility modifiers. Actually,\n
modifier is private. Another one is public.\n
is visible or usable. From all other classes\n
on the package comm Mako speelde. java course\n
in lesson three. So if we would go in the\n
person as long as it's public. A private class,\n
not allowed here. And of course how That makes\n
But just in this class. This doesn't make\n
we can leave the modifier away. And this will\n
why it's called default modifier, also package\n
be used from other classes within this package.\n
the constructors, we can even have a private\n
could have that method private. And this one\n
one. Let's call HelloWorld. From this method\n
even makes sense. Because especially when\n
say not more than three or 10 lines. Then\n
into smaller private methods, and then calling\n
have only two methods. So to make things up,\n
course, I could define further methods. We\n
any number of methods later on. Okay, so to\n
far is public, private, and default or package\n
modifiers, one, for example, is protected.\n
should be perfectly coming alone, along with\n
let's just concentrate on these few. Let me\n
we are going to need it in our test. And I\n
the code as it was before. Okay, because now\n
idea. I have prepared the test should return\n
of type person, one person, person to
we could also use the other constructor, but\n
as this at the moment is just too much work\n
it a different name, like my person, just\n
variables. So what this means is when the\n
is executed, which is just after the column,\n
in memory. And after this line three objects\n
on a number of objects. Now I should have\n
a method that tells me exactly that. Just\n
Okay, still read, remember, we'll create it.\n
three is a number. And a number in Java is\n
this actually a primitive. And the default\n
zero here. So it's not red anymore. We can\n
course, because it statically returns zero\n
Well of course we could say three statically.\n
of course, greenbar Tada. But, I mean, I want\n
have two persons, I want this to be two. And\n
Let's copy it. And now when I execute it,\n
to, but it's still three. So my first approach\n
gag, but yeah, not what we need. So what we\n
is created, we need to count each object that\n
the object is created, there is one thing,\n
is, you might guess it, the constructor, we\n
So we would have to do something in here.\n
variable, I also call it or let's call it\n
And now I say person counter, because I want\n
add one to the last value of person counter\n
side, is the new value. On the right side\n
And I add one to it. Now, this is a lot of\n
is used so often, that they invented short\n
plus plus, we say take the current value of\n
it again into the value of person counter,\n
it. Let's do that. Make it private, of course,\n
column. Because I don't have to say zero,\n
for end is zero anyway, so what we usually\n
would be it starts with zero. The first time\n
by one, and so on. So that my method now should\n
instead. Okay, let's try that. Execute the\n
It didn't return zero. It didn't return three\n
one anywhere. Maybe you guessed it already.\n
one. Now I told you something in the prior\n
of the instance. So each object has their\n
is called for each of the objects. So in the\n
set to one. So what we need is more like a\n
level. But we say on the class level, because\n
only one class person and this we achieve\n
as a recommendation because this will help\n
just believe me. As we have the variable of\n
method of type static, it would work also\n
so now let's execute it. And this work Great.\n
for for an exit executed and it works, I said\n
And it works? Well as you see this might be\n
on. And also there is a recommendation. Actually\n
persons as a static method, it sees that you\n
And as this is a static method, it doesn't\n
this method is static, it's not part of the\n
of the object. This method now is part of\n
trying to be smart and is smart, and calls\n
doing here is working because Java knows how\n
is better is darkly saying person, so that\n
instance, we're just calling a static static\n
that again. And it's working. Thank you. So\n
to talk about the tools which I'm using. So\n
It's an apple MacBook Pro. This ID that I'm\n
at the moment is my favorite ID. Before I\n
is open source is also very popular at the\n
it's open source and free. However, I think\n
and better every year. So I really like it\n
for saying that. But I mean, this is your\n
So I would say just try it out yourself. There's\n
and it's also for free. I'll just in a second,\n
have a closer look at my tests before. So\n
I'm using j unit, which is a testing framework.\n
a set of tools to set of classes. And it's\n
writing such a framework, it's not really\n
done. So this makes it really easy to start\n
few testing frameworks. The second one I would\n
pretty much the same. And really depends on\n
some use testing G or any other framework,\n
knows not so much big differences. So it doesn't\n
in here. IntelliJ IDEA actually supports both\n
try them. Um, I'm not going in much more details,\n
So I focus really on Java. Just to be complete,\n
I do this in less than six because at the\n
is already quite difficult. So I wanted to\n
the heck is that here? What are you doing?\n
is lots of documentation on the internet.\n
so maybe we just go to the browser now. and\n
intelli j idea for you pops up.
So you can download, I would first of all,\n
you like it or not. Besides, of course, there's\n
then you can choose your operating system,\n
but not least NetBeans. Just press the download\n
system. And then also j unit, let's have a\n
won't have to install it, because it would\n
record, I mean, this also has lots of documentation.\n
version. Let's check documentation. Here.\n
also using so this way, you can define in\n
for 12 of j unit, which I actually did, because\n
in a second. But after j unit, let's also\n
test ng g on this page just provides the false\n
IDEA, it's included anyway. And I checked\n
with NetBeans, I have to admit that beans,\n
download and install. So probably works just\n
Maven, there's what's called a build management\n
build. Now, what's a build? Well, when you\n
of classes, and then you would want to release\n
And then of course, you would not want to\n
usually do a package a single file out of\n
and say in prehistoric times, this was done\n
So people very soon started to program, some\n
first tool that I know of is probably make\n
up with and and Maven again is the successor\n
in version three. Besides Maven, there's also\n
here you see it find that I'm using j unit\n
is UTF eight, because what this bill two also\n
things is environment specific settings. So\n
eight, I can do it here. And so much more.\n
Maven. There, you can download it. They also\n
book. You can download all the files, even\n
check Maven book. I really recommend that.\n
And also there should be a PDF somewhere.\n
there is also a PDF available which you can\n
Last but not least, let's also talk about\n
at least one of the lessons I did so far,\n
But you should start with a text editor instead,\n
first of all testbed for Windows, I really\n
not available. In this, you can write your\n
you have already downloaded the Java JDK,\n
I assume I should be easy, but maybe we could\n
that you can download your code, you can just\n
NetBeans, if it was soiled, and you can choose\n
you have already done that. And so when you\n
pad, or sublime, you're on. Mac, I'm using\n
button. It's also available for Windows and\n
loaded the class person that we saw before.\n
on person's name, it highlights the variable\n
so heavily assist you, like you might have\n
is actually good, I think. Because when your\n
your brain cells too much. I mean, after some\n
really welcome. But for the beginning, I really\n
you get used and familiar with all these keywords,\n
get familiar with that. And so for the beginning,\n
go on the console and called Java arc and\n
you because in this course really I only focus\n
it really short introduction of the tools\nthat I'm using.
If you have any questions, send me an email\n
let's start with the topic of this video,\n
conditional statements, and Boolean. So a\n
a primitive and it can have two values, Boolean,\n
it. So conditional statements, I mean, there\n
And that is something is executed or not.\n
sense, and you might easily understand it.\n
Boolean? I mean, this is a bit weird, I think,\n
just looked it up on the internet. Here on\n
really not a big fan of mathematics. So let's\n
matters to me is Boolean algebra was introduced\n
blah whatsoever. So it turns out that the\n
guy here. Um, I leave this as your homework,\n
algebra, of course, this is again a big topic\n
Java, I just wanted to help you that you get\n
So this thing here is really helpful when\n
exists, some condition, then only execute\n
also say else if this condition does not hold\n
the if statement is probably the most important\n
programming languages, at least the ones I\n
have a different execution flow each time.\n
bit boring. Let's call this Monday. And let's\n
also have Monday. Okay, so in this little\n
nobody could say if it's Monday, and percent\ntwice
So what these two M percent mean is, and so\n
check if it's true, and only if the first\n
like, is it Monday, which would have to return\n
would execute, go here. And check if this\n
be a function if Is it raining or whatever,\n
block here, starting with the curly braces,\n
want to implement the functions. So let's\n
Just to keep it simple, and then drink beer.\n
Um, but now let's make this more easy to drink\n
lover. So on Mac, I mean, as depends which\n
and seven, twice. This is called the pipe\n
um, yeah, I know this is a bit weird. This,\n
or, again, as before with the two ampersands.\n
short circuit operator, which means if the\n
end, at least, then it's not going to execute\n
that a condition will cannot hold true anymore.\n
not execute the rest, which is very handy.\n
here, is it raining. Now imagine this method\n
really save that time if we already see. This\n
why you use the ampersand twice. And the pipe\n
So I want to even spend time to talk about\n
much nicer. If I have an orange here, if it's\n
So this really increases my chance my chance\n
so much more. I'm just kidding. Um, so this\n
you can further combine that led so I don't\n
say Tuesday and sunny. So we have to introduce\n
Well, if it's Monday, it can't be Tuesday.\n
it's raining, it can't be sunny. So sunny\n
this. Now it's not Monday. It's Tuesday. So\n
Okay, now they're not red anymore. So now\n
if Monday is raining, but it's not Monday\n
here is not true. But here we have an or true\n
So this is also false, which means the whole\n
a beer. But let's make this true. And now\n
a beer Ray. Of course, I mean, when it gets\n
more brackets to really tell Java first execute\n
only execute the next statement. I mean, if\n
logic, you will see there are preferences.\n
don't know them, and if you always just add\n
what you want. Okay, now, let's make this\n
something, something else. And this else is\n
we can do something else. And this is the\n
statement. So here, we can say drink, milk.\n
we drink beer. And on all other days, we drink\n
of milk. So if you I mean, milk is really\n
like this. It's much better. But of course,\n
sorry, else if, um, as you introduce a new\n
Friday. At the moment, it's Tuesday, Tuesday.\n
Drink water. Okay, so what do we have now?\n
but usually, you formatted like this, too,\n
belongs together? Because it starts with if\n
check else is it Friday? If so, then call\n
still drink milk. So, okay, and you can have\n
Else if I don't know some other condition,\n
um, what should we do? go swimming, go swimming.\n
can have any number of L's if statements.\n
course, but you don't have to, I mean, this\n
You just you cannot have AIDS if by itself\n
the whole statement has to start with some\n
enough for if else if and else. Now let's\n
that. Because normally, you evaluate some\n
evaluated, this returns a Boolean. Under the,\n
you could say in E equals to four. And then\n
that. Okay, so we have two primitive data\n
a form of video. And then I can compare those\n
as with the Boolean is it's obvious. But I\n
Normally, one function calls the other, and\n
And then you don't know if the user typed\n
example. I think we know that four is bigger\n
know. And then I could say, if i greater than\n
I mean, here, it's again, you can say ELLs,\n
else can we say, oh, if i equal to j, which\n
This, by the way, you see there are two equal\n
here when we assign the value of four to i,\n
in Java, here use two equal symbols to express\n
also can do so so you see the result of this\n
boolean value are equal to a boolean variable.\n
I mean, here, it's very easy to understand.\n
And then if you give it a name, your code\n
you can say are equal. Okay. And then there\n
And then also unequal, which I will usually\n
same. So not equal, you can say with the exclamation\n
this, it's both the same thing.
I really prefer that one. I think it's more\n
mark, really easy to read. And with this,\n
if this no holds true, when you put the exclamation\n
before was holes. Now it's true. Thank you\n
exclamation marks, and every time the value\n
and false again, and so on. I mean, this is\n
Just to be complete. I wanted to mention this\n
much wraps it up. We talked about Boolean.\n
code that is in here. You see it can also\n
I mean, I would really recommend you to write\n
of course, you don't have to have an method\n
because it might be much more readable. But\n
i equals to eight, for example. J equals 299.\n
or plus class, the increment operator that\n
five. So if j with three before, when this\n
it was three before it will be two. Okay,\n
you cannot do is compare a Boolean to another\n
make sense what you can do, but, and this\n
a boolean value let's make this Monday again.\n
equals to true, what does this mean? I mean,\n
this works. And if this was false, if false\n
So the whole thing is false. But I mean, this\n
this is enough. Yeah. But really, comparisons\n
often in code. So if you see that, you're\n
it. If it's true, then just say, if Monday,\n
not Monday. So, see, this is not. Okay. Lesson\n
This is really exciting, because it's a very\n
task much faster than any human could do.\n
powerful. So it's very important to know how\n
types of loops. The first one is a for loop\n
times, execute something. So this is why for\n
a bit cryptic. I'm sorry. The reason is because\n
That was also introduced before Java, actually,\n
c++, which are much older than Java, actually.\n
took over the same style. So this is why it's\na bit cryptic. So
we have three sections here. Let me first\n
the three sections that this first loop usually\n
int, and assign it a value of zero. calling\n
because I just for integer because the guys\n
mathematics, mathematicians. So yeah, they\n
so it's so common. I also used AI, but you\n
people would expect you to call it AI. In\n
AI or just why are some some variables short,\n
our counter variable that defines how many\n
Our statement that we want to execute is actually\n
do this four times. In programming languages,\n
don't start with one, but they start with\n
we have to make sure that we stay that I stay\n
this is four times. And each time when we\n
incremented by one, see, this is the plus\n
and then the next round, it will be one, one\n
two is smaller than four, three is smaller\n
So for the fifth time, the loop will be exited.\n
the cursor kind of, and we will not execute\n
And then this should make sure that four persons\n
this variable is defined within the block,\n
in here, I think I showed you this, see it's\n
could do is we could put the definition of\n
access person one, but actually only the last\n
be overwritten by the next one. I mean, for\n
all we want is we want to calculate the number\n
works, as well as we could also define person\n
case, because this is a static method. We\n
now, let's try that code. And it's green.\n
darkness does not work. Because see we expected\n
constructor was called because this is 10\n
for loop. The regular for loop usually looks\n
name like I, you assign it zero, and then\n
case, 10. Before we head for so that the test\n
these blocks with a colon. And then you increment\n
in here. Like you could say, I equals i plus\n
something. See now we only created two persons,\n
which means the number of times that we were\n
of course. And if we add four or even I don't\n
Let's try that. Yeah, see actual one. Okay,\n
but the default is to just increment a counter\n
with the default and do it like This, I could\n
don't even have to do all this can have this\n
you can try this at home, because this will,\n
but it is valid. Okay. But this is how it\n
the second loop, which is the while loop,\n
And each of these loops have their specific\n
you could use always just the one loop like\n
you want to count something, I mean, this\n
at once, if you want to do exactly the same\n
to zero. Well, I smaller than four. And then\n
So this is exactly the same. And you see,\n
while loop, you would usually do one, for\n
dynamically calculated, like we had the function\n
function here, like should drink beer. This\n
I have not defined those variables here. But\n
function that is always calculated, you will\n
could also create person, and you don't have\n
method sometimes would return false. And when\n
Okay, but let's go back because actually want\n
Because I promise you we can do exactly the\n
this probably will not hold true anymore,\n
So let me remove that code. Okay. Let's check.\n
seems like this is okay. So it's the same\n
also another loop, which is called This is\n
as far as I remember, with Java five, this\n
I'm sorry I cannot show you this one. Because\n
and collection which I will do in one of the\n
another loop the coolest one actually. But\n
to two different loops. Well actually yeah,\n
also do it kind of let's say backwards. So\n
regular while and the for loop I showed you\n
hardly ever see because Again, it's a bit\n
is it executes this statement the first time,\n
So the condition is checked only afterwards.\n
So what we do here is we assert that number\n
is, this won't work, we have to sort it afterwards,\n
even knowing how many times we have created,\n
that I is smaller than four. Let's try this\nand remove that.
See, this also works. Um, this depends on\n
use that. But actually, all the three different\n
you can do everything with each of those loops.\n
depends on what you want to express what's\n
when you're like, did a few months of programming,\n
hang of it. What's like usable in which situation.\n
to arrays. Arrays are also a very cool feature\n
appear in Java. I kind of unofficially introduced\n
talked about loops. And we stopped when I\n
that is specifically designed for arrays and\n
you to areas at least. And so we can also\n
really like container, a box, or let's say\n
is a data construct that can hold a number\n
I already said, box, so far person, the way\n
which symbolizes something like a box, I think,\n
put our persons kind of in in here we could\n
a number of persons, I will call this variable\n
there comes something new again. I don't use\n
brackets. This does not call the person constructor.\n
be working yet there's already a thread on\n
here. This is a specific syntax that says\n
hold a number of persons. And we have to now\n
a red underline now it's gone. So now Java\n
can hold four persons same thing we can do\n
to press out five and six for the square brackets.\n
which is also you know in this primitive but\n
again, I'm not creating an end, I'm creating\n
can also see that it's an object, because\n
button, I see a number of methods that I can\n
this is an attribute, which I can directly\n
said you should have your attributes private,\n
actually violates that rule. And yeah, so\n
us one place where Java itself violates the\n
we just have to live with it anyway. So what\n
like the the length of the array than the\n
year, why intz dot length should return three.\n
it for, let's remove the end. Because I want\n
this error. And the way this is done, again,\n
is I say zero here, oh, I said person, of\n
we have only this one object, persons. And\n
it's a person airy object. Okay, and here\n
does is it creates a new person object in\n
which is like a remote control a way to access\n
which is the first position. Let's copy and\n
one, position two, position three. Okay. If\n
have, I would again, get an error, which we'll\n
which is again, a new big topic. Oh, okay.\n
can also do is, you can do crazy stuff like\n
in the person's array, at position, whatever,\n
you write on every ad position, as it contains\n
would say, person, my person equals to a new\n
object was assigned to this variable to this\n
this. So now, this variable can access that\n
at position three, can also access the same\n
like remote controls to this one object. Yeah,\n
say, my person equals to I don't know something\n
means the object that is at this position,\n
reference variable, my person. So this, my\nperson object now
should still be here, if I'm not wrong, I\n
I'm just explaining this so that you get the\n
too complex, because you see, the more complex\n
so I hope you get the hang of it. Um, one\n
one forces you to, to really define to set\n
out position one, this position is empty.\n
position one, there is now because knowledge\n
there is no reference. If we access it, we\n
what to do. Okay. So that much about now,\n
way, we could also now it, it's called, the\n
also write, so this is like, put nothing here.\n
you have the new person object at position\n
away. And if no reference exists to an object\n
a garbage collector, I will talk specifically\n
a specific lesson only about the garbage collector.\n
that object, we'll clean up the memory. For\n
In other languages like c++, this doesn't\n
up the memory by yourself, which is really\n
features of Java. But anyway, I guess I'm\n
again. Because what I actually want to do\n
that with the arrays and introduce the for\n
of all, go to the regular for loop, take that\n
all that all we have is an object of person\n
loop that may create for whatever in this\n
so because I will change while we go through\n
we can say persons at position I this is really\n
one line Let me clean this up. So we create\n
put it into the array dynamically add position\n
array with persons. And what we can also do\n
same loop. We can access this object by saying\n
on it like hello world that we created in\nan earlier lesson.
Um, okay, I think I have not introduced you\n
maybe I should not do this now, because I\n
stuff with Aries, but would be handy now to\n
of something for each person. But maybe I\n
believe me, if we would print that, we could\n
for each person. Okay, this is already nice,\n
loop. And this is not so much different from\n
in Java five. And this works like that. person,\n
what this does is it automatically under the\n
it does. And now we can also access person\n
now we are using it. So this requires that\n
so this is only for like reading it. I'm fulfilling\n
Because you have to specifically say what\n
this would also be empty. Okay, so but we\n
how much nicer This is really condensed. So\n
loop is like besides areas, there's also other\n
also soon introduce, like collections really\n
than just arrays. And so this for each loop\n
that is. And it does whatever is required.\n
it's really smart to use that for each loop,\n
also because it's a performance and proper\n
you in doing it. Okay, so I think this pretty\n
a not sorry. I didn't know Have I told you.\n
Person start length. This attribute which\n
be public. This gives us dynamically the four.\n
just imagine what can also be yours, you have\n
you don't know its size. So you couldn't say\n
lengths that you need. And as you see in this\n
dynamically calculated by Java for you. Which\n
in the background. At least this is what I\n
you to enums. enums and Java represent an\n
they were introduced in Java five. Now, I\n
arrays because there is one more cool thing\n
We can actually even on the fly out five,\n
already PR Since two equals two, and now I\n
is alt eight and nine on my Mac, by the way,\n
This actually also works. So what does that\n
this says there should be an array. And we\n
the size is automatically calculated, because\n
that would be the same as saying zero, which,\n
technically, it's possible. So and we cannot\n
the size, this gets calculated automatically.\n
if I want to, I can also create a new object.\n
also just to show you what's possible define\n
now to it or talked about now, which is like\n
is the same like that. And I mean, I want\n
does not or what is not is, it does not like\n
it says what you have at position zero of\n
object. Take the reference to add and the\n
position as well as later on, persons, zero.\n
if we say, person to zero, that will be exactly\n
point to this object in memory. All this is\n
follow me. And I will do a specific tutorial\n
things look in the memory. For now, I just\n
I mean, we're talking here about areas and\n
is an array and this is a way of defining\n
Don't worry, we can go in more detail later.\n
soon I'm sure let's do the same trick again\n
even do it twice or any number of times can\n
put this or we can also remove them now put\n
can also do it on the fly you can say new\n
which is this object that defined here.
It sits somewhere in the memory and might\n
outside of my array there is no one who has\n
we have defined it. We could also say I have\n
correctly if not imagined I had counted correctly.\n
cannot copy this reference and I can say my\n
just VIP. So my VIP, now I have a reference\n
dirty on the fly in this line together with\n
just for the sake of it, I assigned it a variable,\n
like hello world, which you can also do is,\n
So you can really work with the areas at specific\n
it's exactly the same thing, even though it\n
to it. Okay, anyway, I think this is a very\n
shorter This is here we find just for persons\n
five lines. Now in just one line, we have\n
filled it, and this is all in one line. And\n
is the most important thing. So it's not about\n
it short, if it keeps being readable, or if\n
And I might also already introducing new stuff,\n
is another variable modifier. We already talked\n
something new final says are like locked in\n
around with that variable. So if I later after\n
no cannot assign a value to a final variable\n
you see, I have to say final. So this is one\n
to read. This is one more keyword to write.\n
for a reason. And if you have code that is\n
that, I would assume you know what you do,\n
don't want to just one line later. Where this\n
some constant values, some status values,\n
that operates on some values. And then you\n
in Okay, let me give you an example. So for\n
would do it in the class on the class level,\n
for all objects of the class. I mean, and\n
talked about this already means we have just\n
final. So static and final, they're really\n
because I want to have a string. But actually\n
we said the braces right after it so that\n
and I call it my state values equals and then\n
like pending, processing and processed. I\n
many more. Just for now. I have a short example.\n
wrong here. Yes, comma. Okay. And this means\n
didn't explain you why I did that. So far,\n
in lowercase letters here, use the uppercase\n
again, a best practice, because like, we would\n
in the code. And you see, now the definition\n
a programmer working in here and knows what\n
I don't want you to touch it, I just want\n
capital letters, and we separate them with\n
case. And then we could, again, of course,\n
so let's do the for each loop, say, stream,\n
braces again, we could say, if we're talking\n
actually we can't on a stream, say equals,\n
the equals is working on on on primitive values\n
is directly in the variable. Oh, no, sorry,\n
know if I, if I'm making it too complicated.\n
well continue what we have to say for if we\n
so we can compare that to some other value.\n
some other value. So this is some that equals\n
the string class that we haven't written,\n
compare this string to some other something,\n
same, we could do something. And then we could\n
So we had, as far as I remember, something\nlike pending.
processing. And, of course, uppercase and\n
it was processed, but I mean, this is just\n
much. So, and then we could do some some something,\n
method and the third method and so on. Okay,\n
is a faster way of doing that. And this is\n
to talk about. And second. This string here\n
it any time. You could say in here, state\n
few lines. I'm sure no one would ever do that\n
see a very short examples. We could have like\n
1000s of lines and we could like call other\n
then if someone in some other class, like\n
And you don't want that. You want actually\n
doesn't work, because final only protects\n
in here, which you can change any time. And\n
us to define an enumeration. Oh, okay, oh,\n
for the time being. But let's jump over I\n
will be our enum and it will not be a class.\n
than 10, I have set less than 10. And now,\n
we would say public class, logging level
races. But this time we're not doing a class,\n
is the enum. And now this is obligatory, you\n
And it's not a string. So we don't need the\n
Now this is really a value and enumeration\n
like we like program our own type. And we\n
had processing. I think we had processed.\nYeah, and then we end
And now we have defined an email. And we can\n
say logging level logging level, or let's\ncall it
state equals to logging level pending. So\n
is already how it came. Because now we can\n
over them. But we cannot change this. I mean,\n
we can only change it to the allowed level\n
We can set pending, processed and processing,\n
It can only have one of the three values.\n
it's not a string, we can only use the values\n
helpful and makes also our code more readable.\n
In this case, it's a bit more complicated\n
which is called values. This method will give\n
values in here, pending processing processed.\n
anymore. We say let me also remove that. And\n
say equals, see this works. Of course, it\n
never be equal to an enum, but it's perfectly\n
object. Of course it makes more sense to say\n
processing and prose So \nlet's put processing.
Let's put processed. Okay? Um, but this is\n
here. This is so cool. Actually, for an enum,\n
equals, again, this does work. Because yada\n
So for now, just believe me it's working.\n
value. Okay. Let's just remove this. Okay.\n
more readable and shorter. That However, there's\n
statement. However, we will not do this today.\n
statement, because I think otherwise, it's\n
about arrays repeated the for each loop. Yeah,\n
this values method. Actually, there's much\n
this one. We can also add these brackets here.\n
put values in here. So what is this going\n
be private. Let me check if it may be default,\n
to be working so far, but I'm sure it may\n
default, visibility seems to be okay. Even\n
see this. It says the end cannot be applied\n
is empty. Because what happens is, this is\n
and now it's working. So having the constructor\n
to you, of course this is an object. But it's\n
to go in detail, we can still do the comparison\n
so we have a constructor. And let's now assign\n
you see, this doesn't make sense, assigning\n
thought I know, of course, I have to define\n
it's working. I don't like it, I want this\n
And then I mean, this, this depends what this\n
let's have a method that returns public code.\n
really powerful, is allows us in dismissing\n
has these static values that we can iterate\n
could also think of a traffic like light with\n
of course have more values here any number.
I'm actually you don't have to have the enum\n
fly define it in line in a class, I would\n
forever, you're all there is an exception\n
But in 99% of the cases, you would be better\n
there's much, much more actually that you\n
this tutorials already a bit long. And I think\n
further extend this anytime, of course. So\n
a public method returning the I, you can also\n
But if you're if one of them has it, and all\n
appearing here has to appear in the constructor\n
may use it but you don't have to use it. But\n
So I think that's enough for now. And the\n
show you the switch statement. Because this\n
these enum values, then the code gets much\n
statement, which is another conditional statement.\n
first of all, let's recap what I showed you\n
one, I covered a lot of ground, I think, I\n
shortly recap. First of all, I showed you\n
the traditional, let's call it, the traditional\n
square brackets, persons equals to new person\n
this can hold four elements starting at zero.\n
actually is the short form just with the curly\n
separated list, you can define an array. And\n
end, so that you can easily extend the array\n
modifier for variables, a final modifier,\n
for us. So what it does is when in a later\n
let's use this area on top here, this is not\n
variable persons to also we can try to assign\n
also not work. This is nice ish. However,\n
in a method. But instead to use it for static\n
are defined at the top of the class usually\n
So this at the moment is default visibility.\n
And if you need other classes accessing it,\n
So it would be okayish have this public. However,\n
clean code like I do. You could provide a\n
I don't know some name. Let's call it states.\n
we could copy this out Because with a copy\n
does protect is as I set the variable, what\n
safer if return a copy of the array. And there's\n
Java, Java util, which provides some static\n
going to use the copy. But there's also many,\n
search and so on. This is really a big toolbox\n
them. And I might show some of them at a later\n
then we take the array. And we have to state\n
array dot length, yeah, we also talked about\n
length of the array, which in this case is\n
So this is really a nice way of
generating clean code and returning this array.\n
and using the underscore and not written in\n
a static final. So in short, you just talk\n
write it. Okay. Let's go back to the bottom.\n
also very shortly talked about equals, because\n
And you if you want to compare them, you cannot\n
going to work, even if it's the same. Because\n
compare objects. And this is an object, instead,\n
does work for int three, and then you can\n
in here. But if you have a string, let's make\n
or now in here is the reference, which is\n
a number also will be saved in here. And when\n
signs, then you compare this variable, which\n
the same. So what will be true is if you would\n
usually is not what you want to say. And so\n
say equals talking about equals usually in\n
about the hashCode method. This is already\n
very short for now. It just that if you create\n
it's often a good idea all depends, but you\n
in your person object so that you can compare\n
to implement the hashCode method. What the\n
explain at a later time. Okay, so that much\n
thing. I made this now because I also wanted\n
people that would write equals like this,\n
This is not so nice, because as you see this\n
And of course, we don't like arrows. So instead,\n
never be now, like this string here. I mean,\n
be now. So ACB equals now, we'll just return\n
an error. Okay. So let's stop here. Then,\n
we talked about enums, which I introduced,\n
go to the class. So public enum logging level,\n
Because it's not a logging level, I was thinking\n
But then I decided we haven't talked about\nlogging either. So I
instead took pending processing process. So\n
I don't know, let's leave the name for now\n
that are introduced, which is a bit similar\n
also has some features a class does not have.\n
let's call them labels that we can act upon\n
pending, to this or that, so it's not only\n
introduced was Java five. And before that,\n
if something is equal to not to zero, or one.\n
these values, this really gets nasty and problematic.\n
you to do it in the proper way. And that is\n
You can you don't have to also introduce a\n
for each enum value. And you can provide methods\n
you want, which are also like to make the\n
not always. Whenever your code and you design\n
don't make a decision just because this is\n
the code you work on, for example, the pending\n
that it might be nice to add this method is\npending.
And then you can say return this, which is\n
the current value of the enum equals equals\n
let's do this modern level, still I call it\n
take processing. And then I could say if stayed\n
Some code would have to be executed. But what\n
if you really need it. So it would not be\n
People usually get lazy and when they see\n
stuff and they just like I might be I might\n
no, never do that. Never add a method just\n
code lives. And you can always change your\n
later. So only edit when you need it. Let's\n
remove it. Okay, I think this pretty much\n
much more crazy stuff, like each value could\n
ever use it. So let's not show this for now.\n
usually. And now, let's finally actually go\n
switch statement. So I think I already a bit\n
we want to shorten this here. So the switch\n
And that's pretty much exactly the same, like\n
Okay, I mean, don't think about the exact\n
explain you something. Okay, this time, I\n
on this state here. And then we can instead\n
operating on that value here. And then, instead\n
that the state has this or that volume, case\n
equals pending. If you just have one, then\n
statement is usually used when you iterate\n
this extra of saying switch, but then the\n
it will get processing case process. Now I\n
Let's format this. Now what happens is, I\n
put a break statement here. So actually, what\n
method call, I mean, I have not implemented\n
was a method. So for all these three different\n
would call this method. If imagine we use\n
you could also like return a value. So this\n
you return something from a method, of course,\n
this out is because normally usually, what\n
might not even return something like printing\n
one method. And in here, you would call a\n
or so. And here a third one. Now it gets problematic\n
happens is a fall through this will be cold,\n
a big source of bugs. Very, very dangerous.\n
if sometimes you'll want to have them together\n
example or to to call a certain method it's\n
so that you tell the next program I know what\n
So that much about follow through. And you\n
can also leave One out, if nothing is found,\n
statement, and we'll just not do anything.\n
possibility to adding a default, which will\n
apply. And then people sometimes say, I don't\n
want to warn you. And of course, again, we\n
This is not so wise. The problem is, I mean,\n
from extending your enum adding, for example,\n
would just do this would not know there is\n
the switch statement, you didn't think of\n
a specific behavior. But now you would also\n
So usually, we don't want that. So what I\n
you don't use it, or even better use it. But\n
not doing this now, because I haven't talked\n
time. But this is the place where you would\n
thing about this is when someone extends your\n
one or more switch statements, you will see\n
program crashes. And this is so nasty. Well,\n
design. And this is called fail early, it's\n
production even. And to have to fix this within\n
high severe bug. And no one notices it. Because\n
something in a wrong way. But a client might\n
and then the problem will be so so much bigger.\n
to be brave. I also see people that try to\n
for example, return now. So this is actually\n
wrong. And let's fix it. This is just like\n
it bigger. So I want you to be brave and throw\n
better. I mean, I'm not saying you should\n
and concentrate. But sometimes bad things\n
early. And so you you notice the problem and\n
hidden bucks. Okay. I hope you got my point.
so also here. Well, if you have an exception,\n
always leaves the block. But what is important\n
don't have to break, leave a comment for through\n
doing, I want this and Tommy about warnings.\n
don't overuse the if statements, these conditional\n
also in combination with for loops and see\n
have to understand this switch. And then there\n
then inside of the case statement, there might\n
complicates the code. Same as with the if\n
for me to enter this blog, ha, then this method\n
you might want to check here. So this all\n
is also like a, let's call it a relic from\n
code. Because it's like a procedure, it's\n
a machine, like a machine would switch a state.\n
that. But humans usually don't, they don't\n
object oriented programming is that the object\n
humans think. And this allows you to like\n
your code nice. And switch is not object oriented.\n
it might just be easy and fast to use it,\n
are people that would have maybe hundreds\n
hard to read. This is also why I had here\n
better than just having huge switch statements.\n
you could have an object representing the\n
the process state and so on. So they are at\n
statement. And it always depends, like, what's\n
It might be okay. Just remember, they are\n
Okay, I hope I have added enough warnings\n
one reason why I'm only introducing it now.\n
oriented programmer. I will further continue\n
you that it's used to iterate over the different\n
you were introduced with Java five, um, before\n
you had to emulate enums. And the way this\n
is not so optimal. And this is also why enums\n
And I would also not recommend you to show\n
I already said that in the last episode. I\n
I think I should still show you. Because you\n
can understand code of others, I will show\n
with that warning, I'll show you. And now\n
And now I give it some number 42. I mean,\n
And this is already your problem. 42. What\n
mean processed? How do we know? Well, to make\n
final values, which I'll do right now also.\n
Let's introduce now this is a method. Let's\n
it processed, processed. Forgot to give it\n
is the 42. And we could also have pending\n
is also where a normal use case From. So this\n
letters. Okay, because it's a constant value\n
this the name of pending cross cutting, let's\n
course, we need different values, that could\n
would be a 42. And now I can copy that, go\n
equals pending. And pending now, I think it\n
And now this is funny before depending was\n
to realize, now we're not iterating on the\n
have processed there, as we have named it,\n
show you, this is just because we named the\n
is not used, called is something then of course,\n
because now you see pending as red, because\n
not on the interim anymore. We only int something,\n
one guarantees us that only these three values\n
time could say my state equals to 300. And\n
be processed, it will end up in the default.\n
should not have regular code, but throw an\n
very soon, I hope. Okay, so anyway, this is\n
Java seven, I mean, at the time I'm doing\n
when they introduce Java seven, you now can\n
called also my string. Okay, not used yet.\n
strings. Of course, something doesn't work\n
now, I can of course start to use. Well, for\n
I can use, again, something in lowercase letters.\n
labels, which then of course, are private,\n
don't know. I haven't used pending because\n
And I will call this pending. But it could\n
So of course, using that label, I'm only using\n
Okay, so now, that label I can also use pending\n
session I talked about the fall through, which\n
So if it's this case statement will be the\n
then call this method and then break is called,\n
the court will continue online 122. The default\n
anywhere else. For example at the top which\n
not pending or something, or pending is, I\n
I think it was 42. So, in this case, all the\n
which he or we could have some other method.\n
method. And then we could say break so that\n
called. And then we would end up here, leaving\n
see, if I put my cursor at the beginning of\n
don't really see it ends up here. Because\n
why you should keep your methods very short.\n
141. And this method now got so big, that\n
where it ends. So yeah, I can't say it often\n
keep them at one to 10 lines at most. I'm,\n
at one to three lines. Some say I'm really\n
will very soon see the fruits of your methods\n
And also, the easier it will be to reuse them.\n
switch statement. So you can use it with strings.\n
like I showed you in the last episode, you\n
So far, I've only showed you int. I'm actually\n
really talk about the others, because they're\n
the number ranges just a bit smaller. In 99%\n
it doesn't really matter these days. how large\n
not but maybe I still show you. For example,\n
not working anymore. Why is it because I have\n
So the bite here. This is just smaller than\n
it says this is incompatible, because it's\n
the three works. But I really don't want to\n
you can also read this on the internet. Because\n
is the end. So okay, I think this pretty much\n
So you can use it with a string and an int\n
you. That's also interesting. And this is\n
while it's not a string, but it's very similar.\n
the double quotes. So if I'm using the double\n
doesn't work anymore. Here it says incompatible\n
quotes, it's a car. So it's a character of\n
it could also be a or b. But it cannot be\n
not a character anymore. Too many characters\n
iterate over characters. This sometimes might\n
a string or what is parsing it's have a string\n
can take each single character, because a\n
characters. And then you can iterate over\n
this. Um, yeah. This you might need at a later\n
told you there are other number types. I would\n
Well, first of all, let's tell you what my\n
here. I have prepared a little class car service.\n
And in the class, this class so far is empty.\n
like a print out on the console into a file,\n
So I want to be notified in some way I want\n
that I, like later on, can go through all\n
For example, if my system was hacked, I could\n
And then yeah, analyze it. So that's the idea.\n
actually, logging for me this word was a bit\n
friend. So let's do this. So logging is called\n
this thing here, you know, of ancient times\n
and was writing down at what time he was at\n
in that direction. So this is a logbook. So\n
of the ship. And but still I was asking, why\n
article here have a read just very fast going\n
to a book for recording readings from the\n
and ship block, also called common lock shaped\n
marinas used to estimate the speed of a vessel\n
an image. So this thing here is the shape\n
even a picture they were throwing out the\n
not in this line in this long line and the\n
this chip block is taken away. And by measuring\n
this was done given by knots. So this is still\n
today. So here it says ship log and associated\n
So it seems I don't know this, but I just\n
really looks like a lock, like, like a three\n
would guess that this is why it's called lock.\n
lock. And this is why the act of doing like\n
logging. And as a programmer, you can think\n
And as such when you want to write on events\n
you are logging and this is what we are going\n
and 15 Well, currently you would use SL for\n
of course, you could write your own logging.\n
work properly. And this has been done already\n
Well, actually, this is only for Sade, well,\n
about. So two, we could say it like, allows\n
the code, Ork SL for j logger and logger factory.\n
logger, logger, factory, get logger. give\n
it with different implementation of logging.\n
logging framework was log for J. Well, this\n
better use lockback because it's a more powerful,\n
it properly. But I try shakey Goku, and this\n
love for J. So he continued with lockback,\n
So today, therefore, we use SL for j in combination\n
visade. will allow us at a later time, if\n
that is even better than lockback, we can\n
having to change the code. Because the code\n
SL for J. So how is this done? Let's jump\n
you, I'm using Maven for the configuration\n
to use the dependency org. So for j, and lockback,\n
of SL for J. And this is the core code of\n
to get logging in your code. Okay. And then\n
our file called lockback, dot XML. Here, you\n
all, you can say, how should you lock where\n
this class console appender, that will result\n
There are also other appenders, or other implementations\n
to the database, to email. And if there's\n
doesn't exist. You can also implement your\n
like logging into Twitter in some way, if\n
the pattern, how, when a message is written,\n
like so the patterns has, I mean, you have\n
it's like, give me the time when this event\n
the logging level is like a mechanism. It's\n
levels of logging. This is like a signal of\n
is probably for debugging purposes, use that\nsparsely.
Then there is a logging level info and error.\n
in your code, info for general information,\n
But this really should be used partly because\n
like in your ID, or we haven't talked about\n
Now there are different levels. So, in your\n
on level debug. And here I have defined the\n
when there's some logging of a class that\n
if it's debug, I could to also put your error,\n
it. Or I could put here info, that would mean\n
also, it would include arrows, because this\n
debug, info and error, but error would only\n
So you see, this is really makes it more flexible.\n
for different packages. So we could also copy\n
like Java course two, and so on. And we could\n
other one on error. So this is really flexible\n
root level. So like, generally lock on info\n
level, this is what it means. Okay, this was\n
of course, on the internet. But first of all,\n
login details here into car service, I put\n
Like we saw just before in the documentation\n
for j because I said we want to import only\n
it should be used, I should use lockback because\n
This one here. So SL for j is really smart.\n
SL for j lager and we say import org SL for\n
factory, by the way, is another design pattern.\n
course, only on design patterns. design patterns\n
program code. And when you know what a factory\n
he does. Okay, more about design patterns\n
to do. And now in our class, we would probably\n
because it's all log. And then we say logger\n
does like a factory. This is why it's called\n
know how is it created on the fly is a longer\n
the necessary work for us. Now, we should\n
this will be used car service class. That\n
is in source main resources. As I showed you,\n
even extend that and lock only for specific\nclasses.
So this is the test and this is the service.\n
logger. Now, in here, you can now use it log.\n
as well have this in lowercase log dot and\n
of you might know it, you would usually say\n
enabled, only then log something, log dot\n
plus input. So the input given in our case,\n
will be concatenated. together. And all this\n
at the time when the code runs, which at the\n
can activate and deactivate this. The reason\n
do it without just like this. But this is\n
it's not so bad. I mean, all that would happen\n
together. But you mentioned the string could\n
So Java in the background for you, this concatenation\n
have some very big object here, that might\n
so on. So this creation of a string, as stupid\n
And, of course, I mean, here, we just have\n
debug line. But of course, you would log maybe\n
error, maybe a lot of stuff in your application.\n
calling this method. And then if this only\n
200 milliseconds might not seem a lot of time.\n
can really consume a lot of performance of\n
to concatenate the string, prepare the string\n
because if we say log info, but then the logging\n
to be locked, it would have to throw away\n
we had done this without any need. And so\n
why in lock for j, you would do it like is\n
info enabled is error enabled, and so on,\n
SL for j However, there's actually even a\n
lines of code. And this is you can put here,\n
to say plus, but you say comma. So within\n
the user in this case, this would be us wants\n
enabled for this class. And if so, only then\n
not a string already, and only then concatenate\n
a smart thing now. Okay, so another enough\n
check. We have the input given from the process\n
we put here BMW or Beemer. Okay, so let's\n
XML. We said comarca spiele. java course,\n
So if we no log on debug, within this package,\n
package. This means we should log it. Okay,\nlet's run the test.
Okay, so something happened. Process finished\n
at this time, which is now on level debug\n
we wrote processing car column BMW. So this\n
it. Let's say we only want to log on error\n
Run. Process finished. On top, this is all\n
Well, you see there is no BMW locked. Okay,\n
is debug. And we could say or, let's have\n
let's change the package. For example, Java\n
too. But we are in the package called Marco\n
log on the root level, which is debug. So\n
try it run. And there you see it already.\n
processing car BMW. So this seems to be working.\n
This is what's compared. Here, we have the\n
the message, and then this n, I think this\n
next lowing a new line will be attached. Okay.\n
is the console that we can see here, in our\n
with like, for example, a terminal here, you\n
So let's maybe go back to the documentation,\n
you see, and you should really use read it.\n
you can read it typical usage pattern for\n
with the curly braces, we locked on debug.\n
the documentation, let's also log on Arrow\n
here, it's also explained what I showed you\n
time. So it says here lockback Classic, you\n
framework, which we used. Yeah, so really\n
see the dependencies, this is what we did.\n
lock for J. But lockback, as I showed you\n
is also documentation of lockback which should\n
author is chechi Goku, and others. Yeah, so\n
12 years. I mean, you wouldn't want to work\n
three guys. So this is a lot of work and brain\n
which you can use just by configuring it in\n
also done it import org. So for j logger,\n
And here they log on info level. Okay. And\n
also showed you Okay, and there are also other\n
see, oh, there's also the explanation you\n
I showed you, or you see better alternative\n
basically, it's the same thing. But this is\n
easily. And this is only one line. So it's\n
that you want to focus on the domain, on the\n
just not be in the way of your business. The\n
important. Yes. But it's like not as important\n
go through all the documentation. It really\n
I promised you, and then we should finish\n
like you can see error. Well, as, as far as\n
error is included within debug, because debug\n
also look just here, I would expect to see\n
we would use this in case an error occurred.\n
some logging event is. Now you see an error.\n
Well, in our case, this is not an error. So\n
can we use? Let's see, we should also able\n
would use a warning for something very close\n
interfere, then you could use one logging\n
when your server starts when your program\n
knows everything is fine. And like here, this\n
is just starting up. Okay. And here you see\n
So now that we're using one level, let's put\n
that we're not using as well as the root level\n
out, which is this appender the console appender\n
I would not expect to see any logging. And\n
logging is gone. Which again, we can change.\n
Here also we can say worn. So now we should\n
Okay, well, as I Oh, I think I promised you\n
So let's finally go back to Firefox. And see\n
this is not yet. But it's somewhere appenders.\n
is really a good read. So I'm really fast\n
appender. This one we used. File append Not\n
into a file and go through it read it. So\n
parameters. And there are some really cool\n
that means that new files are created when\n
for every day we could have a new file. This\n
if we're always writing into the same file,\n
tons of gigabytes. Yeah. And therefore on\n
a rolling file appender so that thing is really\n
ODP appender that is supposed to To log into\n
this is really very easy to be configured.\n
somewhere somewhere, maybe I've already missed\n
from the name that is used for logging, sending\n
that was also the DB appender. I know it is\n
There it is. So this allows you to directly\n
databases are supported. So this is really\n
still goes on. So there are really a lot of\n
And as I said, you can always extend it and\n
to lock to any other source. So that pretty\n
you the main method of Java. The main method,\n
used to start a program initially. However,\n
or TDD In short, so far, we actually never\n
a test. And so far, we use the test to execute\n
guessed, at some time, when you're done with\n
And for this, you will need a class that has\n
look? Well, it's not so different from any\n
it can be started from outside of the class.\n
run the method without having an instance\n
Because the class car selector in this case\n
frame that we use to start our program. And\n
need further instances of this class. And\n
static by definition. The method is void because\n
be made. So that it can be found by Java.\n
say noticed by Java. Well, actually, what\n
the compiler will compile the program to bring\n
is a big topic, there are huge books, if not\n
so you could even study compilers. So I tried\n
But maybe, if I keep talking about Java, executing\n
and at a later time, I might do a dedicated\n
that you will know more about compilers. So\nanyway.
Okay, so back to the public static void Main\n
they have to contain something, because when\n
to give it arguments that the program might\n
are arguments that come as input as strings,\n
is an area here. So square brackets, and all\n
any name, people usually would set here arcs.\n
I usually prefer arguments, because this is\n
always, for our method, you would have the\n
curly brace. Um, yeah. And that's already\n
empty for a moment. And now when you compile\n
you can execute this Java class. And it will\n
So this is the place where you like set up\n
created a car service. So we can use this\n
technically, it also works to run just the\n
So let me directly say new car service, no\ntitle car service.
And then you can call the method, car service\n
an input. By the way, just to show you what\n
on the fly, you can say new car service thought\n
will then just call the method, and you will\n
If you don't need it, that might be fine.\n
And I mean, we could use any string here,\n
interesting, we could no, take the string\n
Of course, we need a for each loop, which\n
string argument for one, colon arguments.\n
will be given to us. When the program runs\n
That was formatting it. Okay, and now I can\n
the process method, dynamically creating an\n
object. So I mean, I wanted to show you that\n
probably be smarter to just create one object\n
this would be from the performance perspective,\n
about performance. Because the compiler these\n
stuff. And when before a while it compiles\n
performance is a topic by itself. And you\n
saying this will be fast or slow. You can\n
could also do a course only on performance\n
an instance of car service. And in the main\n
for each argument that is given to our main\n
car service. Now let's execute this. Because\n
Well, I mean, in this episode, I want to show\n
still holds true, you should always start\n
see, now I have a run car, see, like the domain.\n
Here. This shows me the console directly in\n
printed. This could either be because in the\n
could be deactivated, but so far, I remember\n
the moment. Well, the reason this doesn't\n
the reason is, well, we didn't give our program\n
this is not throwing any exceptions. By the\n
But it's empty. So the for loop will actually\n
When the program is run, so we will directly\n
the program will directly exit. But we can\n
on Run, and I say Edit configurations. And\n
this also contains always the full package\n
program arguments. So on the console, that\n
and then the program arguments. So let me\n
Mercedes. Yeah. And then Okay. And then let\n
see, we have three warning loggings processing\n
commerce. seeders. Yeah. So that's the main\nmethod.
one more thing just really slightly mentioned.\n
know is you can do this. Now what does that\n
This thing with the three dots here is called\n
which means when this is defined like this,\n
method, while in this case, it's directly\n
would have the ability to directly put in\n
by the compiler to an array. So this is very\n
convenient for whoever calls that method.
Just so that you have seen this would also\n
sense because, well, Normally, you would not\n
So if you don't call the main method directly,\n
you can write a test and you can call the\n
make a difference because you could directly\n
but let me talk about Fox arguments at a later\n
So now, string array arguments. Okay. So that\n
of background knowledge for you. So I don't\n
Java course you ever listened to? I guess\n
have heard another course or read the book\n
directly understand why didn't talk about\n
14 already. So let me give you the reason\n
specifically. Because this is really a main\n
is, first of all, I think, in the very first\n
and its keywords, it's hard to directly be\n
is just the name of the method would be okay,\n
All all this stuff for a very first lesson\n
episode 14, you should by now know what public\n
we have also talked about arrays. So I hope\n
and you understand what all this means. Okay?\n
And I told you, I think various times, Java\n
usually focus on creating objects, but static,\n
just one version of this method. I mean, of\n
you should try to avoid static, wherever possible,\n
very first Java lesson might lead you to the\n
reason is, um, even though, I mean, the main\n
because you use it to actually execute your\n
For any program at hand, you would have only\n
consists of hundreds, if not 1000s, of classes.\n
least, I mean, this counts for Java, se. But\n
what you usually do is you don't re implement\n
for example, an open source server implemented\n
I mean, has already been implemented for you.\n
even though it's software. And so the Java\n
infrastructure. And therefore, when you program\n
a main method at all. So, for example, in\n
for pretty much 14 years now. But I have maybe,\n
less than 100 times I have programmed a main\n
of classes, I'm sure. So the main method is\n
with writing a test anyway. So you are also\n
without the main method. Of course, I mean,\n
one. This one is based on the framework j\n
So j unit does have a main method. Of course\n
is the test methods that we write. And his\n
j unit mechanism, bet so we don't have to\n
just using a framework, like j unit is so\n
own main method. Yeah, and I promise you,\n
method as if it was a regular method. Now\n
array of string arguments, BMW and Porsche.\n
arguments as execute the test. And yeah, you\n
as one last thing, maybe you'll see I could\n
could say the method returns some end. In\n
not make it static anymore. I can change it\n
it just will not be recognized as a regular\n
to execute it as a Java program, but our test\n
now this is returning something. So we don't\n
use equals to Yeah. So you see, it's really,\n
And we can still call it from the test. But\n
that will allow us to start a program, then\n
I think that's pretty much all I can tell\n
that we were finally able to have a look at\n
that I've done so far, I think this course\n
should really know quite a lot about Java,\n
by now, I will, of course, add a few more\n
to write your own small programs. I would\n
So first of all, let's have a look at some\n
exception handling. And then later on, we\n
And I'll show you how to do Exception Handling\n
the Wikipedia article about exception handling.\n
the definition. So here this article says\n
to the occurrence during computation of exceptions,\n
special processing. Okay, let's stop here.\n
very important is two things. First of all,\n
in my opinion, this is something that many\n
is called an exception, because it should\n
really is what is an exceptional condition.\n
is something that really, as the name implies,\n
really rarely, only in very specific situations.\n
So for example, programming errors, you when\n
forgot to like you have an array, and you\n
about that means it has a field that is set\n
field, that, for example, can throw a so called\n
really an exceptional case, which you wouldn't\n
would be one reason for an exception. So exception\n
of responding to the occurrence of exceptions.\n
a way to react on that to handle that. Some\n
program flow. But the cool thing about Yeah,\n
flow of program execution. because ideally,\n
the exceptions. And so we want to leave the\n
take care of this exceptional case. Okay.\n
programming languages, which gives us a hint,\n
Well, let's have a look at the history which\n
developed in Lisp in the 1960s and 1970s.\n
was invented by Conrad Sousa in, like 1943,\n
until they finally came up with the idea of\n
is really necessary. And, well, maybe a bit\n
with Java, there was some something new introduced,\n
exceptions were unchecked, which means any\n
And there is no mechanism that forces you\n
for example, causes the program to stop. And\n
like introduced, many times nasty things happened.\n
or something, they thought, well, maybe we\n
of all, let's have a look. Check the exceptions\n
exceptions, which are a special set of exceptions
Okay, and to make it short, so what's the\n
also already an example and i o exception,\n
an IO operation input output operation. The\n
is they must be declared explicitly in the\n
do that, you will directly get a compile time\n
compile your program. I looked at actually\n
Like how it came with the checked exceptions.\n
concept of checked exceptions, checked exceptions\n
must declare the exception in our throws clause.\n
interesting. According to Anvil wrath, who\n
the Java exception scheme should mostly go\n
after some consideration, Java was retooled\n
for which there was no recovery, or reflect\n
the world was safe again. Well, so they thought.\n
like 20 years later, and it turns out, that\n
and this is also why stated at the beginning,\n
handling is the process of blah, blah, blah,\n
And this is the important part. Because they\n
for something like programmer arrows for something\n
and to have the checked exceptions for everything\n
for these cases that can be handled? Why not?\n
why not that instead validating that situation?\n
also, and now maybe we should already jump\n
on too much theory. Let's directly jump into\n
that I've written car stayed with a different\n
written this public static from method that\n
statement. I told you, in a previous episode,\n
a switch inside air to use a string inside\n
still applies. But in this case, I assume\n
And input from the user coming from the console\n
is all I mean, for every rule, there is an\n
rule, which is here, we do have a switch,\n
the default case. And I also told you, I wouldn't,\n
case, so that it cannot happen, that someone\n
then be handled by the default case without\n
And so I told you to throw an exception, but\n
now, and this is quite easy. You can just\n
in Java is just an A class. Like almost any\n
methods and stuff, but at least it's an it's\n
from it. Now, you know, you already see a\n
how the compiler already wants me unhandled\n
also the package java. lang exception. And,\n
Don't ask me why it's called exception. If\n
like a checked exception. I mean, the name\n
have to know that this exception is a checked\n
exception is an is a runtime exception. This\n
to know I mean, runtime makes sense, because\n
so you would normally use a runtime exception\n
them directly. And the idea was, as I said\n
where you know how to deal with them. But\n
why not dealing with them without throwing\n
But anyway, let's continue. So how can we\n
at exception to method signature, let's use\n
exception. Now you see the compile time error\n
like we fixed the problem. So we told the\n
exception. We just don't care. The color should\n
class here. And I made it to use the static\n
now this class has the problem. Okay, again,\n
method signature. Okay. And I guess you know,\n
And again, we have the same problem. Well,\n
Seems like we're on the wrong line. But yeah,\n
signature. Okay. Well, sorry, I'm just lazy.\n
But whenever you write something, you have\n
So whenever possible, I prefer to have my\n
by the ID that can save From introducing bugs\n
will be, like fixed. But now the question\n
exception, we have here throws exception,\n
times we had to add this throws exception.\n
exception. But besides, we didn't handle it,\n
the code, and the default case is chosen,\n
it will bubble up to the top to the main method.\n
the program will stop. And we will see the\n
could do, for example, with a runtime exception,\n
cases, you can and you should add some meaningful\n
So let's say on state, and concatenate that\n
still keep it. But I don't have to. It's not\n
Same here, I can remove all these. And this.\n
differences I mean, I'm not handling the exception,\n
Because he mentioned again, I'm always bringing\n
of classes. And as soon as one method has\n
which means another method, sooner or later,\n
will then also have to say throws exception,\n
or later, you will have that throws exception\n
help us at all. It just makes the code more\n
meaningful. So anyway, let's first of all\n
as exception stacktrace looks like for this\n
And I have to give it some some argument that\n
I think I have prepared something already.\n
driving, driving ABCD E, F, G, because also,\n
the main method only in the prior episode.\n
as some invalid states. Also in lowercase\n
we're iterating through the arguments and\n
just let's just do it. actually still have\n
Okay, so you see, the exception happened.\n
reason is that this happened for the very\n
flying. So this is our program stopped. This\n
we could also handle it to show you what's\n
to handle such an exception? Well, always\n
can do something about the exception. In our\n
that I get a wrong argument from from when\n
this to be printed to the console, so that\n
to be printed here, which means the first\n
only here on the very top. And so there is\n
like telling the compiler, I know something\n
But now I can say catch. And I can catch this\n
like really short variable names, because\n
exception, because an exception log is always\n
exception for with E, for exception. And I\n
we don't want too much with the exception,\n
knows the E is for the exception. Okay, so\n
First of all, we could say e dot print stack\ntrace.
Well, this is actually exactly what we have\n
you can think of as a list of all the methods\n
you see main process from can also jump there.\n
anymore, because I've no edits code. But at\n
was thrown. This told us exactly in each class,\n
true. And he also, but in my car selector,\n
this. The fact that I've adjusted the code,\n
and you need to analyze where it came from,\n
that is running on production is older than\n
development continues. And then you can only\n
happened, if you keep the exact same code\n
but this only really, I don't want to go into\n
its own, again, about revision control. And\n
that, you know, the stack trace is really\n
same code that was implemented at the time\n
both in sync. Okay, so print sectors here\n
it's just the default anyway. But this can\n
in I don't know, was it Episode 13? I think\n
to log an exception, you can do it like this.\n
got the log class here. And I don't want to\n
depends error only when there is something\n
that has to be restarted. In other cases,\n
the string that I added into the constructor\nof the exception
And I have removed the stack trace, because\n
which I want to add. Now. If you want to know\n
13 where I explain logging in detail. Okay,\n
this case, I say I don't want to log it, I\n
think also in the episode about logging, I\n
But in this case, let's assume we have just\n
from the console. And in this case, it can\n
Which I also wanted to show you once print\n
dot get message. Or I can also say e dot print\n
we already have that. So to alter the behavior,\n
you see something else. You see the program\n
state flying Unknown, Unknown state driving\n
invalid states were printed out. Without we\n
it's red, because I use system error Clinton,\n
is a special channel, only for errors. And\n
white. But really, I still warn you don't\n
Because Yeah, you can't configure it like\n
cases, using a logger is better. Anyway. So\n
is this really nice. Now, we have a for loop,\n
a lot of different hierarchies and a lot of\n
And this is something I really don't like,\n
so this is something you should try to prevent.\n
you, we could do exactly the same.
If this was throwing an exception. The only\n
throws throws. So yeah. Maybe also, I should\n
want to add the method signature, the exception\n
and then handle it. And then I mean, handling\n
you have to print it, I mean, printing, it\n
be even better to really have a method that\n
I call no handle the exception. The only problem\n
method now. And this is what I said before,\n
something about the exception, which in this\n
typed in something wrong? There's not much\n
for example, ask the user to type in to fix\n
handle here. So again, if we want to, like\n
the state. So let's remove all that. Let's\n
but I will change it very soon, because now\n
can be done in the following way. I remove\n
argument and now I can implement that method.\n
now well actually I want to say is valid with\n
is also interesting, by the way about the\n
Also, this Method automatically esthetic.\n
method cannot call a method that is not static,\n
when there is an instance. But for the main\n
selector. So for this reason this is static,\n
call it from outside, only the main method\n
Well, for example, we could use a method directly\n
is value of this is not implemented by us.\n
when they implemented when they invented the\n
get for free. So this will directly try to\n
can say car stayed. Cars date. So this actually\n
did. So actually, we won't need it anymore.\n
I implemented is or bill, I did it actually\n
work, this will throw a runtime exception,\n
types of runtime exceptions. And I think it\n
us the argument that you are giving that you\n
it's a runtime exception. So and because this\n
try catch block here, because we want to handle\n
he or I hope I'm right. I can directly say\nin valid on
human exemption, there it is. I think that\n
the exception will not be caught. Okay, and\n
let's see, this is the wrong one, because\n
thrown. So invalid. Let's see what the lead\n
it's an invalid state exception. Yeah, this\n
value off may throw an invalid state exception\n
of, for example, flying driving ABC. Okay.
I think this is also not optimal, that we\n
would be much, much nicer if the IEM base\n
just returning true and false. But again,\n
listen to my opinion, crap, just making a\nstupid joke.
So for this reason, we do have the exception,\n
we converting it directly. The exception doesn't\n
say like this. And we can now say here, return\n
the method. This is exactly the same. Yeah.\n
the jump. I mean, imagine you could have lots\n
just print something. So you could have many,\n
Let's just copy and paste that to show you\n
state to something else. Let's just say now.\n
work out, because the argument is unknown,\n
the execution will jump from here, directly\n
program flow, the normal program flow would\n
here we're jumping from line 21. To line 28.\n
imagine we have done this several times, for\n
methods, then we have just one catch, that\n
an invalid state exception could happen. That's\n
to give you some more details, actually, I\n
We're only like, abusing that volume of method\n
not? Okay, and now I can react on that. I\n
only process it if it's valid. Yeah, maybe\n
the invalid states are printed. Now let's\n
arguments. The question, of course, is, does\n
we have a problem in our code. So maybe we\n
system error dot print. ln M. invalid. argument\n
argument. So this, by the way, whenever you\n
I already talked about logging, but I can't\n
as possible, so that whoever is responsible\n
yeah, it's really hard to understand why something\n
illegal argument exception. How is that? I\n
we can now check. Like, this is what I told\n
have the from it seems like we call it somewhere.\n
five, so this is not yet. Car state. Illegal\n
the invalid state exception. And what is actually\n
So see, this is actually what happens most\n
this is what Java gives us. It throws an exception.\n
See, this is how everyone introduces sparks.\n
not doing a test because I just want to show\nyou exceptions. But
yeah. We do need tests. Tests are very important.\n
argument flying ignoring new argument driving,\n
valid argument was processed. It just didn't\n
we can change. Here we could say system out\n
Only use it in very rare situations. Yeah.\n
again. Yeah, so you see three invalid states\n
I mean, this was just an issue. Sample Normally,\n
do something with the car state. I mean, I'm\n
explain you is exception handling, but I hope\n
thing. And then actually, I would like to,\n
exception handling. And the missing thing\n
because I showed you, we don't actually even\n
can also do is finally, finally, this is very\n
making some something up. Finally, as the\n
end, no matter even exception happened or\n
this is important. When you have resources,\n
in short I O, which you have, if you communicate\n
a file from your hard disk, Yeah, something\n
especially from external systems, who can\n
available, or like the hard disk could fail.\n
nasty stuff that can happen. But there's one\n
you need resources. And so you have to close\n
have a problem with more and more resources\n
explain it in like a second. But all I want\n
important when you deal with IO operations\n
close these resources and close resource for\n
stupid method. You can implement that yourself.\n
classes found on the internet, open source.\n
find something. But this is not the one I\n
think from Apache, and they will have something\n
And then this will do all the nasty stuff\n
important is, and this I want to show you\n
And I will throw the exception again. Which\n
have to catch it. This also works try and\n
So, now with the invalid argument, our program\n
But in any case, let's say I am still running.\n
be printed after exception. Well, it might\n
is a valid one. Because with the string airy,\n
But so that you see, I would whatever happens\n
This will always be printed. So let's see\n
wrong. Not Found. Yeah, because we have removed\n
so let's for now remove that. We at the moment,\n
I'm really doing some nasty hacking at the\n
And then the exception comes. So this is what\n
catch it. So we see the stack trace instead.\n
And this is the place where we can do some\n
But you could do anything in here, basically.\n
exception, runtime exception, because it's\n
inheritance very soon. So that I can go in\n
Yeah, we need the closing curly brace, and\n
I just have I will also show you print stack\n
And now I will say, system out. print on.
So you get an idea. In finally block.
Oh, no, something weird happened. And this\n
The stack trace was printed. And it's red.\n
So it's printed on system error. Okay. And\n
the first one, the second one, and so on.\n
comes after the whole stack trace. Yeah. And\n
off to exception. Because it seems like I\n
So for the valid ones, this line was printed.\n
a hard time knowing why this happened. So\n
the argument. So here I can say, our arguement.\n
after the value of if this is printed, it\n
call this again. And let me also remove the\n
readable for us. And let's run the main method\n
what do we have here? Hello again. So the\n
did not add the argument. In this case, I\n
message of the exception. I'm not sure if\n
Yeah, see, this is the message Hello again.\n
marktspiegel car stayed flying. So now you\n
better. And after this we under finally block\n
this case, we're also ending in the finally\n
Also in the finally block. Here again comes\nin invalid state
but we're also in the finally block. And the\n
So the finally block is always executed all\n
I wanted to tell you about Exception Handling\n
go Shortly through the most important gotchas,\n
which are unchecked exceptions, unchecked\n
to be handled, you can just throw them, and\n
a very famous or infamous exception is the\n
that yourself, Java will throw that for you,\n
the moment now, just now for the exception\n
when it happens, you will not be very happy.\n
exceptions. And they can happen at any line,\n
don't have to be handled in code. If they're\n
stop, like we saw before, because the exception\n
not handled in here, the program will stop.\n
Then Java was the first and actually also\n
checked exceptions. Checked exceptions are\n
that you do something about them, or you add\n
method signature. There are various checked\n
probably the i o exception. That will be C,\n
thrown if you have any problems with i o operations,\n
and I said Java was also the last programming\n
So this really shows us indicates us that\n
you see, all they help us in the end, is they\n
case that to use them for cases that you can\n
them, why not taking care of the problem beforehand,\n
the case, the given argument, and then handling\n
And then maybe last but not least, for every\n
that. I've removed it, unfortunately, yeah,\n
on the string were set in a prior episode.\n
where I use the value of method of enum, which\n
trying to validate it. So if we are validating\n
we can't do anything about it how the method\n
just have to live with that sometimes an exception\n
But what we can do is we can catch the exception\n
so we can convert the exception into a valid\n
have a library, like an old library that will\n
exception. What you can always do is you can\n
our runtime exception. And this is like this.\n
an ioexception shortly. So see, you can also\n
now on that exception while it cannot be called\n
say throw new runtime exception and okay.\n
we wrap it. Because we put this exception\n
which means this is the wrapper, it wraps\n
will be fine. Because we're not throwing an\n
exception. Well, this is a nasty hack. But\n
exceptions all over your code. And yeah, I'm\n
interfaces are probably the most powerful\n
before I continue, first of all, some organizational\n
Part One, I will first of all, talk about\n
interface mean? And tell you a bit about the\n
into Java. I'll make this part as short as\n
we'll jump into my ID, he will do a short\n
use interfaces in Java. And then last but\n
the pros and cons of interfaces from a clean\n
jump into the topic interface. So what does\n
the dictionary page. And the definition is\n
between entities. And interfaces do not only\n
kinds of areas, for example, in chemistry\n
layer or boundary between different substances.\n
interfaces in object oriented languages and\n
you will see, there is not much code needed\n
a boundary. When I say boundary, I think of\n
a big protection from the hordes that were\n
And this is also what an interface is, it\n
more of this later, okay. And then maybe we\n
of view. So in object oriented programming,\n
that other code must implement.
all this holds true for interfaces in Java.\n
a look at the history. For this, I actually\n
I found this page here from Oracle, where\n
the Java language was borrowed from the Objective\n
everyone says are things that Java was actually\n
Well, actually, Oracle itself says that this\n
C. I find this quite interesting. Objective\n
Objective C, then. So I found this page here\n
Objective C programming language in the early\n
was released as far as I know, in 1995. So\n
to write cleaner, more modular and clearly\n
quite good. Now, what is interesting is here,\n
were introduced to Java to enhance Java's\n
they had introduced the interfaces to get\n
code. There is a different reason. Well, I\n
And this for a reason. Because, um, well,\n
now we have 2015 20 years later, actually,\n
code community does not believe so much in\n
has a lot of advantages. But also many disadvantages\n
next episodes when I'm going to talk about\n
was introduced, Java is the successor of c++\n
multiple inheritance. So multiple inheritance\n
And here they write. multiple inheritance\n
as well as compiler writers, because it's\n
inheritance in into a compiler. So it seems,\n
they added interfaces to Java s, like replacement\n
Java did not support multiple inheritance.\n
in different words, I would say, it seems\n
was introduced into Java by kind of accident,\n
about cleaner, more modular, clearly separated\n
something similar to multiple inheritance.\n
my ID. Cuz I felt you were already feet been\n
So it was test that I've used in earlier episodes.\n
method, as the name implies, just for demonstration\n
that I've implemented, also in one of the\n
to it. So the idea is to have a service class.\n
should, like start the engines of several\n
my car service, I have now a method drive.\n
I mean, this is just an example you mentioned,\n
drive method on each of these cars.
all these classes are in the same package.\n
But imagine each class would be in a different\n
separately like BMW and Porsche GE and mercy\n
this car service at the moment is really Deeply\n
that we don't want more about this later.\n
make all the cars drive. And this is something\n
Now what, again, what is an interface? Well,\n
definition, what this type should be able\n
already saying it three different cars. And\n
I have three different drive methods. I mean,\n
be different because of course, a BMW has\n
a different engine than ms cedars. But they\n
they would all have four wheels, and they\n
like a contract off. So in this way, let us\n
here, car dot java. So far, we only have the\n
So let's start. Let's start as if we would\n
And then opening and closing curly brace,\n
And then in here, we could do something. Well,\n
is not going to work, because then all the\n
way. But this is not what we want. For example,\n
drive method. I mean, this is just a stupid\n
system out print line. But here for our example,\n
is implemented in a different way. Here it\n
driving, the BMW actually even has three different\n
a later time. And it also has the drive method,\n
continue with doing the interface, we could\n
do this. Now, just so that you see the code\n
BMW driving Porsche driving Mercedes driving.\n
just using interfaces with less and more readable\n
is fine. If there is a problem, an exception\n
back to what at the moment is still a class.\n
an interface. And this is I say interface.\n
It says interface methods cannot have a body,\n
here in blue. So the body is actually the\n
signature. This on top here is called the\n
of the method is drive. Okay. Sorry. So it\n
Well, I guess even makes sense. Because we\n
a contract. But we don't want to actually\n
by the specific implementations by BMW by\n
done the implementations. So let me remove\n
and you see it. Here it even says it It wants\n
now says, I'm finished. I'm done. This is\n
definition of a method that other classes\n
interface car, we can also say they play the\n
implement more than one interface. A class\n
remember, before I spoke about multiple inheritance,\n
because it also allows to have
multiple implementations in a way, but more\n
multiple interfaces. Okay. And when I click\n
Why is it redundant? because by definition,\n
must be public, they are always public. So\n
the episode I did on the visibility modifiers,\n
is no modifier defined, it must be of default,\n
a public visibility, because it always a method\n
modifier that is implicitly here is abstract,\n
here, that the method is not implemented,\n
and that the body is left away, and that we\n
we can and we should remove it. Always define\n
you should just know that implicitly, they're\n
you can do in an interface. Um, but I warn\n
pattern. But still just to be complete. I\n
static, final. So you can add constants, for\n
And however, you shouldn't do so. And when\n
find out why. Because for example, the BMW\n
Yeah, maybe a Porsche can maybe do 320, but\n
know. So this is actually not part of this\n
this is an implementation detail. And such\n
within the class, or even better. In an email\n
little page I found on the web. And this is\n
it's called sonar cube sonar cube is really\n
favorite tools for static code quality analysis.\n
on sonar cube alone. For now, let me keep\n
want to talk about interfaces. But in this\n
that you don't use constants in an interface.\n
in an interface. And there was also or there\n
the author of Effective Java, which is, by\n
that book on my blog, on Marcus minus bo.com,\n
if you have the time, you can also read all\n
important parts. Here, we can also look at\n
variables directly in the interface. And you\n
final because this is again, every variable\n
don't even have to say public Static final.\n
solution is to use an enum we have talked\n
detail here. Another possibility, which I\n
called constant classes, which you make final.
And then in the class, you have the public\n
but it's always better to have small enums.\n
classes, they would grow. And they would grow\n
While this only contains the constants of\n
about this when I talked about enums is type\n
Okay, so anyway, so now back to our code.\n
All this is redundant, and should be removed.\n
before, because it's an implementation detail,\n
the car should just define what needs to get\n
behavior has, like it should drive, but the\n
or slow. This is part of the class and not\n
when you want to do clean coding. Okay. So\n
now, when I say implements car, when you you\n
so, BMW plays the role of being a car. And\n
interfaces, which we're going to do very soon.\n
implements car. And Marie cetus implements\n
I hadn't got to do anything, or implements\n
implementing the method will normally let\n
here, code example. You won't do it like this,\n
an interface. And then you see I already have\n
I have to implement the abstract method, Dr.\n
is happy. It has to be exactly like defined\n
say int as a return value, because this is\n
now, let me also show you, oh, there's still\n
brace missing, somehow. Okay, it seems like\n
small is is well, actually, by the way, I'm\n
While this is also not true, there are even\n
even contain a single method. This is a specialty,\n
usually they would have at most, at least\n
But again, I want to warn you, I see often\n
And sometimes it's just too much. It's better\n
your classes implement several interfaces\n
your class is going to play. And lo let's\n
prepared again, adding these methods, because\n
is the lockable interface, which I defined.\n
for example, you want to do logging, you want\n
could have cars, persons whatsoever, everything\n
to be likable. Such a class must implement\n
in my learning framework can call. Because\n
luggable, it will then have a message method\n
that should be used for logging purposes.\n
with examples, because what other roles could\n
And such a property would have an owner, for\n
I mean, I want to have a BMW, how cool is\n
came up with the idea, we could also have\n
a value. And so my BMW also has a value. So\n
Think this one I have not implemented yet.\n
car, comma luggable, comma, asset, comma property,\n
I've implemented already the value method\n
I still have to implement. And as I said before,\n
here, we just return something
Okay, now, I have a class implementing 1234\n
classes implementing the interfaces. For this,\n
already do is now my variable my reference\n
class so my BMW can do everything that a BMW\n
owner value, these are methods that we inherit,\n
object class more of this very soon, but these\n
in the BMW class, so I can use all of them.\n
Now, what I can also do is, now that I've\n
I create an object of type in W, but I put\n
will change the behavior. Now, I can just\n
plays the role of a car I can also say luggable\n
because in the context of logging, I don't\n
and for logging, I would need such a message\nokay.
now, let me make all these cars and this is\n
is the object, but the variable when you have\n
with the with an interface to the object.\n
your code like imagine the BMW would come\n
moment we have these three cars, imagine at\n
Marquess car, then when the when the method\n
method will work even with new classes introduced\n
wrote our car service. So whenever possible,\n
to have a for each loop. And for this, let\n
also already talked about array called as\n
I can even do it on the fly. Make it even\n
Hello, on this array, I can now easily iterate.\n
said before, each car will drive differently.\n
our car service does not know any specific\n
decoupling the code is not coupled so much\n
all that it uses, all that it knows is now\n
lines, I iterate over all the cars and I drive\n
a look if it works. Yeah, see, this is exactly\n
Porsche he is driving and marmet cetus is\n
car reference. As you see, we can only execute\n
And we do have a BMW as the first element\n
know. All we know is we have an area of cars.\n
In here. I can also not say BMW, because this\n
And we don't need to be in w because this\n
I think this pretty much wraps up my explanation\n
And now let's have a discussion about the\n
of interfaces in Java. Well, first of all,\n
I can tell you, there are not so many. Well,\n
I have to type down this code. And, of course,\n
much more. So, and each class that wants to\n
So there is some added complexity. Also in\n
And whenever you introduce complexity, this\n
sense, if it helps us with something, for\n
over all the cars. And you mentioned there\n
my for loop would just be the same. So this\n
my car service at the moment does not implement\n
of an exception to the rule. Most services\n
this makes sense, because it's specifically\n
implementation just here. In my example, it\n
explain you interfaces and I want to have\n
And I know I will only have this one class\n
I implement an interface here? So at the moment,\n
implemented interface. Sometimes, of course,\n
personal point of view there is, if I know\n
need the interface. My probably leave it away.\n
you can change it, you can refactor your code.\n
need to interface then edit at a later time.\n
people that like they love interfaces. And\n
call interface readers, like an illness, blindly\n
always just because, and I think this is not\n
be a disadvantage, because they just make\n
already pretty much it. To the advantages,\n
already mentioned, while explaining the interface.\n
that it decouples your code, as I explained\n
the cotton Well, no, I was actually like,\n
the moment, we still say new BMW new Porsche\n
it would still be coupled. But normally, of\n
what he would usually do is you forward an\n
our core service is not coupled to the specific\n
your code much better than without interfaces.\n
things, for example, team cooperation. Imagine\n
and you have a big project that you have to\n
And then you would probably form smaller sub\n
a specific area. For example, one team in\n
while there would be another team implementing\n
car service is then or might be dependent\n
And on the other hand, the team that implements\n
for testing, and for integrating their code\n
is dependent on the other team. And so each\n
be finished. And interfaces can be a solution\n
an interface here, for example, in this case\n
the different cars would just do a very stupid\n
I did here, where you don't have real code,\n
And then the other team can independently\n
implementations where needed. And only when\n
So interfaces really help in team cooperation.\n
include increase your flexibility, because\n
the cars array as a parameter to our drive\n
in our test method, we have this drive method.\n
some condition if it's sunny, just making\n
And so I create a pushy here, so I would say\n
equals to new Porsche else if it's not Sunny,\n
say I take the BMW instead. Because it's better\n
I don't want to make an error, let me change\n
So I just say true here, just to have it simple.\n
be cold. And in my drive method to make this\n
I would call drive. So now at runtime, we\n
really adds a lot of power to our code.
it might be a pushy, and it might be a BMW\n
I guess there are many, many more advantages\n
to add. But I think this is enough for the\n
are. And all the advantages interfaces have.\n
our interface, serious illness. This will\n
much wraps it up. I'm going to talk about\n
a very powerful feature of object oriented\n
allows to handle a group of similar objects\n
it also allows to prevent code duplication,\n
a parent class to its child classes. I'm the\n
are the instance variables, and the instance\n
Okay, well, I guess that sounds very promising.\n
a bit too powerful actually. Well, if used\n
your code. So really take here and use inheritance\n
cases to use inheritance. But whenever in\n
it actually requires some experience to know\n
it. To give you a real world example, I'm\n
about maybe 15,000 lines of code. And there.\n
are alternatives to using inheritance. I'm\n
let's stop here for a moment. Because now\n
in the button right corner talking to you\n
into the code. And I'll show you how to implement\n
request. I'm doing this videos for you. So\n
to you. If so, I would be very happy if you\n
right corner of this video. And remember,\n
Okay, well enough of advertisement Now, let's\n
code. Okay, so here you see I have prepared\n
not really testing something now. This is\n
example. Um, this example is actually taken\n
Sierra, she is really my favorite author of\n
this and any other offer books. In case you\n
to her books on my blog on market minus b.com.\n
find her books. Okay. Now back to the code.\n
we have here an area of animals, similar to\n
where I introduced you to interfaces, and\n
a pushy, now we have an animal array with\na dog, a gorilla
and a line and a tiger. So you should already\n
you use them, here, it looks quite similar.\n
And the idea of polymorphism is that on the\n
variable of this super type a parent class
animal. But Polly Morphe. I mean, this also\n
that you have an interface or a super or parent\nclass.
And on the right side, an animal can be a\n
so different things can be assigned to the\n
like, many sides solve can have different\n
will all be handled uniformly here, I have\n
jump here. Okay, so you see I've prepared\n
we still have to implement, I have only prepared\n
still have to implement the eat method. And\n
again, totally stupid, arbitrary example.\n
to have variation. My idea was, I mean, totally\n
of your code that like because code is always\n
totally depends on the example that you're\n
that I said, Well, every animal, a dog, a\n
different way. Because for example, a gorilla\n
grass, or leaves or something will align.\n
my idea was to have an implementation, a concrete\n
animal subtype. And further, my idea was to\n
be of a different age. But the calculation\n
the age. But I mean, this is just my stupid\n
would be the same for every animal. So my\n
an animal and have this as an abstract method,\n
are different. Sorry, yeah, but interfaces\n
you only have abstract methods. And because\n
keyword is not needed. But here we can enhance\n
already jump now to the animal class. So an\n
class totally empty. So you can use inheritance\n
methods, public void, eat Later on, I'll make\n
public void. Ah, oh, okay. So now maybe we\n
again, avoid system or print boom, just, if\n
This is the exception polluter rules and it's\n
in real code. Okay, so I say, animal is eating.\n
step one, I said every animal is eating in\n
Okay, it's here the dots to just for fun.\n
a regular class, you have seen so many times\n
want the dog to be an animal. Well, if this\n
you remember, but it's not it's a class and\n
is extends really, totally easy. Okay, so\n
using inheritance. Now it's looks like as\n
can already use these two methods in dog Okay,\n
extends animal line extends animal. These\n
to show you and I might just as well do it\n
extending classes using inheritance we can\n
extends or first implements, well, there is\n
and then implements because in Java, we have\n
a single class, but we can implement any number\n
separated list. And therefore, to make it\n
implements and I can say this interface lockable\n
a role. So Aline can play the role of being\n
is running on production at a certain time\n
to print it and therefore we can implement\n
that align can be a luggable Okay, so, you\n
have a lot of methods that we have to implement.\n
order is arbitrary. We can also start with\n
was it print I hope method. And then there\n
This might have Yeah, you see, so the rule\n
as well as when we, when we implement an abstract\n
signature must be exactly the same. We can't\n
must be a string, return something just for\n
So, now, there is still the eat method missing.\n
concrete. So we don't have to implement the\n
So see the red is gone. Everything so far\n
Sue test. Let's check this are the tiger,\n
the tiger was not an animal yet. Maybe we\n
interesting. So see, all those classes are\n
animal. And so these are already animals,\n
an animal with found is a tiger. Okay, so\n
animal, and now it's working, the red is gone.
Okay, but we still haven't done what we want\n
Now, you probably remember from when I explained\n
this method body, the implementation, but\n
the method signature. So let's remove the\n
we didn't have to, because in interface, all\nthe methods are
by definition abstract. But here I mean, how\n
And this one is supposed to be abstract. So\n
set abstract. But now there is a difference.\n
what does this mean? Because actually, this\n
depends on your personal design of the program\n
be instantiated. Now, we cannot say the compiler\n
An abstract class is like just the contract,\n
an interface is just the raw definition. But\n
class, we can only do so from concrete classes.\n
methods. So this is something that you have\n
another warning, I said that this can be used\n
think, well, cool, I just create some tool\n
needed anytime for example, age. And then\n
the inheritance can have several layers. So\n
again, could extend some class. So now we\n
that. But I hope you get the idea. Well, even\n
Because the rule is you have to apply and\n
the book of Kathy Sierra had first Java. They\n
So because it's not a tool, we don't say tool.\n
And so you shouldn't do that as a recommendation.\n
So yeah, it still has to be made abstract\n
have to remove new animal as I said, doesn't\n
let's go into our classes again. See, now\n
abstract, we also forced to implement it.\n
have to implement this method. And again,\n
is eating dot dot dot Okay, So gorilla is\n
grass or something reuleaux is eating leaves.\n
again, and the tiger is eating something.\n
method in their own unique way. So in reality,\n
print on but some specific, Tiger related\n
why we have it here. And all the logic that\n
by the way, I mean, on the one hand, this\n
double sided sword. I mean, this is handy\n
is really complex. Because when you're in\n
implements eat, and which animal just inherits\n
because we could of course, again mix that.\n
example, ah, this one is implemented. But\n
age by itself are not something. So this is\n
the rule. You could say, well, my gorilla\n
H method is replaced by exactly this method.
And the way this is done is, the signature\n
pretty much looking the same. You can't like\n
Well, you see, you can, but it's just not\n
not yellow, but gray. Um, this is called overloading\n
a method that has the same name as another\n
different. So this has nothing to do with\n
and override me override it, it has to have\n
And for this, I have to introduce a new visibility\n
it in an earlier episode, but I didn't really\n
Now what means protected? protected, means\n
default, or package level visibility, and\n
method. In Su. See, this is not read. It also\n
animal. In our case, now, we have all the\n
we had a car sitting, extend animal, I mean,\n
But just technically not from a design perspective.\n
protected. So the visibility is to any class\n
that is in the same package. And this again,\n
to explain? so that you understand, and this\n
you to passively know when someone else uses\n
visibility is less visible than public. And\n
the visibility in a child class. So where\n
I can say public void. Now this is still not\n
I can say public And now I can also say protected,\n
as in parent class animal. But I'm not allowed\n
error, attempting to assign weaker access\n
to make it public, but not less than protected.\n
method defines a contract a protocol. Exactly.\n
you promise any other class in the Java universe?\n
is public. And you can call it. And me. You\n
If there is one class that just does not fulfill\n
it has to have the same visibility. Oh, okay.\n
members of a class. And I said, the members\n
are the instance methods, and the instance\n
I'm in H. And this will also be inherited.\n
I mean, this is the H method. Maybe I should\n
And now I can say this dot age, or just age?\n
45. So this would now we'll set the age to\n
this. Because we know what we mean.
even though this is possible, we inherit the\n
first of all, because I don't like protected\n
kind of violates the capsulation because the\n
its internal variables. Okay. So avoid protected\n
the the instance members visible. If we have\n
Well, I have to implement it. Of course, this\n
animal dot something can also be used now.\n
dot something. Yeah, you see, it's also formed\n
But this will not be inherited. So you cannot,\n
class again, you can say animal dot something
will actually you can even say and this is\n
is not inheritance. It's just Java is a bit\n
It knows gorilla is an animal. So it realizes\n
But this is still not inheritance. Oh, K.\n
you too much with that static.
Just remember, we only inheriting the members\n
instance variables. All Okay. Well, I think\n
it again. And now we see dog is eating. He\n
implementing the H by itself. This was I was\n
in gorilla lion is eating, he has calculated\n
really is a complex beast. And in conclusion,\n
say, when you can apply the is a test. A dog\n
is an animal, a tiger is an animal, a tool\n
extends animal never. Um, if you're just looking\n
to not duplicate code, you can do something\n
tool object. And then always you can use composition.\n
don't know, um, has a leg. If that makes sense\n
And then you if you have a method, for example,\n
have another animal that also needs up it\n
now if you have another animal that should\n
this member. And this by the way can and also\n
it much more flexible actually, even than\n
can exchange the behavior at runtime. This\n
talking about interfaces, where I said you\n
this implementation and otherwise the other\n
code. Just very fast. Else. I just hope you\n
equals to my fast leg, legs, you know.
the other days, I'm not so fast. I say used\n
so of course I mean this if else again introduces\n
there's always pros and cons, you have to\n
use this, just to give you one example to\n
your code. Oh, okay. Let me remove that. Don't\n
Okay, now, let's maybe recap before I finished\n
We talked about polymorphism. I showed you\n
actually design principle it's called favor\n
you you can mix using inheritance with was\n
I showed you can implement several interfaces,\n
say it already. When I do the calmer Something\n
doesn't work. Just believe me for now. Or\n
is implemented. Yeah. So you see class cannot\n
class. And it's even more nasty than Single\n
the deadly diamond of death. But I will not\n
can google this deadly diamond of death. Because\n
I'm going to talk about the method finalize\n
root of the class hierarchy. Every class in\n
of this class. So this is what makes those\n
also the reason why those methods are very\n
questions. So one way or the other, I hope\n
should now that this episode, as well as the\n
are based on the really great book, Effective\n
I mentioned it already earlier. Now I have\n
better would be if you read it twice. It's\n
developer must know my episode is supposed\n
But for more detailed information, it's necessary\n
start talking about the finalize method. The\n
What is a hook method? a hook method is a\n
provided in the class object. And the idea\n
he would provide an implementation in your\n
by overwriting this method. And then this\n
I go in more details, the one the most important\n
is flawed. So the best you can probably do,\n
method. So not using it because it has its\n
mean, there are some few valid users, at least\n
I can tell you, I'm developing in Java since\n
never ever overwritten this method. So today\n
you what it is what it does. Okay. So maybe\n
because it might be easier to understand for\n
So the finalize method is called by the garbage\n
terminate determines that there are no more\n
was probably what is the garbage collector?\n
cool thing of the Java language. Other languages\n
it collects the garbage what's the garbage\n
worked on them. And after some time, the process\n
he will not need them anymore. And so it helps\n
you hopefully never run into out of memory\n
cover as synchronously by the garbage collector\n
the finalize method. Because I said it's,\n
runs. The problem is the garbage collector\n
when or if the garbage collector is running.\n
no influence to tell it that it should run.\n
you have no guarantee that the garbage collector\n
that the finalized method will ever run. Yeah,\n
method, the idea was, you will also clean\n
resources, but not memory real related resources.\n
i o resource, like a database or a file, those\n
are limited. So if you don't want to get in\n
totally, totally freezes. You have to, again,\n
anymore. So the idea was to do that in a finalized\n
guarantee that runs. And this already says\n
Joshua Bloch also gives other reasons. For\n
an object and destroying it, again, is 430\n
implemented. So again, don't use it. And now\n
detail from a practical point of view. Okay.\n
you might have seen, this method is actually\n
void finalize, I mean, this Java API is just\n
see the source code of object dot java. So\n
And you'll also see this method is empty.\n
idea is that you override the method and that\n
And Java, try to prevent that you directly\n
the idea is that you don't call it but that\n
method is actually protected. Protected is\n
use a method that is protected is if you are\n
isn't a package Java Lang. So I thought being\n
this package. And this is what I did here,\n
everything seems fine. I don't have any exception\n
this method. And you see, this dot finalize,\n
because this is a static method. So we would\n
also say, new object. Well, there is an exception,\n
here at the bottom. I'm not going to handle\n
from a theoretical point of view, this is\n
now surrounded with a try catch to get the\n
directly calling finalize, from a class that\n
public static void Main method. So I execute\n
It's prohibited. I cannot use Java Lang as\n
I just thought I should show you. I think\n
this again. And let's complete remove this\n
I mean, I said object is the root class, but\n
objects of the class object. What I don't\n
twice, because the class is called object.\n
you can call all those methods. But here within\n
finalize method. Okay. So for this, I have\n
the object class. And now let's override the\n
any parameter. So to override a method, it\n
Like in the superclass, the only difference\n
anymore, I can make it public. Because public\n
Okay. And now, the idea would be that in here,\n
I had some i, o reader, i, o reader, reader,\n
so that you get the idea. So the idea was\n
close those resources for this, usually I\n
are several problems for one, I said, you\n
ever called. This is totally in control by\n
running out of your control, it's running\n
really serve our purpose. Second flaw is,\n
process here will be stopped. And our objects\n
state. Yeah. Okay, so the question is, what's\n
method, the alternative would be, if you have\n
that you want to be closed before it's cleaned\n
the reader would do. You could and you should\n
you do clean up i o resources. I'm going to\n
in car selector. Let's imagine this was a\n
Porsche, a new Porsche you use a try Finally,\n
And I think, there in this episode, I told\n
but you can also use the finally without a\n
is a different discussion. What is important\n
the finally the cool thing about the Finally,\n
it will always be executed. Because this was\n
non memory resources get cleaned up. Okay,\n
say Porsche e dot close to clean up the resources.\n
Also, shortly, let me show you what I think\n
and the finalize method. What I also want\n
the method from the class object which is\n
that is quite helpful here. And this is the\n
when I put the ad, there is override. So about\n
yet. So let me explain that. In short, this\n
compiler, that you say, Well, my intent here\n
has a different name, some other name. Here,\n
not override any method from its superclass.\n
us. So let's, again, call it finalize. Now\n
the signature is the same as in the superclass.\n
not overriding anymore, but overloading the\n
the same name. But as it has a different parameter,\n
This is not overriding anymore. And now we\n
notice. So this helps us to have the problem\n
in our ID. And we will not because otherwise\n
a method while you have not. And then you're\n
you will only see when your code runs on production.\n
at this override, annotation. Okay. So I think\n
finalize method. In short, again, generally\n
non memory resources use a try Finally, I'm\n
a class object I said before, is the superclass\n
this is why you can use the method clone in\n
you Really? And the short answer is, you should\n
is similar to the finalize method that we\n
again, the reason why I'm talking about clone\n
quite similar. Okay, but before going in more\n
what actually does the clone method do. And\n
copy of the object that you call clone on.\n
object, you can always assign it a second\n
The problem is with references, that when\n
will have will be influenced by your changes.\n
want an independent copy. And for this reason,\n
are actually better alternatives, which I'll\n
Anyway, let's jump into my ad and look at\n
test, you might remember the pushy and the\n
I show you the nasty stuff, I wanted to show\n
clone is actually good. And this is here I\n
here, I mean array is also an object. So it\n
for the array, it was already implemented\n
clone your array. So I can say string copied\n
I can assert not same, which means this check\n
In this case, it would be the same
and copied airy. Which, to show you that their\n
twice the same reference. So this should be\n
there are two different instances.
You see, it finished with a green bar, this\n
and error compared to itself, it's both the\n
the same object. But the copy is not the same\n
a new object. And now, I mean, I want to make\n
system out, print ln. And then, you know,\n
element and then I just hope you believe me,\n
whole elements in in an in a loop. Now I'm\n
and you see it's one. So the other ones are\n
telling me copied error. This way, you also\n
And this is now I can print odd STR and this\n
So I hope this is working now. 123 C, out.\n
was just a very short demonstration of a good\n
and very interesting article Actually, it's\n
he's really a very cool guy. I love his newsletters.\n
that clone for very small areas might be a\n
arrays, it's actually faster than any other\n
don't have the time. And this is also very,\n
beginner level, approaching more advanced.\n
And this is now really test driven style programming.\n
is not finished, we start with writing a test\n
we are working our way to get the test screen.\n
Porsche, actually, this is something you can't\n
adjusted my class a bit. I mean, you might\n
because it was empty. Initially, there was\n
I had to change because I want to show you\n
And I said, it's the owner, the name of the\n
Because I always wanted to own a Porsche.\n
owner of the Porsche. And because you probably\n
make you the owner of our copy of our clone.\n
I might just as well copy mine and then give\n
pretty much impossible. However, here in Java,\n
is I don't know your name. So for this example,\n
Peters for xi equals to new pushy. But this\n
to clone. Yeah, because this is no easy. I\n
pushy but this is not the original idea. So\n
dot clone. And interesting enough, really,\n
covers, because this is done really, by the\n
it works. But there is never a constructor\n
copied from the memory. It's just see, we\n
I mean, I said, we start with a not working\n
and Java Lang object. And this is something\n
method is already there. The problem is, it's\n
Protected would be visible in the same package.\n
Lang, as I showed you last time. But here\n
test does not inherit anything from Porsche.\n
clone in here. Okay, but what I can and what\n
method. But still, I want the code the implementation\n
into the Porsche class. So I will override\n
And then you might still remember from my\n
override here to have a check from the compiler\n
Now, let me format that. Okay. And you see,\n
everything is fine. We'll actually went into\n
says object, because in object, what could\n
the method always is exactly copying whatever\n
a Porsche. So the so it is also fine to directly\n
empty implementation. And we have to return\n
But again, this would not be a clone. So this\n
this is not what we can do. So we have to\n
from superclass in this case object. And you\n
the first method, because my ID is already\n
clone. So it assumes I want to call super\n
it. So all we did so far is we override the\n
I don't really want to override it. All I\n
object. So all we did so far is we made the\n
it this is all we did so far. But now, you\n
Java Lang clone, not supported exception.\n
because, like, why does this Now throw a clone\n
clone. So, anyway, this is how it is. And\n
is we say try, because there is an exception\n
Catch. And there my ID Egan helps me clear\n
I usually give it the name here. Okay, now,\n
now. Because we have to return something.\n
to return something. But this is not so nice.\n
never happening. But if it for whatever reason\n
is a problem. So in this case, and I kind\n
my episode here about clone is based on his\n
was exactly the same with a finalized episode.\n
to the topic. Um, Josh block in his book,\n
clone method. You should really buy the book\n
minus bu.com, click on mastery, it's, and\n
it directly. Yeah, also all the books in the\n
name for a reason. Those are really books.\n
classics, the best of the best best Java books\n
if you trust me by that book and read it,\n
do the same, as he said, throw new assertion\n
even an exception. Is this really an error?\n
this clone, not supported exception. Okay,\n
something wrong. And this is it says I'm calling\n
the method from object, an object returns\n
the method. But we are smarter we know actually,\n
I mean, the super type is object. But the\n
cast, and we cast it to Porsche. This is safe\n
a class not support F. Class cast exception.\n
see. It'll it should work. Okay, well, are\n
as I said, this method is flawed. So we are\n
But this I want to see. I mean, my idea was\n
let's go back to our test, you see, everything\n
has gone. So I can execute the test. But I\n
assert that I want to do is I want to assert\n
of saying not equal. So it won't be the same\n
pushy, because the idea is that when my clone\n
it's good to have a method that already from\n
this one. And I compare not pushy to Peters\n
that this reference here is not referencing\n
this isn't our first test all I want to achieve.\n
You see the assertion error? I said, Well,\n
And this is why tests are so great. Yes. And\n
why did this happen? Okay, so by jumping here,\n
is here in this case, without further knowledge,\n
So here, sorry, I have to help a bit. Thing\n
It's not enough to use the power of inheritance\n
implement an interface here. So we are already\n
you in the episode about interfaces, we can\n
now to implement the cloneable interface from\n
the interface, normally, I would expect that\n
in this case, it's different. We don't have\n
case, this interface we call a market interface.\n
nothing in there. It just tells the JVM. I'm\n
here, it's a clone here it works. And this\n
implement an interface, you would on the Reference\n
I can also make this a cloneable. So that\n
a cloneable. The problem is, this doesn't\n
cloneable. Here, all I have is the other methods\n
now. So because I can also say car, I mean,\n
And then I can call the methods of car, which\n
Okay, well about macros interface. That's\n
haven't talked much about design patterns\n
So a marker interface, generally speaking\n
a bit weird that you have to implement a market\n
the method. Okay, so I make this portion again.\n
working because otherwise you could not compare\n
see, now we have the green bar process finished\n
correct. Peters pushy is not the same instance,\n
the owner at the moment of Peters, Porsche,\n
is why before I prepared this method, because\n
name, because at the moment, we don't have\n
name. So I wrote this method to see who is\n
when we're finished is that the name would\n
partially off, Peter. So let's write a test\n
you also see it expected. So I'm expecting\n
comma. And I put here pushy, Dodd as string,\n
method to return portion of pizza later on.\n
equals. In an later episode, I'll talk about\n
that for now. I'm using the S ring method,\n
method yet. So there's still something missing.
Okay. I can remove that. So let me also execute\n
oh, there's an error. I have missed a semicolon.\n
portion of Peter bar is still mine. Now this\n
two portions. But okay, I said you get one.\n
now change the owner. So I will sell on my\n
Which means I mean, I just made this method\n
owner. Okay. Also, by the way, I mean, here\n
possible for our example. Usually, I would\n
name, for example, and I also already have\n
string. But more object oriented would be\n
so I sell it to Peter. And now I expect the\n
But now I have a fear. What if now my pushy\n
to assert that the The original pushy is still\n
K, which also worked. And this tells us now\n
clone method, it created a new object in memory.\n
Peters Porsche he inserted that they're not\n
then I changed the second object. And I gave\n
now, the owner's name is Peter, by comparing\n
I was working on the second object, the copy\n
like proving that our clone operation did\n
we needed to do for this, I had to say implement\n
I had to call the super clone method, I had\n
This is a lot of lines for only this. And\n
what this does is it creates only a shallow\n
is a more advanced topic. So I try to keep\n
between a shallow copy and a deep copy? Okay,\n
here a more complex object BMW. And here I'm\n
using a second object color. And let's make\n
copy would mean that not only BMW is copied,\n
copied. So that they are also independent,\n
But a shallow copy will here, only copy the\n
as the object for color will still be the\n
is okay. And sometimes it's not, it really\n
short, if you're using clone want to make\n
on the internal objects, and this here, I\n
actually, three times. Now, one more problem\n
here is, first of all, I cloned BMW, and then\n
already fully functional, I assign a new value,\n
it turns out not to in order to make a deep\n
not final, and sometimes you want them to\n
the clone method. And also, I mean, how complicated\n
there are alternatives I told you. I've written\n
so I could show you what to think. Yeah, I\n
because I noticed many of you are only watching\n
be asleep anyway. So let's keep this short.\n
to using the clone method. There are two alternatives.\n
constructor, and this is this thing here.\n
this is the same object. And then you see\n
color on BMW dot color. So I'm taking the\n
And so in the end, I will have a new object\n
This is One alternative. The other alternative\n
one to use a so called static factory method.
And this is this here, it's a static method.\n
dot new instance, you can give it the BMW\n
probably Marquess BMW. And then you You see,\n
here, this is a more complex object. So I\n
Because again, I say new instance. And he\n
those methods to also cop copy the owner's\n
copy, I could have also just assigned the\n
it's safer to have deep copies. Okay, so And\n
name and color. Because there again, I do\n
method. So this is taking a parameter of name,\n
name. Now, first name and last name are both\n
why do I not need to copy down? Okay, now\n
um, thing is a string as an object, but it's\n
means whenever you change the object, like\n
say, Peter, you will not influence the other\n
this, Peter will get a new object. So this\n
done for you. Actually, you can do this for\n
this, you just have the limitation that you\n
allows the client to change the values in\n
change, return a new instance. And then you\n
an immutable test. Because there's another\n
integer. Big integer is similar to an int,\n
First of all, because it can work on ends\n
until your memory is full. And there you have\n
Now, when you say one, add one, this is returning\n
not be influenced by this operation, you can\n
every method that you call will never influence\n
it's created once with the constructor, this\n
you can never ever changed it again, which\n
a good design principle to use immutables\n
very good to use multiples when you do code\n
about multi threading yet, we will at a later\n
they will make everything more easy. So, maybe\n
episode. And this is favor immutables try\n
that they are not mutable, that they cannot\n
here at the same thing with string. Maybe\n
Hello, which in memory is actually the same\n
This will influence the string one, and not\n
So you see, like the speaker so we can see\n
string here printed Hi, the second string\n
first one has high, and the second one has\n
had commented it out. Let's execute it again.\n
the difference. And this is one example of\n
Maybe we could also call this here. So I print\n
to do a few things. So the input value of\n
and the end value of zero is zero. Well, I'm\n
return a new object, return a new object,\n
this is an immutable, this is something that\n
go back to Chrome. Let's recap. So in order\n
all, override the clone method so that it\n
it's recommended. Use not object here, but\n
then you will have to do an explicit cast\n
exception, thrown your assertion error, which\n
cloneable, which you will also have to do.\n
will create a shallow clone for you. Which\n
and you then operate on those objects, this\n
not totally independent, you're only potentially\n
independent. But if here you have name, this\n
here would still be the same. If you also\n
a more complex clone method, you have to also\n
assign the new clone values to your internal\n
not final. So this already limits the way\n
not working now. So and this is one of the\n
why you should not use clone, other than to\n
One is use a static factory method. I call\n
Effective Java that you should buy. And here,\n
I call again, a factory method on name. And\n
creates a deep copy. Or I can do the same\n
is actually shorter. So this is why I would\n
works. Okay, so here, I call twice a new constructor,\n
new color object. Okay, and then in name,\n
And this now is strings and I said strings.\n
string is immutable. So you have to do this\n
object or a primitive. If this were again,\n
with cloning, or copying, like calling the\n
or a primitive value or another immutable\n
And then we can also look at color one last\n
different implementations here with a static\n
And last but not least, here with a copy constructor,\n
the string from the original object, which\n
any further. I'm actually going to talk a\n
on track, talking about the class object,\n
I need to tell you about are hashCode. And\n
important methods in Java, I think. However,\n
to explain. So before I can tackle them, I\n
things up. And to talk a bit about the missing\n
As you see, I have prepared this demo test\n
testing something today. This is really only\n
things. So let's do one by one. And the first\n
You might remember, I think I talked about\n
and I kind of promised you to later on give\n
this time was we hadn't talked about static.\n
have any better opportunity to talk about\n
I should really finally tell you what exactly\n
what it helps you. So actually, we used it\n
Here on top, I have this import static org\n
because at the moment, I'm not using it. This\n
Now you can see it. And you can also see here,\n
is, what's the difference between the two\n
whole test class? Well, this one here, a static\n
Because in j unit, you know, when we want\n
Let's do that now. Like assert. True, you\n
true is always true. There's a stupid, but\n
thing is, this is only not working because\n
great anymore, it turns white, because now\n
at this class, I can jump there, you see even\n
the classes extend object anyway. So here\n
what my ID he can also do to show me the code.\n
what happens behind the cover. So you Here\n
actually like groups them and makes them usable\n
when I talked about static, the way you would\n
say class name, dot assert true. Now you can\n
is org dot j unit dot assert, and then assert\n
you would import the class and then you can\n
there are also other methods, you can have\n
complicated ones like you can even compare\n
equals, with message without is equals, and\n
this means I can also say, assert dot assert\n
because I have not normally imported the assert\n
have to say org, J unit dot assert, and then\n
used anymore, because now I'm not statically\n
the class name to use the method. But I mean,\n
with Java five. The reason to introduce this\n
shorter, because you don't always want to\n
and you could also do it as said before, without\n
than you could go without the import. Now,\n
That's the difference, because now I don't\n
we can go with or without. And if you wanted\n
the static import and not the regular import.\n
means just input all the static members or\n
functions and also the static variables of\n
to import that are static. This also works\n
say you import only specific functions or\n
that's all I can tell you about static imports,\n
it actually happens, then you would think\n
that are the same in different classes. If\n
I told you about the imports, you can only\n
import. If you're not have a second class\n
to use the full fledged, or at least a class\n
in this case, I want the assert true method\n
want the assert true from another class. Okay,\n
the compiler understands what you're trying\n
I mean, then it's just obvious the assert\n
okay. So that much about static imports. The\n
open this up is default values for different\n
And also interesting. Because I see many people\n
And then they would say something like, here,\n
Boolean, they would say false. Or for the\n
have to do that. Because now for object, as\n
So this is in my eyes, this is just clutter,\n
for every rule, there's always an exception.\n
this is set to false. And this is really a\n
with it. It might make sense to sometimes\n
maybe you should name your variable a bit\n
And because my Boolean now I mean, this might\n
always false. Okay, so And now let's have\n
when I say default, this really only applies\n
for example, have a local variable Like Boolean,\n
use it, this is not going to work. If b, this\n
have been initialized, but it does work with\n
see, and now it will count as false. So the\n
but now enough of talking, blah, blah, blah.\n
so let's make this a bit bigger, so we can\n
default value, short default value, those\nare all zero.
Actually, there are many number types that\n
what I'm just going to introduce right now.\n
this is actually the one that is most times\n
others. I think I shortly once showed you\n
but there are also others. In my opinion,\n
see the ranges very soon are only here because\n
in other programming languages before Java,\n
The reason is probably because like something\n
And that was really the memory space was so\n
I don't know, a few kilobytes. Now with gigabytes\n
So the space end users actually a bit more\n
these days, in 99, and probably 99.9% of the\n
in my personal opinion on something like deprecated.\n
I'm sure someone can tell me there is a specific\n
on average, generally speaking, you should\n
the sizes of these types in a second. So yeah,\n
numbers than end. This sometimes might be\n
now float and double, as the name implies,\n
numbers. And double, I think the name comes\n
then that of a float. Actually, also these\n
did not worry too much with these kinds of\n
case I did, for example, calculating on money\n
are quite problematic, actually, because they\n
can happen that when you have something like\n
by a number of people that in the end, you\n
in the financial industry, this is just not\n
use be used there. The reason I can't perfectly\n
these numbers of course stored in a binary\n
and one. And then when, from this format,\n
those floating point numbers, then there are\n
need to have a correct floating point result,\n
to store the number without the floating point,\n
even as a string. And then you do your calculation,\n
point visually at the place in the UI where\n
local settings, like numbers are formatted\n
this is usually the way you would go, at least\n
industry. Okay, so anyway, float and double\n
by the way, is like, when you have a constant\nvalue
like 34 43.31, this here, I mean, this is\n
this down like it is at the moment, this is\n
If I want to have this as a float, I have\n
end, which makes it up float. So, if I have\n
he really have to say f at the end, because\n
it says it found a double, but float is required.\n
bite, there is just the other way around,\n
want to use a short short as equals to 43.\n
the space is fitting. And then it will just\n
really an end to a short. Ever. Let's try\n
also works because now you try to store the\n
should always fit. Okay. So what else, so\n
I said before, Boolean, the default value\n
depends on if you safe if you display this\n
it's called the null character. And I think\n
see there is something it just can't be displayed\n
blue. But here when I go over it, it's displayed\n
it just not visible. And I can convert this\n
And then it's a zero. Okay? Now, so these\n
data types as primitives. Okay, and now, as\n
told you so far, all these number types, they\n
a look, and then see and discuss why that\n
is used already on top. So let's call, let's\njust call it b
equals to bite dot value of now, this is a\n
be used to convert convert, sorry
a byte primitive to a byte object. You can\n
because this might use more space. So, I don't\n
talk forever for performance optimization.\n
while the other one might reuse an existing\n
is good enough. Okay, there's also other methods,\n
this works not on a string as far as I know,\n
See, I can Also see it here. And this converts\n
This is the value of it converts it to abide.\n
of 10, or eight or 16, or whatever. This usually,\n
a lot of lot of functions. What is also interesting\n
abstract type number, abstract class, we talked\n
you can see all these different functions\n
So each of them, you can convert to an int,\n
The green stuff is, by the way, the Java API\n
short. So this, all these types automatically\n
these cool methods, that might be helpful.\n
as objects. The reason is, I think, mainly\n
that we will talk about very soon. I think\n
similar to an array, it's like an array on\n
throw in objects. And that's really it, you\n
not throw in any primitive. And therefore,\n
this object then will wrap around this primitive\n
by the way, in the past, when working with\n
before putting it into the collection, you\n
like the CMI byte, input, the object would\n
just say add, I mean, in reality, it looks\n
an abstract idea, you would have added this\n
to retrieve it again, you could then say,\n
there were people saying all this is just\n
five, there was an auto conversion introduced,\n
an object, and will automatically behind the\n
an object to a primitive, whatever is needed\n
in used by almost every developer, I really\n
ID, that I get displayed these conversions.\n
with performance, because I see functions\n
an object, and to a primitive and so on back\n
Second, for a primitive, you have the default\n
throw a null pointer exception. Ever for an\n
the value will be now and then when you on\n
actually throw a null pointer exception. The\n
be automatically be inserted by the compiler\n
you get a nullpointerexception. So really\n
I personally hardly ever use them.
Okay, but so, so that you know of each of\n
the same type as an object. The names are\n
a short object. For int, it is integer. So\n
float is float and double, it's double. And\n
the way, also has static Boolean object values.\n
the bite. Because this is also B, Boolean\n
now this is an object. I can also show, you\n
though this is an object, the aesthetic value\n
final Boolean false equals to new Boolean\n
is there's only one object that is always\n
to save some space. Okay. And now you see,\n
type, because internally, the compiler will\n
so that's, I think, all I can tell you about\n
variants. Now let's have a look at their ranges.\n
smaller ranges. But enough of talking, I think\n
test. And we see, let me scroll this down.\n
we can see the test and the code in parallel.\n
Remember, don't use it avoided. Just try test,\n
to show you the results. Otherwise, in productive\n
and would probably lead generally speaking\n
here you see. So there are signed types and\n
is a minus, so it goes from minus 128 to plus\n
127. While he is 128. This actually is for\n
in your memory of your computer will actually\n
if it's a minus or a plus. And this is why\n
just decided that minus has one, one more\n
also done it the other way. But this is how\n
is only for very small numbers. 120 minus\n
much bigger. It can store 32,000 something\n
right side in again is much bigger. I think\n
much bigger. I don't even know how long this\n
fine with int can in some rare cases you need\n
a long Okay. And then there's his float floating\n
and the double see here even is much bigger\n
is unsigned. So, this is why there is no minus\n
something is the double size down to the short\n
it needs to have it in a signed form with\n
Okay, and car as you have seen before, usually\n
But if you convert it to an end, you can also\n
to 65,000 something okay. I think this is\n
Okay, so these were the ranges let's go to\n
way to show you see here I converted this\n
you see here I used actually the object, the\n
gives me the min and max value. This is really\n
just use them out of the box and say min value\n
directly, I can just look them up. And Java\n
now. And go to our next test. Because now,\n
also save your numbers, not only on the base\n
base 16. So now it depends. I don't know,\n
I'm assuming you had, they should be really\n
with the base two, you would store you would\n
then with the base 10. If you never heard\n
is not so important. I think I never needed\n
decimal was always fine to me. I mean, this\n
on the left side is always the same, no matter\n
used, because you are in a certain domain\n
for example, the hex. And so you would, for\n
a hex value. And so you could have the same\n
here in your code, which would make it easier\n
has nothing to do with how they're stored.\n
in your memory. And your computer can always\n
no matter what format, you use them internally,\n
by default, they're also when I print them\n
also always be printed out on the base of\n
them. So this is only a displaying issue in\n
binary format, you need specific formatting\n
So I will not go in detail on this. Now. Also,\n
What is also interesting, by the way, I mean,\n
only. So this only recently got added. Speaking\n
to store, base 10, base eight and base 16.\n
stuff more readable. I mean, especially with\n
here of zeros and ones. So that you could\n
credit card numbers, you can add these underscores\n
You're just not allowed to do it at the beginning\n
But I mean, I don't see how this would make\n
it. So this is why you put them here in between,\n
is, is that more readable? In this case? Of\n
especially for something like credit card\n
them more easily, like on the phone talking\n
number nine you can talk about the blogs makes\n
is just the displaying issue in the code.\n
like before. So now let's execute the test.\n
and we can have a look at it. Okay, so here\n
I'm storing the number in binary. The number\n
the zero. You can use the capital and a lowercase\n
On this long thing here, so ob four is binary\n
you can use your calculator and calculate\n
By the way, of course, just shortly, binary\n
three, this will give you an error, integer\n
format, indicated by the B and zero here with\n
with a zero only. So zero and seven means\n
eight in the on the base of eight. As I said,\n
you will understand what I'm talking about.\n
you will hardly ever need it. Let's have a\n
this is the eight. And this is some large\n
in here. This is just to help so that it's\n
hex code is started with zero and x. And then\n
no, I think our numbers are not Latin, their\n
they are based on the base 10. So nine is\n
10 you need one zero, while on the hex, you\n
they used ABCDE, F for 10 to 16. So the F\n
I was lying to you. It's the 15 in this case,\n
it's the 15. Because the 16 again, would be\n
I think it's not so important. It's just interesting\n
I think you really should know, Java inside\n
is possible. The question if you personally\n
project will need it. I don't know, depends,\n
here to be used. Okay. So now, I think really\n
And I think this is really now a good base\n
talk about the two methods equals, and hashCode.\n
of all the methods of the Java Lang object\n
that ties them closely together, which is\n
just one episode. Knowing all the details\n
programmer today. So get yourself some popcorn,\n
the equals method. What does it do? What's\n
is used to compare two objects for equality,\n
for primitive values. But before I'm going\n
my ID and see this in action. Now in my ID,\n
equals method for anyway, because so far,\n
and this was working. Let's see this again,\n
and J equals four. So now I assert, with the\n
we get the green bar. Old tests passed, everything\n
same thing with strings. Here we have a constant\n
Hello. And each has assigned to its own string,\n
these two strings with the equal operator?\n
And I told you a string is an object. So as\n
equals method at all. Well, with a string,\n
a string, there is some optimization going\n
because for this constant value, there's actually\n
to the same object in memory. The variable\n
object is stored in a memory area called heap.\n
differently. As soon as I add a new string\n
is only age. And then I say at runtime, hello,\n
ello to it, which will be Hello, at runtime.\n
that we can see there is really Hello. In\n
assert, hello, one, equal operator Hello,\n
red bar, nasty, nasty. So now they're not\n
Hello. So from our expectation it should have\n
And ensured you should not use equal operator\n
for strings, there is the equals method. Let's\n
This works, it seems like using that method,\n
actually at the method. Here we are clustering.\n
an object. It says things like if this equals\n
the same reference, if it's the same object,\n
Now it checks that the object is actually\n
of operator so that we can safely cast the\n
both strings. Interesting thing about instance,\n
developers know, or at least not all, yet.\n
object is now this will never be a string\n
So anyway, without going into further details,\n
character by character of both strings. And\n
return true. So this is how equals was implemented\n
you, the equals method is a method from the\n
out string is also a subclass of object and\n
here. So then it works on strings as expected.\n
happily use equals, so that we know that a\n
Now, how are things with classes that we implement\n
car with owner Marcos. It is a Porsche and\n
like it has both instances have exactly the\n
be the same car. Now let's first of all, try\n
this is false. It's not working test failed.\n
instances here. Let's try the positive case.\n
again, comparing it to itself. So yes, this\n
to be able to compare Porsche one and Porsche\n
Let's do this now. For sheet one. Oh, let's\n
let's also do this, my portion one equals\nmy portion one
I would expect that to be true. And yes, it\n
itself. Now, let's compare my Porsche one\n
it's not working. We have the nasty red bar.\n
why is it not working with our car? Let's\n
in the class object. Because in our car class,\n
So what happens is, the original method of\n
here, internally, what happens is, we compare\n
my pushy one, we compare it using the equal\n
the method, which was pushy to. And this compares\n
So this is false, because we have two different\n
What by the way, is also possible. To make\n
to the reference variable of my portion one,\n
in memory, no variable is assigned to it.\n
here in this line, and both variables, point\n
Because now actually, we only compare the\n
compares the reference variables, which now\n
But this is not what we want. So let's remove\n
We want to compare this one with that one.\n
This is a small class I wrote. And it just\n
now. And there is no equals method overwritten.\n
equals now, I want to compare a car car. No,\n
is object. So we have to say object here.\n
car, like we saw on the class string.
And then it's always good to add the override\n
overwritten the method. Now, I mean, this\n
false, just to see that when I'm in my car\n
I overridden method. Now, of course, when\n
not work. So we will have to implement it.\n
is not so easy. And we need some more theory\n
first of all, jump back to my slides. Before\n
we need to come up with a proper design first.\n
phase, and simply click on autogenerate. But\n
two sub optimal performance. Let me show you\n
you have to define what it is in your specific\n
or unequal. In your program, are two cars\n
maybe when they have the same manufacturer\n
of wheels, the same top speed, the same vehicle\n
on a knowledge of the business you are working\n
and which fields are redundant, and not helpful\n
performance, you need to define in which order\n
any fields that have a high chance to be unequal?\n
than others? To implement a meaningful equals\n
in great detail. Let's go through this process.\n
top speed relates to the type of engine and\n
field, that will not be helpful for the equals\n
just say the number of wheels is always for\n
would also not help us to differentiate the\n
this again depends on the type of program\n
like the police, this one is probably the\n
about a program used by the manufacturer itself.\n
sold yet, I would assume the witness of less\n
that in detail. This is what you will have\n
company or your department. Based on their\n
to compare in which order. In my artificial\n
the business analyst. So for my example, I\n
different wind numbers can be seen as equal.\n
my equals comparison later on. Now, before\n
some more theory to cover. In the introduction,\n
of equals and hashCode we have to fulfill.\n
equals implementation has to comply with,\n
it actually is. Let's go through each of them\n
to itself, so that when I call my old car\n
this makes sense and should be easy to understand.\n
not they are equal. If my old car equals some\n
car must also return true This sounds obvious\n
a simple condition when it comes to inheritance.\n
is extending the car class, it could happen\n
car is not equal to the BMW is every BMW is\n
see, the symmetry rule can actually lead to\n
interesting articles online discussing potential\n
each contract condition with a dedicated unit\n
with a contract. Our next condition is transitivity.\n
a second is equal to a third, then the first\n
sounds much more complicated than it actually\n
object b is equal to object C, they should\n
equal to a consistency. If two objects are\n
unless one of them is changed. So when you\n
a equals method, it should always return the\n
method should not alter the object in any\n
what Josh Bloch calls nonlocality. When knowledge\n
they should always return false and never\n
know quite a bit about the equals method.\n
for? For processing similar objects are usually\n
Java is like a more powerful array, or an\n
Besides other things, it allows you to look\n
but also based on their specific values. This\n
To speed up this lookup process, the Java\n
that will use the hash value as a grouping\n
needed. In the optimal case, each object that\n
will also return a different hash code. This\n
so called buckets. Now, in this optimal case,\n
by a lookup based on its hash value. However,\n
where two unequal objects will share the same\n
same bucket. Now, if I'm looking for that\n
based on the hash code minus 391. That car\n
I will on top have to do an equals comparison\n
but inadmissible bad performing hash code\n
for all objects. In this case, however, the\n
In the end, you would have to do an equals\n
as if the hashing logic had never existed.\n
idea of the hashCode method, I have to further\n
method has to fulfill. The first condition\n
is for any two objects return same hash codes\n
use the same idea Define attributes for both\n
much. Now, if you don't fully understand what\n
you to the hashCode contract, I will jump\n
When hashCode is invoked more than once on\n
the same int value as long as the object is\n
consistency rule, I introduced to before both\n
results. To fulfill this contract, you should\n
and vice versa. Also, when you add or remove\n
probably also have to adjust your equals and\n
to return different hash codes when equals\n
rule, but it will improve the performance\n
hash collisions. to the extreme, the hash\n
42 for all objects, as Josh block states in\n
result in quadratic rather than linear execution\n
between working and not working. Actually,\n
beast, I couldn't practically fit in all the\n
asleep. Therefore, I have done a follow up\n
on the hashCode method, which you will need\n
programming. Below this video, there's a link,\n
for the hashCode method. For now, that's all\n
your own hashCode and equals methods. to round\n
to show you some practical examples of hashCode\n
can see I've implemented both equals, and\n
engine, and color, also here and hashCode,\n
them based on the decisions I made during\n
the theory session. So the car clause has\n
Manufacturer, engine, color, and number of\n
that number of wheels and Vehicle Identification\n
to compare a car. Of course, this is totally\n
it. I really can't stress that enough. In\n
the business people and find out what is important\n
the order that I've chosen. I just thought\n
millions, or however many colors that for\n
I think most cars are either black or white,\n
or yellow. But that's probably it. So I thought\n
and also more different engines. And I want\n
which are not equal, I want to leave the method\n
I leave the method, the faster the entire\n
then usually it has to go till the very end.\n
But it does make a difference if it's not\n
for perform performance optimization I have\nchosen this order
So this is what I would also ask you to do,\n
And if the performance is important, then\n
for sure until you test for the performance.\n
hashCode and equals. And now, we should also\n
mostly is also a performance optimization,\n
I compare the object to itself, I want to\n
variable references the same object in memory.\n
I don't have to compare all these things.\n
a specific case, where you know that you will\n
might even leave this if a way. Yeah, this\n
because such a check doesn't also cost a lot,\n
is actually very important here. This is the\n
an object that we compare is now that we always\n
otherwise object dot get class would throw\n
happen, because now we are already, we have\n
Okay. And then the third, if check compares\n
object that is coming in to all equals method\n
bikes and cars, for example, or cars and apples\n
this explicit cast to car would also throw\n
made sure that they both have the car class,\n
And now I think this is quite interesting.\n
equals other dot manufacturer. This field\n
car, even though that this is another instance,\n
access this private field. So I don't have\n
field. This works and makes the code I think,\n
not equal, then I can directly leave the method\n
fields. And if I'm here, that I'm sure that\n
compare number of wheels as well as we equal\n
about them, I don't care because number of\n
be four. So would be the same anyway. And\n
be two different cars. But just like, if I'm\n
example, or the look, then I wouldn't care\n
cars. I would just say well, this is a BMW\n
Lou. So for me, this is the same car. Period.\n
Now the hashCode method. This is actually\n
that I've decided to do a follow up tutorial,\n
to watch. There is a link below this video,\n
only in short about the hashCode method. The\n
he is multiplied with always the result. So\n
field, which will return some in number
will edit, starting with one, and then multiply\n
reason is this is an optimization to have\n
the collisions, that this can decrease the\n
really doing something here, I'm just calling\n
is why now we should jump in the other classes,\n
but I've chosen engine here in this case,\n
in one example. So let's jump to engine. Okay,\n
or at least similar. Then in car, the only\n
I called optional field, because I defined\n
So therefore, my check here for equals has\n
to save it's now then I check if the other\n
field is now the other is not so it's not\n
both fields. So I'm doing all this again to\n
other case, when I don't check for now, my\n
field, so it can't be null anyway, but for\n
a manufacturer, engine and a color. So my\n
now, if someone would introduce a bug. And\n
course, this could produce a nullpointerexception.\n
This is what I call rocket code. Because it's\n
your code, you have to have certain assumptions,\n
fields, these fields can never be now. And\n
will crash, okay, we have to fix it. But then\n
stays clean. And this is important, because\n
Okay, so only four fields where you know,\n
Okay. So that much for the equals method,\n
in car. Now for the hashCode method. Here,\n
Because here, I cannot forward to a hash code\n
long. And for that, because the primitive\n
some hash code. And this is a long, and so\n
A long has that has like 64 bits, while an\n
make the long to half its size, and you want\n
least possible number of collisions. And therefore,\n
is actually based on again, Josh blocks book\n
actually, he doesn't explain it too much.\n
watch my follow up tutorial about hash code\n
This is a shift by 32 bits. And then this\n
combine this field. And last but not least,\n
where you throw away Half of the long, but\n
one minute, you have to watch really the extended\n
good way to create an end hash code from alarm.\n
And the second field here is a string as it\n
And I'm not sure if I have shown you this\n
because otherwise the code would even be longer.\n
now. If true, then return zero, else return\n
calls the hashCode method of string. This\n
where the code was like in a while loop, going\n
calculating an int value for the string. So\n
call the hashCode method. Okay. So, again,\n
And in this case, I decided to use both to\n
And so as I use type an optional field and\n
and hashCode in the same order and same fields.\n
is this doesn't mean anything, this is really\n
short, show you all the different primitive\n
that you see like how the different cases\n
So let's first of all look at hash code. So\n
one here, float, well float has the same size\n
function, which will convert the float to\n
A has the same length as a long. So this is\n
again, with a native function too long. A\n
implemented in Java, but in machine dependent\n
details, watch my follow up tutorial. But\n
weird line here to convert it to an end. Next\n
here. If it's true, question mark, then 1231.\n
All this stuff is really technical might be\n
just really important for performance optimization\n
car is just taken as it is because it can\n
we call this hashCode method. And for the\n
an end or smaller than an end, that's relatively\n
it is because a byte will always fit into\n
And an end of course, should also fit into\n
really short. I'm just not enough time to\n
I have done the follow up tutorial you can\n
understand. Now last but not least, let's\n
the primitive values here, it's pretty easy\n
can just use the not equal operator.
So if it's if the bytes are not equal, just\n
involved, false is returned. And also see\n
the smallest possible values, because here\n
all this crazy stuff down here. And this is\n
end, long, this is all easy. With float, it's\n
first to an end, that will has to be converted\n
actually, if this was a real whoops, if this\n
it differently, I would have put it to the\n
you the different types. So in reality, I\n
based on what the business tells you what\n
you have to think, like, from the performance,\n
it depends if you really need the performance.\n
because you might think something is fast.\n
as fast as he would have thought, okay, because\n
thumb, but I might be totally wrong in a specific\n
need a certain performance, I test for it.\n
it. Or I just use my gut feeling to sort the\n
so I hope you liked this episode about hashCode.\n
and so long. I just wanted to put in everything\n
topic that he should really understand in\n
So I would also ask you go to my blog, www\n
some links to some extended articles on the\n
Effective Java from Drush blog, he also talks\n
I will give you a high level introduction\n
there are several overloaded uses of the word\n
up front. The different use cases are a collection\n
or group of things. Second, the Java collections\n
and classes. Third, a collection as a data\n
can hold a group of objects, like an array,\n
interface, one of the two main interfaces\n
Java util collections, or utility class that\n
collections. This episode is based on chapter\n
packed with knowledge. As a great fan of the\n
you to read this book, even when you don't\n
is the Java collections framework? from a\n
more like a library a toolbox of generic interfaces\n
collection interfaces and classes that serve\n
to arrays, collection related utility interfaces\n
I'm going to describe both parts in detail\n
interface and class hierarchy for collections.\n
grow or shrink in size. Except before a collection\n
pairs of objects that have some kind of relation,\n
does not have a specific position in this\n
is related to relax if you don't get it now.\n
So here you see the hierarchy of classes and\n
collections interface. Just try to remember\n
an overview so far. As you can see, the collection\n
and implementing classes. A collection can\n
is extended by the interface of set list and\n
objects. What is considered as unique is defined\n
set holds. So in other words, a set cannot\n
a sequence of objects. So unlike a set, a\n
a list keeps its elements in the order, they\n
sides, entries are added to the end and removed\n
as first in first out, which is pretty much\n
first person queueing up will also be the\n
have a closer look at the interfaces and classes\n
Hash set linked hash set and three set are\n
is the default implementation that is used\n
is like a mix of a hash set and the list as\n
set. But it returns its elements in the order\n
would do. Chrisette will constantly keep all\n
mind, there is no free lunch. So every edit\n
After looking at the classes implementing\n
at the two extending interfaces we haven't\n
set is a set that is constantly sorted. The\n
Java six, it allows to navigate through the\n
to retrieve the next element greater or smaller\n
have a closer look at the classes that implement\n
implementation of the list interface. Like\n
elements and it does allow to iterate the\n
on arrays, it is very fast to iterate and\n
an element at a random position is very slow,\n
array structure. vector is a class that exists\n
framework, which was added with Java two.\n
So So please never use it. Use ArrayList or\n
it and forget about it. The next list implementation\n
is based on a linked list, which makes it\n
in the list. Last but not least, let's have\n
interface. We already talked about link list,\n
However, the fact that it's based on a double\n
also implement the queue interface. linked\n
priority queue is a queue implementation that\n
It has similar functionality like a tree set,\n
look at the map interface. This interface\n
A collection operates on one entity, while\n
for example, a vehicle identification number,\n
for example, a car object with the help of\n
to. The interface map is the root of a lot\n
at now. The class hash table was the first\n
on a data structure hash table. So the Java\n
this makes it a bit hard to differentiate\n
deprecated, because of its sub optimal performance,\n
Instead, use one of the other classes that\n
default implementation that you should use\n
not make any guarantees on how it internally\n
rule is linked hash map, which allows to iterate\n
not least, tree map is a constantly sorted\n
extend the map interface. As the name implies,\n
and defines the contract of a constantly sorted\n
map interface, and adds methods to navigate\n
all entries smaller or bigger than a given\n
similarities between the map and the set hierarchy.\n
are actually internally backed by a map implementation.\n
the Java collection classes often contain\n
name. To choose the best collection for a\n
characteristics of data structures like array,\n
short, there is no single best option. Each\n
I promise to talk about this very exciting\n
overview of the collection and map classes\n
In a later episode, I will introduce you to\n
framework. generics is a topic at least as\n
the context of this episode, I will therefore\n
to understand the collections framework. It's\n
my brief overview. Give both of us some time.\n
the other I would recommend you to subscribe\n
as I upload the next episodes of the Java\n
belts. Here, it's going to be a bumpy ride,\n
brackets. Just relax. Actually, it's much\n
line, you see I have defined a list variable\n
brackets, I tell the compiler that my main\n
Then I create an object of type ArrayList.\n
is only supposed to be used with strings,\n
tries to put anything else than a string into\n
the container type safe. Also, note that I\n
not ArrayList. This makes your code more flexible.\n
at various places in your code, he will use\n
for the reference variable. You could later\n
you had to adjust was this one line of code.\n
you could also use the collection interface\n
for the reference variable. Oh, by the way,\n
use as a constructor argument. This I have\n
before, the collection classes can dynamically\n
and all hash table based collections are internally\n
grows in size, it will internally on the fly,\n
from the old to the new array. This of course\n
so fast, that is usually should not be a problem.\n
the exact, or at least the approximate size\n
better than trusting on the collections default\n
the data structures that the Java collections\n
understand performance considerations like\n
small little details, makes the difference\n
So I wanted to let you know as early as possible.\n
Let's get back to the topic of generics. Now\n
is instantiated accordingly. As said before,\n
key to one value element. Both these elements\n
example, I use Vin, the vehicle identification\n
value. This has to be added as a comma separated\n
will check that this holds true. If you create\n
both in one line, you can also leave the second\n
inferred from the generic type of the reference\n
and is called the diamond operator. Because\n
a diamond. Actually, I'm sorry. That's not\n
what I just showed you. This was actually\n
class when you lock in the contract parameter\n
method, the interface or the class was defined\n
you see a generically defined interface. In\n
an interface operating on two separate generic\n
time. However, when these types are locked\n
type, the types, interface methods we'll use.\n
in one of the next slides, just remember it\n
a generic way. Okay, now we are ready to look\n
Java collections framework. They are implemented\n
the JDK in general, but they can also be implemented\n
of the collections framework. Well, strictly\n
is not part of the framework. But more precisely,\n
of Java util collection. So every class that\n
implement the Java Lang iterable interface.\n
in detail. an iterator is an object that acts\n
collection. Let's look at its methods. Boolean\n
more elements. ie next returns the next element\n
the last element returned by this iterator\n
provides only one method, which will return\n
this interface can be used on the for each\n
your homemade collection. In order to plug\n
you will have to execute two simple steps.\n
over your collection with methods like hasnext.\n
you need to implement the iterable interface\n
an instance of this iterator implementing\n
a sort order for your entities. The interface\n
which is int compared to if you want to define\n
make it implement this interface. return a\n
a given method argument, zero if the object\n
a positive integer if the object is greater\n
smaller or greater is for you to define. For\n
is smaller than five, for example. But for\n
sort your entities. When you put objects of\n
into a tree set or tree map, it will use your\n
elements you put into the collection. As you\n
been greatly designed for extension. It offers\n
classes. This interface is very similar to\n
define additional sorting orders like a reverse\n
implemented in your entity, but in an external\n
be added to a collection or sorting method\n
your collection of entities. The rules of\n
same as for comparable. return a negative\n
the second argument, zero if both arguments\n
argument is greater than the second. Last\n
classes, collections and arrays, like a Swiss\n
that greatly enhance the general usefulness\n
offers methods like sword shuffle, reverse\n
on areas and not on collections actually,\n
us to swap areas or to search through areas\n
look at ArrayList. And the second half, I'll\n
get started. Error list is the default implementation\n
of list, you can have duplicate elements in\n
to element in the same order as the elements\n
provides fast access, but inserting or removing\n
time, as this will require to reorganize the\n
most applications, which is why ArrayList\n
data that changes frequently. However, consider\n
linked list. I will talk about this as well\n
of my free Java video course. Okay, before\n
terms, which are important to understand in\n
Size is the number of elements the ArrayList\n
to the list, the size will grow by one capacity\n
underlying array can hold. The capacity of\n
list starts with an initial capacity. Every\n
the ArrayList copies the data over to a new\n
one. Let's say you want to add 100 elements\n
10. As the list grows, the system will create\nsix more areas
to take the place of the first. First one\n
a maximum of 22 elements. Then areas with\n
to hold the growing list. These restructuring\n
you can instantly create an array of the correct\n
by defining the correct capacity at creation\n
of the ArrayList at creation time, estimated\n
capacity however, can also negatively impact\n
I advise you to always explicitly set the\n
intentions. For most projects, you won't have\n
powerful hardware. But this is no excuse for\n
you can see a simplified extract of the class\n
a bit more complicated. This is just meant\n
class ArrayList looks like. As you can see,\n
written. Given enough time and knowledge,\n
actual source code online. However, don't\n
the source code as they may change any time\n
specification. Default capacity is the initial\n
it as I recommended before element data is\n
ArrayList in size is the number of elements\n
remove are some of the many functions ArrayList\n
So, let me give you a short overview of the\n
easy for you, I have broken up the overview\n
interface, and methods belonging to the Java\n
containers in later episodes, implementing\n
refer back to this episode. Instead of repeating\n
are subscribed to my YouTube channel. So you\n
these classes also. Okay, so let's start with\n
interface. The contract of the collection\n
order, and therefore does not provide any\n
can see the first set of methods that implement\n
these methods does not only apply to ArrayList,\n
collection interface. The method Boolean ad\n
to the next empty cell of the underlying array.\n
to the end of the collection. The stuff in\n
short, it ensures that no one can call such\n
relax. I will tell you more in my upcoming\n
the first occurrence of the element you specify\n
the given elements from the collection. The\n
use an a loop to move from one element to\n
step you say I iterate over the collection.\n
number of elements of the collection. Boolean\n
at least one instance of the element you specify.
Wide clear removes all elements from the collection.\n
collection contains no elements. And to array\n
Alright, let's move on to the methods of the\n
similar in part to the methods we just looked\n
order on the elements of the list. So here\n
about these methods does not only apply to\n
the list interface. This add method with an\n
insert method. It allows you to insert an\n
Instead of just adding the element to the\n
of the underlying array will be shifted to\n
necessary. The remove index method allows\n
of the Similar to the Add method we just looked\n
elements of the underlying array to the left.\n
from any given position of the list. The index\n
index of the first occurrence of the element\n
not found. In last index of returns the index\n
list, and SP four minus one if the element\n
of the list starting with the position you\n
before the one you specify as to index. Last\n
list following the order of the given comparator.\n
time to move on to some practical coding.\n
prepared a little test method. So we can do\n
already created an instance of ArrayList.\n
of type collection. Now, this might be a bit\n
that not ArrayList? Or at least list. While\n
C. Martin, he says, a good architecture is\n
not made, or the number of decisions D fared.\n
I mean, you would create this instance for\n
forward it to either other methods and or\n
ArrayList, this will really limit all your\n
to work on an ArrayList. But now, the requirements\n
they change very fast as you know. And if\n
can hold duplicate elements. But now there\n
you cannot have duplicate elements, you have\n
So you might want to use hash set here. Now,\n
the code, it will be very tough to change\n
code, which is very dangerous. But if this\n
many things. And so it will be much easier\n
have to do is change the instantiation of\n
if you can, like of course, this limits the\n
the collection interface is less powerful\n
But if you can, like if you only need the\n
then do it like this. later on. We will switch\n
show you methods that only appear in the list\n
variable of type collection. And then you'll\n
you see in the single line? Besides you might\n
brackets, so we have created a collection\n
so called diamond operator that was introduced\n
in one line, we do the object instantiation.\n
line. The compiler directly sees this must\n
the diamond operator. But we could also do\n
is just a bit shorter. Okay, and last but\n
initial capacity, which is a constant, which\n
initial capacity five. Now you might say well,\n
this is faster. Well, at first, it might seem\n
this is evil, because it makes it so much\n
this five actually means. Like not everyone\n
But if you use the constant value here, you\n
is actually quite good. And I recommend you\n
start using some methods. I mean, we're not\n
but we're just playing around with the methods,\n
is all about. And as this is now, a collection\n
see all the methods of the collection interface.\n
like one limitation that we have now is we\n
also the reason why before in my slides, I\n
related to the collection interface, and methods\n
now I can also go back to the slides and compare,\n
collection interface. So this is a collection\n
let's add B, capital B. Let's add some more.
later on, I'm also planning to sort the error\n
with the collection reference variable, we\n
I'll do it soon. Oh, okay. And I mean, even\n
collection, still, the fact that we have an\n
elements. So let's do that. Maybe I should\n
count how many numbers elements I added 123456,\n
means when you add the sixth element, internally,\n
list will realize, well, the capacity is insufficient,\n
and will then migrate all the existing data\n
the second he is added, which costs some extra\n
regular situation like this five, six elements,\n
Low. So never, I'm never worry, in such situations,\n
about this when it gets a problem. And usually,\n
1000s or 10s of 1000s, if not a million of\n
of elements, you have the amount of iterations\n
print the error list as it is now, there are\n
string method. So I could directly say system,\n
bit more complicated, because I want to show\n
And this is because every list, and every\n
which is used in this new for each loop. And\n
string string, and then column and then elements.\n
list will be done here inside and you don't\n
us internally. In short, I can tell you, there\n
Before, which will then be used. You might\n
you to the iterable. And the iterator interface,\n
enough, if you see, you can use the ArrayList\n
out each element separately, which of course\n
print something else here also. Okay. So this\n
element by element. And I also want to show\n
this out. I also want to show you that which\n
the elements, AB AC E, will be also exactly\n
out, which would be different if we had a\n
execute it and see it in action.
Okay, so here you see a B, A, C, E, exactly\n
list. Okay, now that we added elements, we\n
also just as easy we move. And now on the\n
But I can remove by stating the object that\n
uses the equals method. If you don't know\n
about the equals method. So it goes over each\n
is implemented in the string class. If there's\n
and will then remove it. You might also notice\n
know, like will the first a be removed will\n
And there is another tip I have for you. Like\n
a book for video tutorial, you can look in\n
never be as Sure. And probably also never\n
like this. And you just try it out for yourself.\n
the code doesn't lie. If you see it, he will\n
So let's do this and check which element is\n
first a is missing? So the answer is the first\n
Okay, so what else can we do? Let's check.\n
a bit simpler now. To have it as short as\n
copy that copying some code that you will\n
or K. And as long as you're just copying the\n
fine. So this supposed to give us the number\n
we just see the number of elements. Let's\n
the object. So I expect here to see a zero,\n
be another alternative possibility that this\n
So let's check that execute the test was actually\n
but here. So you see it return zero and the\n
method. You can even ask, Are you empty? Which\n
probably false. And there is true and false.\n
interface. And let's also play a bit around\n
variable of type list. And I can use all the\n
extends the collection interface. So it includes\n
has. But on top of that, we now have more\nmethods
than just the methods of the collection interface.\n
an index position. So I could for example,\n
know T and S at index position two. And then\n
Let's remove this because it'll format the\n
you what I mean. Okay, we still have the true\n
it's fine. So we have T, because we added\n
will be inserted where a was before. Before\n
zero. So this, put it to where a was before\n
And this is why now this is nice to see it\n
is all shifted to the right, which when I\n
if I remove the s, everything will be shifted\n
and we added the S at index two. So now this\n
rest to the right. Okay, so let's also remove\n
remove index. And now let's see, I remove\n
Okay. And there, it seems I am wrong because\n
Let us see. Well, of course, because it's\n
position zero, a is now index position one,\n
is always not so easy. Really, I have to tell\n
use them. Normally, you're fine, actually\n
interface, which again tells you like, often\n
so this actually inserts an element. Okay.\n
for demonstration purposes. I mean, this is\n
in my slides, you should really think carefully\n
here, it's different because we're playing\n
again in the example between the capacity\n
is the number of elements. Let's do this here,\n
a capacity of 100 the area will be 100 elements\n
however, will be much smaller. And here you\n
is a difference. Okay. Now we can also ask\n
do this here. Index off and there is also\n
we should also use. So index off. Let's use\n
that. to not make it too confusing to keep\n
the first index of an element. So I would\n
And they receive zero. And we can also say\n
Check that. And you might ask, Well, what\n
to more flexible, like combine the methods\n
say like, like, remove this index or so on,\n
I can also show you the clear method. And\n
method we already saw. But now let's use rim
clear. This method is void, so I cannot cannot\n
empty the list. And then we'll ask
is it empty now. And we can also print out\n
because this would be another question. Can\n
throw an exception maybe? Let's test it. And\n
it. It's just the square brackets and there's\n
Okay. So I think that's already it. A really\n
I really recommend you to do the same fire\n
with the methods of earliest. I know there\n
just that in this video i don't have all the\n
motivation to play around with it by yourself.\n
any questions, leave them as a comment below\n
a thumbs up before you go. Thanks for watching,\n
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.