Welcome back for the next little project.
I want to show you a few tricks.
Okay.
So there are two things I want to show you in this video.
Number one is Thread.sleep and the other one is how to make a line break.
Okay.
So if you want to have a line break inside of a string you need to use the backslash n.
So let me show you how that works.
So string my string and I'm going to say hi.
And then backslash n and hi again okay something like this.
Now let me print out my string here inside of my for loop.
So I'm going to use c w and then print my string.
Okay.
Let me scroll down a little bit so you can see it better.
So this backslash n is like pressing the enter key and going into the next line.
So it's this line break here that you see.
So I have this line break.
And then I have an empty space.
And then I say hi again.
So this is what's happening here.
And it happens multiple times.
That's because you see I have an empty space before the line break.
And then I have another empty space after the line break.
So I can get rid of that if I want.
Hi and hi to be just on top of each other, as you can see here.
So now it says hi and hi.
And that's what this backslash does.
So it's an escape character.
So in strings the backslash is an escape character.
This means that whatever comes after this backslash is going to be disregarded as text.
But it's going to have a special function like in this case the special function of this n is going
to be a line break.
So n stands for new line.
And that makes it a little more sense right.
So here backslash N stands for new line okay.
Now this does work very well on Unix.
But if you are running this on other operating systems it could very well not run.
So in that case what you can do is you can use backslash r backslash n.
So this will then work on Mac on Windows and on Linux basically okay.
So if I were to use backslash R backslash n I'm going to pretty much get the same result.
Let's run this to see.
And you see I get the same result.
So this is called a carriage return.
So this backslash r is called carriage return okay.
So the return key that you have like the enter key is the return key on your keyboard.
And that's what this backslash R does.
All right.
So I also said that I would show you how to use the Thread.sleep.
So there's this method that we find under thread and it's called sleep.
And then inside of the brackets you can enter for how many milliseconds you want your program to freeze.
So this is not something you should use whenever you build a production application, because this will
literally sleep your application from doing nothing.
It will not do anything within this time frame of, in this case, 1000 milliseconds.
So we're basically saying, hey, software crash for that time, so to speak.
Don't crash.
But like don't do anything, don't run any code whatsoever.
So this is just something you can play around with for your console applications, but don't use it
in final products or real world applications, because there's this story of a guy building a software
for a company, and he had a bunch of those sleeps in there, and this made the calculation quite slow.
Then he offered them, yeah, I will optimize the software for you for value x.
So then what he did is he just changed the number here, let's say from 1000 milliseconds to 800 milliseconds.
And the software got faster and they were like, yeah, amazing.
Now we have such a better software, so much better, whatever.
So then then he says, yeah, I can optimize more, but I will need more time and we'll need more money.
So the only thing that he did then is he changed it again a tiny bit and it got again faster and they
were happy and so forth.
But at one point somebody else had a look at the software, at the code, and they found out that this
is what he was doing and he got into serious trouble.
So I'm not sure how exactly the story went, but it went something like that.
So yeah, just so you're aware, don't play around with Thread.sleep too much, but let's run our application
with Thread.sleep again and we will see.
It says counter is ten.
Hi.
Hi counter is nine.
Hi, hi.
So now after each iteration, it pauses for a second.
And then it executes the next iteration.
So now we actually have a countdown timer.
So let's get rid of this.
Hi hi.
And let's just run the timer like this.
Counters ten counters 9876, five and so forth.
Okay so for the next video I have a really cool little project prepared that's very simple, but that's
all we can do for now.
So see you in the next video.