Transcripts
1. Introduction: Hi guys and welcome. My name is Michael.
I'm a photographer, cinematographer,
and a physicist. And in this course,
I'll be exploring with you guys some tips and
tricks that you can definitely use to up your coding or your
programming. Level. Rise will be primarily
dealing with Java. And this course is intended for intermediate groups of people. That means that you have
some basic understanding of what Java is. But we're still developing
some fundamentals with it. Just did an intermediate stage. So there's a bit of some background that
you need to have. And I hope to see you
in the next lesson.
2. Conditional Statements: Alright guys, I'm glad
to have you here. And just a reminder that
this course assumes that you already have some basics
of coding in Java, right? And what we're gonna do is
we'll just start off right here with conditional
statements, right here, the up right
here, conditional statement. And remember that's a
comment right there. And we'll particularly start
off with if else statement. If dadadadada means we put a
condition right over there, and then we'll say
else over there. And then we also put a condition of what should happen, right? And inherently, Java
is well able to support quite a number of some logical conditions
from mathematics, right? Whether it's logical
conditions like maybe something like less than, let's say less than. And remember that we, we show, mathematically
speaking, we show less done by something
of the following scenario. We say a is less than
b, for instance, right? And then we could
have another scenario where we have less than or equal to, right? And that one would obviously have to be in
the following fashion, where we would have to say maybe a is less than or equals to b. Then we could also have
a scenario where we talk about greater than, greater than, which would then be referred to as a
being greater than b. And you can definitely be
able to track on top what I'm going to say an x greater
than or equal to, which would then be referred
to or shown rather in the following
manner where we say the same pattern is greater
than or equals to b. Then we could also
have a scenario where you talk about it being equals to something and how we
refer to that as we say, a is equals to fact, let me just do this. Well, I say a is equals
to a is equals to b, not, and I repeat not. A is equals to b. Encoding
a is equals to b means that you are saying a
is to be replaced by b. It means equal,
literally means insert. You see, but when you
say equals, equals, it means that you
actually referring to the mathematical notation
or equal to, right? Which is what you
would want to put perhaps in the scenario
of conditions. Then you could otherwise
show that something is not equals to something
by saying not equal to. And then let's have that
and say a is equals to b. So definitely there are quite a number of conditions
that you can definitely use. And based on what
you want to you, you can come out with different performances or
actions to achieve, alright? And another thing that you might just want to be aware
of is that Java itself has the following
conditional statements. So here they are
conditional statements. From Java itself. We have if condition. We have the if
condition, condition. And this is to do what? This is to specify a
code to be executed. It's as simple as that really. If a specified
condition is true, if condition is true, right? So essentially would be able to have this if condition, right? And let me allow me to just
put it in these two brackets here just so that
it stands out as an as a condition itself. You're not able to
confuse it as if it's just one
continuous sentence. No. We use if to be able to do that. I could even put that use here. We use f to do this and then
we could also use else, else to specify a code
to be executed as well. Alright, let me just do this. But if a condition is
false, you see that? So here this is perfect. If condition is true, no else means that if
the condition is false. So that's how you use it. And then we could also
talk about else if, which is also a condition. Therefore, I should
put a bracket. Else if we use else if. And with the condition
being condition. And that's when we want to specify a condition
to be tested. To be tested. That's if condition is false, right? So essentially what
usually happens is that would probably have something
like this condition first, which is the if condition. And then next would
have else-if. And then finally we would
have this one else. That's if all conditions
are happening, then we go to finally the else. That's usually the order in
which would have one worry. We'll definitely be looking into this just shortly after this. This is just a presentation
of this methods. And then we also have a
switch condition which is used to specify there is a
condition over there, right? Typo, which is
definitely used to specify what many, many, many alternative blocks of code to be executed. This comes in very handy, especially into a
machine learning or data science or
anything of that. You will find yourself using
this a lot of times really, just to delve in deeper into the whole idea of
the if statement. Let me give you a bit of some some, something
interesting, Garrett. Let's have a look
at the following. Let's just say we're looking
at if now if condition. And with this if condition, what we're gonna
do is write this.
3. If Conditional Statements: What we're gonna do is
we're just going to put a condition over
here and say if. And then we put our
condition if for example three is greater than one. That's an example. Question is, now, if that's
the case, what should happen? What should happen
if that's the case? So you could tell it mixed
for example, system. Remember to put it
properly system. Dot out, dot, print, LN, print. Let's print a string that says first number greater than
second number. Right? So essentially what I'm
telling my, my, my, my Java here to do is
if the first number, which is three, is greater
than the second number, if that condition is true, then this should be printed out. This is what I should see. And if it's false, the question is if it's false,
then what should happen? Well, nothing because
I didn't tell it what should
happen if nothing, if this condition is false. So let's just try and
run that and see with java will be able to print out what we expect it to print out. There is first number greater
than the second number. Now, don't be moved
by this HelloWorld. That's because of
this right here. I could just easily very
simply delete this. We don't really need it. Honestly. I could run it again
just so that we are convinced that we
just have that as first number greater than the
second number. Really good. So consider the
following scenario. I, I put my one here
and I put my 3M. Now, let's predict
what should happen. Inherently, we know that one
is not greater than three. And therefore it means this
first condition is not true. And remember that if
condition is used to specify a code that is true, if that condition is true, then code will be executed. But then in our
scenario over here, this condition is
not true, right? So let's see nothing. Why nothing? That's because the condition is false.
Definitely false. But then Java is wondering, now if it's false,
what should I do next? Since we didn't specify what it should do next, just
summarized nothing. What we should do is
we should say else. We could say else if,
or we could say else. But I'll start off with else. It means if the first
condition is not true, if it's false, therefore,
what should happen? That's where else comes in. And then I say system, out, dot, dot, out, dot, print LN. And we have first number, smaller than second number. So let's see now let's run it. Essentially, we're
saying if this is false, then this should be upheld. Now, if this condition
is not true, then obviously it
means it's false. And therefore, if it's false, this here should
be tested, right? It means if we don't
do the first one, else are therefore we're
going to do the following. So let's run it and
see what we get. Walla, first number,
smaller than second number. That's definitely true
according to what we see here, the first number is smaller
than the second number. And we could play around with
as many values if we want. We could even play
around with 100. And let's see 103,
which one is greater? It's definitely hundreds, so it means the
first number should be greater than
the second number. Let's see. Voila, there it is. First number greater than
the second number, right? So that's definitely the if
statement over there, right.
4. Errors Associated With Conditional Statements: Then just take, take note though that there are some errors
that people will do. Sometimes take mode
that if you use uppercase, if like that, that means something
else different way, then we'll give you an error. It's very important
to note that, right? So you can think of it, or rather even Teslas that
we've just done here. Not only for numbers really, you could even tested
for variables. What if you add variables
like, for example, let's say I were to say int x
is equals to 25, like that. And I say int y is equals to 79. Then unlike right,
semicolon over there. And then I have
put if condition. Now I just open the
bracket and I just say x is less than y, for example. Alright? If that's the case, then what should happen?
This should happen. System. Then I say
dot out dot print LN. And therefore we just
say x is greater. Let's say x is less than, x is less than y. For example, if that's, if that's true, if that's
true, then that's the case. Now, what do we
predict to happen? Well, are inherently, we can
see that x is definitely smaller than y because
x is 25 and y is 79. And therefore what we expect
to see is we see over there, it's, this should be
held true. This is true. If this condition is true,
then this is what's going to be printed and it is true. And therefore what we expect
to see is x is less than y. That's what we expect
to see. Now, what we wanna do here, remember, is want to put this in
comments just to ensure that we don't get answers from the previous
thing showing as well. We just want this
portion and viola data's X is less than y, right? Awesome. I hope that it's making
sense for you guys. It's written the most
important thing.
5. Else Statement: And right then we'll, we'll just quickly move
on and see if we can just toggle on the L statement. Alright, We're
just understanding the else statement even further. The focus has been primarily
on the F. Now we'll focus on primarily on
the else statement. As I already mentioned,
the else statement is just used to specify a block of code to be executed if the
condition is false, right? So you can think of
it like, let's say, for example, were to say end
and then we talk about time. That is equals to maybe
40 minutes, maybe, right? And we then say, if time is less than 60
minutes, an hour, right? If, if the time is less than 60 minutes, what should happen? Well, if that's the
case, then I'm early. The student is LE maybe if this was a
case where a student getting arriving
late for an exam, whether it's 40 minutes early, or rather 40 minutes
late or 60 minutes. 60 minutes beyond time. Obviously the student
is lifted away, but it's just a matter of which one is better between the two. So we'd probably print
something like, hey, if the time is less
than 60 minutes, then the student
is not that late. Know if this is advisable, but hopefully the student
will not be late at all. Student is not that late. That's if it's less
than 60 minutes. And let's see. Remember, that's because
the first one here is appearing because we
didn't put comments in it. So let me just put comments
there and then we can focus on the second one without
any distractions. So there it is hidden and the student is not that
laid here as we can see it. That's because again,
the variable here, what Java does is
that it understands. Remember we spoke about
how the equals sign and essentially just means
insert whatever I see time, I have inserted 40 into time whenever time is
mentioned to Java, java quickly understands
that This isn't really time. This isn't really the
time we're talking about is 40 if the integer, all the whole numbers for t. And therefore, according to a condition that we have here, the student is not dead laid. And then we tweak it. We tweak it. How do we tweak it? We say else now, we say else Now. Else. It means that if
this condition is not upheld, student is late. Friend, Ellen, student is late. This would apply in the
following scenario. Imagine I were to say, which one is Alia being printed minutes late
of 40 minutes late? Which one relatively as Ellie, obviously 20 minutes, right? Therefore, the student is now the lead if he is
40 minutes late. For example, in our
scenario here it means in therefore is for t less than 20, no any, That's what
we're saying here. The condition is if for
t is less than 2040, represented by the time,
the variable of time. If that's the case, then this is what's
gonna be printed. But that's not true. What is true now is that
for t is greater than 20, and therefore that falls in the else statement
because here it's false, and therefore whatever is false here will be executed down here. So let's run it and see we predict student is late, voila. There it is. There we have it. Student is late. Beautiful. I hope it's just making even
more sense for you guys.
6. Else..if Conditional Statement: Go quickly into the else. Let me just put
this as a common. The else, if that is the
ELF, the else-if condition. And with the else-if condition, really what's actually
happening here is that what's actually
happening here is that we have the
following scenario right? There. We have it, for example, we're going to apply exactly
what we just did here. Just take it with
us down here to just understand much better. So let me just put this one in comments because we
don't need it anymore. I could always just
literally just console this and
yeah, it's okay. So we haven't
following scenario. Now, think about it. What if, if The time
is less than ten, then this is what will print LE or maybe Good morning.
Let's change that a bit. Just to understand. Better. Afternoon, for example. Or other here it's evening. Evening. It means that whatever
time I have here, it's a fairly binary situation. If it's less than ten, the time is less than
ten, it's morning time. If it's greater than ten, even in dire automatically. Right? But then the else
if comes and says, Hey, what if I want to say else? If? Let's say else-if. And then we put our
condition and we cells if time is less than, what if it's just
less than 30 minutes? Well, in this case, what will happen is you just
let me just copy this line here so that we don't have to keep writing it every
time when we see it. Then here it would
just be the afternoon. Afternoon. So literally what's
actually happening here is that we're saying if the time is less than
ten, it's morning. If it's less than 30
in the afternoon. And if it's greater than what? Than T, meaning it's not less than ten and it's
not less than 30, then it definitely means it's
greater than Theta, right? And you probably are wondering
why would we need the LCF? Well, you see when you
make it just if then else, It's a very arbitrary or rather
a very binary situation, its limits you so much
because it's really an option, a, I'll be. But then the beauty
of else-if is that it allows you to add
more conditions. Therefore, you can
add options a, B, C, D, E as many as you want. You can add as many
else ifs if you want. Now, if I have a time that's let's say I had a
time that was what? Literally five. Maybe. If I have five, it means it's less
than ten, right? The first condition
is satisfied. It means what in Java runs
that you might say to me, but then five is also
less than 30, E13. Print this message. Well, when Java runs it, he's starting other stuff literally from
here, checks here. Is this true or not? And then if it's not
true, he moves on. But then if it is true, then Java will just execute whatever code that
is in there that is definitely right here, right? But then if it's greater than
that, it will come here. So in this scenario, we predicted to be in
the first scenario. Therefore, we expect
it to tell us about the money and datas morning. That's because it ran
this first one is, notice that hey, five is less
than ten, therefore money. But then what if I add plus ten? Make it 15 now? Now it's greater than ten. It means it will check
first-year and verify and say, Hey, this is false,
this is not true. And once it does that,
what does it do? It skips and goes here and
verifies hens is here. What about this area? Else if time is less than 30, is 15 less than 30. And it says, Yeah, it is. And therefore it's going
to print afternoon. So let's run it and see. There it is afternoon. And definitely true, able to see the trend if I
were to increase this by printing here and
just make it better five now, it means you will check
here first and say, Is that a five less than turn? It will say possible
that's false. And then move on and check is five less than 13
not true as well. And then you also
now just say false. Then it will finally come over
to the else here and say, if this one is not satisfied and this
one is not satisfied, then it definitely is going
to be solidified here. So therefore, this here will
be true and we predict, therefore, for our
code here to be run, our string to be shown here, our text to be shown
here to be what? Evening. So let's
run it and see. And voila, there it is. So I hope this has helped you guys and I'm going to
move on to another thing now.
7. The Ternary Operator: Right? So the next one that
we'll be looking into is still
unconditional statement, but I just want to show you more shot way of dealing with the whole if else
conditional statements. And this here brings
us to what we call, I'm going to make use of
the ternary operator. And worry not. We're gonna
be explaining this shortly, but it's called the Java. Let me just put it
davon shorthand. If else. Alright, so that's the
Java shot and evil. So it's more like a shorthand which is a ternary
operator essentially. And you're probably
wondering what the operator hears itself. Here's how we spell it. It's a ternary operator. And a ternary operator really is just a ternary
operator because it consists of three operands. Operands are, you will
see them shortly as I, as I'll be texting them. So think about the
following scenario, which is what we had. Think about it like this. Let's say you had something
like end and time, like we did in the first
scenario and we went to give this a value
of 30 for example. And then say if the time
is less than Twain t, then what should happen is, what should happen is we
should print it out, right? It should print out what? Maybe something like good day. Or if that's not the
case, what should happen? So we write else
and then we say, this is literally
what we just did. We could print out the print and now we could print out
maybe a good evening. Yeah, this one's well, we already spoke about this and nothing's particular
bothered now at least because we've
already done it before. And I definitely gonna
be a good evening because 30 is greater than 20. That's a long way of
writing this way. Using ternary operators, or really this shorthand rule
that we're about to use. You can simply replace all of that instead of
writing it that way, what you could do is you could just literally
write it like this and just say end time
is equivalent tool. Let me just put them in operator here and say end
time is equals to three, as we said in the previous case. And then we change
that bed shear strain. And then we say result. And then we say, it's equivalent
to time, alright, which is less than t. Then we literally from there just put a question
mark over there. Just like that. And we
put the question mark. We even have this here
where we say, good day. Just like that, even put a dot. And we don't need a dog
necessarily really, it's just the mean
was putting a dot. And then we could also have something like
Good evening here. But just in as a string, because that's what
we define this as. It's a string. Evening. Alright? Then we just close
it up like that. As you can see,
there is no error. So it means Darwin's life. I understand what you're saying. And then I just put an
odd Print and result. So instead of writing
it this long way, as you can see, this
takes how many lines? 12345 here, three lines. Probably wondering why would we, what's the point if it does the same thing with the point? Well, the more you really delve deeply
into the whole coding, you'll see that
sometimes is you would come up with thousands
of lines you see. So it's just easier
sometimes if you can find abbreviated forms of those
lines or codes, right? So let's see again,
good evening. You see that? So
that right there is an application of
the shorthand rule or the ternary operator, right?
8. Switch Conditional Statements: That brings us then
to the next point. Let me just put some comments. Our next point has
got to do with the conditional
statement of switch. Switch conditional statement. So for such conditional
statements, we use them to select one of many blocks of code
to be expressed. How it works is like this. How it works essentially
is like this. Here is an error because this
guy should be down here. Yeah. Yeah, good. Assault house, which
essentially works. It's like this, right? The switch code, let
me just put this in. We're going to make
some commentary here. The search code. Rather the switch expression
is evaluated, right? What's the value of the expression is compared
with values of each case. Alright, so comparison for values of each case
is done, right? Then we also have
if there's a match, for example, if there's a match, we'll have the third stage, which is if there is a match, the code will be executed. And there are some words,
keywords like break. The break, we use keywords
like this one here, the break and default. This are some keywords that
we use, this optional, right? And this has some
keywords to note. And I'll elaborate them moist. We keep on going, right? And the key thing to note
here is really, as we'll see, particularly now
with an example, is this really, this
really is the main vein. So an example, let's
say we talk about, hey, they default, for example, or rather the five and
preferably five over four. And then we talk about a
switch and then we say, day, this is how we
do it by the way. And we'll just open up our statement of there are
conditional statement, we run it through
case number one. And in case number one, what we do is we look
at case number one. We can go down and
then we say, Hey, this is the first case, then print Monday, for
example, the days of the week. And if this isn't the case, if, if if what, our
condition is not one, therefore, you will
not print Monday. We are going to tell Java
to go out of this loop or this conditional on
case here and come to case number two and check a two. So it's taking with
this values here, it's trying to match these values here to what we
initially have over there. So it's going to come here and allow me to
just copy and use this so that I can do this much faster and say
it's gonna be Tuesday, the second day of the week. And in case number, I mean, definitely we're
going to put a break. So if you don't put a break, it will hit the devil will not properly understand as to how to properly do or what
to do in this scenario. You see? So it's just always wise to use it in the following manner. All right, so, so we have
dot over here out of print. We have wildness day and
that we have a break. And we have kaizen before
given by Thursday. With break. Yeah. So as you can see
this definitely way more lines over
here That's mucus with specifying a whole
lot of conditions. Lloyd, case number five, I think I'll probably stop
at Kids number seven. So just bear with me here. Friday. It's Friday over there. And just coffee
break here as well and case let me just copy
all of this should be much, much faster like this. Case number six and
case number seven. And here's Saturday
and Sunday. Sunday. Right. So what we expect this, since we're checking
out the day over there, what's going to happen
is that Java is going to run through, check, take the number five and
scan it through with all these conditions here,
it's condition 1234. And when it gets
a match of five, as you can see, it's getting
a Metro five already. There's five here
and there's a five, whether it's already
highlighted. It's going to print Friday. So let's run it Friday. Alright, so beautiful. That's generally how
we just work our way around the whole Java code. Whole Java code when it comes
to conditional statements, that is, conditional statements.
9. Java Loops: Alright, so the next thing
that we are going to look into is what we call Java loops. This is a Java loops, especially we'll start
off with the while loops, means that quite a
number of loops. But we're going to focus
on the while loop. And loops themselves. Molecule like how we define
the loop in English, it's something that's almost, you could say repetitive rate. They can execute a
block of code as long as the specified
condition is reached. They're very handy
because they save time, reduce errors in the
main code more readable. And the Java code, it's same texts can be seen
in the following manner. So let me just put
the comment over here just so that
we can have this included a comment here. See, good. Alright. Syntax for this
would be given by, let me just have it
right down here for us, the syntax will be given
by while condition. As you know, I love putting conditions in
brackets like this. Just sort of be stemmed out and you know how to
better use them. And obviously we're going
to open up something over there and then we have
a code to be executed here. And then we're going to close it down with this guy over here. So it means therefore
this is probably all that you might need and
just close it over there. Alright. So an example of this would
be something like that. So let me just say, for example, int I is equivalent one. And I say, I give a
condition while I is less than five
or five, why not? This is what should happen. System. Dot out, dot, print, print I. Alright. So what's going on over here? What's going on so far is we
are telling our code that while I is less than five, keep on doing the following. Keep on showing the value of I. And don't just stop there. Increase. And this is me
putting the increase. I say e plus plus. I say a closet like this. Keep on going up. Make an increment of
plus one every time. So Java will take one, check it against
five and see that, hey, it's less than five,
then what you will do, you take the value of
11 and print it as one, and then come down and add one to itself again and
then go back to say, now I have one plus one. Now it's now two. And this is how to demonstrate
it will be one, then one. Then you will have one plus one. And then you will have
one plus one plus one. And then you will have one
plus one plus one plus one. And then finally he
will stop right there. Therefore, the values that
you essentially get will be one plus two plus
three plus four. You see that? So let's just run that for
us and see what we get. So there it is, 1234. Those are the
values that we get. And just remember that the reason why we
stopped at four and we don't go beyond or like on a five or
anything like that. It's simply because it's simply because we
said less than five. If we had said less
or equals to five, that would have been
different because it means that five is included. But in this case,
five is not included. So now that it is true that
lets us see the difference. You'll see it gets to five. Let's see, datas
from one to five. Now, you see that. So it's just a way that
we we work around this. Just a bit of some
caution over here, just please remember this. Let me just put it for you. Do not forget to increase variable used
in the condition. Do not assume, right,
not assume that. Just this alone is enough to cause the eye to
keep on going up. No, it's not. It really is not.
And it won't even know when to stop is either
your risk on making an end, never ending loop really let me just remove this and
just shrink something. Yeah. If I run this this way, Let's see what's happening here. It keeps on going 111111. Why does it do that? It keeps on doing
this because it will just keep on an
infinite level of it. That's why my computer
is literally even struggling to see that
it keeps on going down. I can even scroll down or up. I honestly cannot. The only thing I have to do
in this stage is I can now just do this again. And I'll stop it
right here as well. Stopped and run it again. The reason why that
was occurring, It's because it's
taking the value of one and then repeating to check again against
the will of one. And every time it keeps taking the Villa of one and
taking that button five, but one is less than five. So what should I do? I should print I, which is one. And it gives me that
again and again. So it's an infinite loop. It will keep on doing
that. It just won't stop. So you need to have this some of these conditions
below it to give it, when should it stop? How should it know
that it should stop? Wait. So when you give
it this increment, you are saying keep going up, keep going up until
you reach the spine, the statement here,
and this is true. That's when you stop. You see that It's just very
valuable and very important to to understand
that as well, right? And just coupled with the
while condition is what we call the default condition. And just the four condition. Let me just move those, put them here. Beautiful. So the four condition is quiet. One of those conditions
that are used a lot and how they function
really, it's like this. Let me just put it down for you. We call it the Java for loop. And we use this one when
you know exactly how many times you want to go
through a block of code. Instead of a while loop. While loop, if you don't know, when would it stop, right? So it's like you're
putting a restriction. But for, you definitely
know what's the beginning, you know what the end. And the syntax that we use for
this one is the following. Really syntax is the following. We say for my just scroll
down, we see four. We put statement one, statement two. And then we put
statement three as well. Bear in mind that
in-between here, we're putting
semi-colons instead of just commas like that. We put semi-colons. And then as you know, we always just open up here and you want
to close as well. And then here we just have
a code to be executed. Executed. You're probably
wondering at this stage what is called statement1. What does statement two, what does, what does
all of that all about? Well, what this is all about really is
this statement one, what it does is that it's a statement that
really is executed one time before the execution of the block code gets executed. One time before the execution of the code. Then statement two. On the other hand, it defines what
defines the condition. Condition, defines the condition
for executing the code. And then finally the
statement three. Statement three is all about, really is just it's the
last stage you could say executed after the cold means after the code has been
executed, of course, right? So an example of this would probably be
something like this. Let's see if we can just
find it over here and say, for x is equals to 0. Then we say statement two. We say I is less than
or rather not I, x is less than seven. And then finally
we're like, Hey. I'm the x keeps on increasing
in the following manner, meaning it has an increment. And we just open up
parentheses over there. And we print, we just print x. So just in prediction of what's going to
happen here is we're essentially defining
the value of x starting from, from, from 0. We define x is 0. Then we give it a condition to say while x is less than 0. And then keep on
doing the following. What should keep on happening? Simply keep on adding one
because it's x plus plus, which means an increment of one. So couponing going by, up
by one again, again, again. So what Java will do is he
would take the first value, check against seven and see
that we have just 0, right? Then from there, he
will just from 0. There'll be an increment
of 0 plus one. Then again, keep on
doing that again, 0 plus one plus one. And then again 0
plus one plus one, plus one plus one, and so on and so
forth until it goes all the way tool to six because it's less than seven, it
should not reach seven. So it's from 0 all
the way to six. So it means when we run this, we should just see values
doing this all the way to six. Beautiful, There it is. That's exactly what
we expected it to do, and that is exactly what
it has done, right? So that's just amazing,
That's just amazing. Example that you can
definitely think about or just find a value would
be this right here. Let me just comment
this and but, but, but, uh, right. Just comment on this and let's consider the
following scenario. What if you had, for example, for end, I definitely itis time. Love French from me. Quite nice. I say it's less than or
equals to maybe ten. And then I give it
to the final act of simply going up by two
instead of blast one. You see that? So now I'm going up by two
and then I say system out, print LN and the managers
say, Hey, print i. And what's going to happen here is you can just
probably predict, is we're going to
have the following. We're going to have 0, and then we're going to have
an increment of plus two. So it will be 0 plus two. Alright? I hope you're already
seeing where this is going. We're going to be 0. And then plus two,
plus two, alright? The increment is true,
it's not one, right? So it means our increment. Each step is going
to be just by two. So we now have it at six. It's still not bigger than ten. So it's going to finally
just stop right here too, as two plus two plus two. And find that this
is eight, right? And what it will
actually do is this, this is how it will actually do. Now, I'm just telling
you with hindsight already haven't done
this many times. It will check and say 0, and then check and sets now to record the value of
true, print it out. It'll print a value of 0,
print the value of two. Here it will print the
value of two plus two, which is four here it will print the value of two plus two
plus two, which is six. Here it will print the
value of two plus two plus two plus two,
which is eight. And then when it gets here, it says this is ten. It will run it through
scan and say, wait, ten is ten less than
or equals to ten. And then the answer is yes. Actually it is, because
it's equals two. You see that? So it should actually be able to even take this value as well. I had actually not seen that
I put an equal sign here, but because there's
an equal sign, yes, it will
definitely register. This is ten, so it
will show 0246810. So let's run it and see for
ourselves, there is 0248610. And just bear in mind that
if I remove the equal sign, it's going to stop at eight
is gonna be 0 up to eight. Var law, that is datas. It's just amazing. Beautiful. The next one that would
definitely want to look into is called the Java each loop. Java each loop, Alright? And for this one, this is how we have
it as Java each loop, that's how we write each, each from English
really. For each group. That is for each, for each, not just each, for each should be for
each all the time. Alright, so for this one here, the syntax for this is
really the following nature. We say four and then we put
the type of a variable. And we put the
variable name here. And right over there
we put an array name. And we open it up like that. And then we simply
have our code. Here. This is code to be
executed as always. Code to be executed
is right over there. And yeah, just to please bear in mind that you need
to close this, right? So remember, you'd probably want to have something
like this over here. Alright, very important. And just, just like that, Let's try and see
an example of this. Let's say we had a string containing names
of costs, right? And let me just do this because my keyboard does not
inherently have this, so I have to always change my key votes in
order to do that. If you're on a computer or on a Mac that does not have the, the place of many buttons, you can always just change
keyboards over here. Remember that? Then? We can always come
over to the cars here. And just we have,
for example, Volvo. We could have, for example, BMW. Yeah, you mentioned it
could be VW, volkswagen. It could be for Americans would
definitely love this one. It could definitely be
maybe Honda, Japan. So you could also
have that as well. And then right in here, what do we, what do we do? Well, we don't really
open it up at this stage. It's when we start
using the four that we start opening
things up for now, this is just a list of ints, an array containing
a list of data. So it's string, string I. Then we say cos. Yeah. Then we open it. Now. Now it's definitely
the time to open. And then we say system out dot print, for
example, right? So let's just, it means
therefore in this scenario, let's just run it and we
see there the beautiful. So it means that for each of the things that
we see in a list, continuous lists like that, we were able to
present each of them. So it means that Java was able to go
through each of them. How java interprets
this is like this. It's almost as if this is
represented by a value of 0. And then the value
one, value two here. But if three value four, and by simply doing this, we're telling you
that it's a string. Print the value one, and then it will understand. So one is actually a
string and it represents, it represents a string of Volvo. So it will print Volvo, represented by the value of 0, and then come to one
and c1 is BMW and then come to two and c2 as vw, and then C come to
three and then S3, S4, and then come to four. And C4 is a Honda, right? Ben mine that I started with
0 and that's how in Java always count starts
from 0 and goes up. That doesn't stop here and see it as a one necessarily
it sees it as a 0. So that's just something
else to keep in mind and see its value as well as we're still on this very
interesting topic. Interesting topic.
10. Java Break & Continue, Arrays: Alright guys, so the next
thing that we're definitely going to want to address will have to do
with what we call Java break and continue. So Java break and continue, it's still part of what
we're still on right now. Definitely still part of that. And what we're
gonna do with this, and you will see as well. For this, we have already
seen the break statement used in an alien situation of what we've been
talking about. And we used it obviously to jump out of a switch
statement, right? We can use the break statement to jump out of a loop as well. Jump out of loop. And obviously that's
going to be a comment. Alright? And the best way to
demonstrate this would be, let's say you have something
of the following nature. You were to say fall.
And then you'd say end. I is equivalent to, let's say is equivalent to 0. And as you remember, whenever you have four, you need to have three statements. So let me just define a few
more statements for myself here and see what I can
come up with. Alright? Alright, and so forth. And then we say, if E is
equivalent to four, right? Four like that,
what should happen? Then there should be a
jump out of that loop. So there we have it. And then what do I do next? I just say system dot out, dot print LN, and I
have my eye over there. Alright, so let's
just run it over here and see what
we get from that. And voila, there we have it. We have the values
from 0 up to three. And the reason why that is, is because we have simply
told our code here, our software, java to
do this, start from 0. And as long as this condition is satisfied off having
a number that is less than ten with
increments of one at each time. What should happen? The
following should happen if I is equivalent to for
the moment that I get up to the value
of four, then stop. You see what Java does. It starts from 0, does
this and notices that, hey, we're 0 is
definitely less than ten. And then it goes and
prints the value of any checks here as well. Definitely it will check here and check and say that, Hey, I is 00 is not equal to four. Therefore, there's no need
to come to this break. It'll skip and come here
and print the value of 0, and then go up by one and
then take the value of one, check it against tag and see
if it's larger or less than, and see that it's less than. Here again, check
it against four and check if one is equals to four. And it will find out obviously
it is not equal to four. So what it will do,
it will jump again, print the value of one,
so on and so forth, until it gets to the venue of, of, until it gets to
the value of four. When it gets to
the value of four, following is still
satisfied, right? But then when it gets hateful, see that four is equals
to four and then neutral. So therefore, I will break. It will stop right here and one come to print
this value here. You see that? So there'll be no need to print this
value right here. But if I were to
say, for example, if I were to remove or if I
were to have this print here and just say if let
me print this here. If I were to print this
and set it at four here, it should print the value of I. It would definitely register the value of four that
was there, right? But yeah, it definitely
makes sense. I hope now. Then we're
going to continue and then talk about there it is. There's our comment over there. So the next thing we
want to talk about is definitely the Java continue. They go hand in hand. It's a break and continue. So the continuous
statement breaks one iteration in the loop. If a specified condition
occurs and continues with the next
iteration in the loop. So it's almost as if we're
skipping that one area that perhaps we were looking to get. For example, in the very
problem that we have over here, the various situation
that we have over here. Let's just copy that and
paste it right here. Instead of me putting
a break here, what if I had put
maybe continue? What what's the effect? Was the effect of definitely having a continue over there. And there's a bit of a problem, as you can see with
my code over here. It's okay now we run that. And now not 0123,
there's no false. We skip up to 56789.
You see that? That's because we have said that when four is
equals to four, contiguous, meaning skip it, there's no need to print it out. Just skip it and go
back to this as well. So that's why it skips the
value of four over there. So essentially That's
how continue works. Awesome people. Then the next thing that we
want to look at is probably one of the most essential
ones when it comes to coding in that has got
everything to do with what we call the arrays, java arrays. So let me just put
that down for you. Java array. That is that, right? And with Java arrays, we use arrays to quite
a number of things. Honestly. We can use them to,
for example, store, store multiple values
in a single variable. That's the beauty of it. Render it can stall. For example, it can store
a collection of cars, collection of different
peoples heights, all sorts of statistics
really you can keep them and store them
under one category, maybe people's heights and
different colors you see. So the key thing to know is when you want to declare an array, you need to first define the variable type
with square brackets. And how do you do that? Well, essentially you
just do it like this. Obviously I'm gonna
make an example, but just want to help you be
familiar with the syntax. I have my square here. Let me change the keyboard
and it is over here. Let me just open it up. This
I can copy and pasted from some way I know because
I had already used this, I believe today at some point. No, I have not used it yet. No problem. We'll just
give it like this. And let me see on my keyboard where I can find a
way to close that. So, yeah, that's
the one right here. Alright. Beautiful. Alright. So
we go like this as well. Excellent, Excellent. So if you're in a
situation like mine where you don't have too much to
explore on your keyboard. You can always use that
visual cue bottom with it. And then you have cars, right? And it's a string of cars. And with this string of cars, really, you can store as many cars as you
want in there really. Because when we have done this, we have now declared
a variable that holds an array of strings
to ints add values to it. We can just use an
array literal and plays the values in a comma separated list inside curly brackets. How do we do that? Well, here now
comes the example. So let me just yell
and we say string. Let me literally take this example should be
easier to do it with what I already have strain cars and then haven't changed
my keyboard. Awesome. So screencasts and then we
say is equivalent tool. And then let's place
our cars over there. We could have, for example, Toyota, Ford from my
American friends. But as you can see, it's in red. Why is it in red? Because it's not identifying it as a string. So we need to put those
quotation marks over there. Awesome. So there it is. There it is. And then
we could have Toyota, we could have FADH, we could have Ferrari,
our Italians. We could have Mercedes Benz. We could have as many
cars as we want, but I think this will be
enough for now, really. And voila, it's as
simple as that really. And this works not
just for string oriented objects of course,
or string subjects. We could also do this
a bit for integers, that is for values
as well, right? And how we do that is
by simply doing this, just coming over to string, let's just copy that. And we have int. Now we have this and
then we say my number. We can declare Nino
variable like that. Then we open up and start
storing values from ten to 20 to 30 to 40. It's up to us to be as free and as explorers as
we want to be, right? So that's a cool thing. We start all these values in one place, my numbers, right? Then let's say that for example, you want to access elements
of this array, right? Let me just write
that down for us. Access elements of array. How would we do that? Well, to do that, or would have to do
is, for example, let's say I wanted to
access elements of this array right here,
that's this area. How would I do that? Well, all I'd have to do is
I'll just have to say system out dot print LN, preferably like
printing them this way. And then selecting
them as costs with our elements here and n here. In between my two
squares over there. What I do is I can choose
what to bring out, right? Remember when Java knows this, he knows this S. This
S corresponding to 0, the set S corresponding to two as one rather this
corresponding to, this corresponding to three, and so on and so forth. You see that if I were
to say, for example, 0, what Java gets from that is, I need to print the first
card that I find, right? Because it starts counting from oral to start
showing outputs from 0. So let's go. We should see a Toyota voila. That is, we have
a Toyota, right? If I were to say three, for example, three
means if this is 0, this is one, this is two,
this is 33 should be bends. So let's run it and see if we do get it bends,
indeed, maturities. Yep, awesome. Mercedes Benz In deed, right? So let's say that maybe
you want to do it. You want to change, let's say you want to maybe
change an array element. An array element. How would you do that? Well, to change
an array element, you just need to specify the element that
you want to change, right? So for example, let's
say that I wanted to change Ford been having
quite a lot of for today. So let's change that
just for the loan. So let's say cars and
then select fought, which one is at 123 or four. There is no force
10123 actually. So we'll just pick on the
first one, which is 01, which represents
fought, and then say Cars 01 and then
replace them by. How about Aston Martin? As to Martin? Aston Martin, de benign, deviant idea of a bit of a fan of cars,
really sports cars. That is, we could have an replays are forward with
an Aston Martin, right? So the moment that
we now have this, all we have to do therefore, is we could just simply
print cancer again. So we could just say
instead of us printing ton number three,
how about this? How about I take this guy again and place it here now after the change has happened and
just register this as kirwan. That is because it used
to be fought over there. But it has changed. It has become a stone Mountain. So we should have
our stomata now, Aston Martin DB nine. So that's the beauty of it. Now you're probably wondering
why I had to shift that. Well, let's say it
was y t Actually, let's say one's right here. Let's run it and then we see what's going to
happen over there. It's fought again. Why is that? That's because it's Java. It's reading the code here. It starts line by line. That's from 187188. So as far as 188
or 1921 is found, when I tell it to launch and give me what
value one is here, it's getting forward
because it's not yet understood or rather received information that
it should change. That change only
happens in line 194. So that's why I had to
do what I did and just placing this down here
so that when I run it, now it has understood that at 1 before
there was a change. Now Cao Huan means and Aston
Martin de be nine, right? So that's how we do if we want
to change in ROE element. But what if we want
to determine, what? What if we want to
determine an array length, for example, for one
reason or the other, it's possible when it's possible that you'd
want to find out how many elements are
there in an array. And for that, all you
have to do 3D is just to use the length property. And how do we use
the length property? Well, do this. So let me just use the same
thing we've been using here, the system that thought. So I'll just come here. Instead of printing
cars like that, I'm just going to say cars
and the length, right? So when I do this,
it should tell me how many elements are there. Bear in mind. It is not counting from 0123. No, no, no. It's counting how many values are
in the first place. It's counting 0 and x1, it's gone to one, S2, S3 to S3, and its
content three is four. So therefore, it should produce four elements are in
the array, right? Because I'm asking it
how many elements are in the areas where it says
four elements IN diarrhea, which is true, It's
1234, therefore, right? So that really is that I
hope that definitely helps.
11. Java Array Loop: Then the next thing that
we want to look at is what we call the Java. Java array loop is definitely something we
want to look into now. And we can loop through our array elements with
a for-loop of costs. And we can even use the length property to specify how many times
the loop should run. Right? So let's think about, for example, same, same, same situation that we
have our string over here. Let's say that for example, we wanted to just run a bit
of a loop over there, right? Just try and run out
a bit of a loop. So what we could do is this. We could just take this here and place it where I
can easily access it, especially this one, this
first one about the cars, S1 about the cause. Alright, let me try and
just write it again. Myself. Just over here. Alright. Here the arm, the cause. So here's our Again, let me rewrite that again. I think it's deleted
itself java array loop. So let's say I have
the following. Let's say I have a string and I have
the following as well. This training alongside that, I have the following
string with cars and let me just put this one now in bracket because I don't
need this anymore really. So I'll just come down from
here and move this one. Now. Move this one bit of
some work going on here, but should be done in a minute. Alright? So what I do is now I introduce
some bit of a loop here. I say int I is equals to 0. And right there I
say I is less cost. And then they are
length of course. And right then, then I
just say I plus, plus. And I open this
up for us to use. And as I open it up,
right like that, I'm just going to
print out and put this printing method in here just so that I can work out what's actually
going on here. Now this time, I'm
definitely not going to be printing in terms of car length and rather than using the E that has already
been placed over there. But I'm definitely going
to want to place it as well in my squares because it's an array that
we're talking about over here. It's an array, so we don't
want to lose sight of that. So that is my eye
and it's ready. Let's run that and see
what we come up with. And they it is, we have the following elements. We have Toyota, we have Ford, we have, we have bands, right? So in essence, what we've said here is we've
used this technique of this length property to be able to print out
everything that is in there as long as it does
not exceed its own self, as long as it doesn't get
out of its array list, then this should
keep on happening. Right? Then, on the other hand, we could also just talk
about how to use R at, how to look through, um, through an array with four each, with a for each statement, if you want to write the for each loop, if
you want, right. The syntax for this definitely occurs in
the following manner. We just simply do this. We could say for with lower
caps off cancel four. Then we write the type of whatever variable that
we're working with. And then there's a
variable over here. And then we just
stay the array name, array name as well. Right here, no space in between. And we open up whatever it is that we
want to be dealing with. You see that? Obviously we won't have
to close it as well. So an example of this
would be asked doing what, for instance, still using
exactly the same thing. Let me just put them in Commons. Put this in common. Right? So let me just take
this, all of it. We definitely are gonna be
using it right here again. So let's say we have the same
string of costs over here. So what we could say
is we could say for this train I with being the syntax of costs
with causing there as well. And also opening
up our brackets, we have system dot out, dot, print LN, print LN. Then we can just print
out i, for example. We can just print out
I just like that. Then we try to run it. And let's see what we get. We come up with the
same thing, right? So it's really just a method of, you could say I'm a matter
of technique if you want. You can definitely use them as freely as you
want to use them really, It's just a matter of technique
and preference on how you prefer your code to look like. Then the next thing that
we're going to look at, let me just put this in Commons. The next thing that
we're definitely looking into is called Java multi dimensional dimensional, multi-dimensional array. Java multi-dimensional
array, right? And for this one, you just need to
bear in mind that in order to create a
two-dimensional array, you need to add
each array within its own set of curly braces. Syntax of this would
be something like int. And then let me just take
this braces as well here. Let me just do this. Great. Yeah. My num bus for example. Then we say, we equate
it and then put this in its own braces. So just be very
cautious of this will have two events, be 1234. So there's four over here. We'll stop right here at four. So let me just copy this guy
and paste them right here. And then we say coma because
we are still continuing. And we open up. And then you say
567 for example. And then we close
it up like that. And not only once
but actually twice. And then we lock it
like that, right. So this is an example of it. If I were to take it
out of your comments just so that you can see if it's recognized by java. Let, let, let me
just try and do that and you can check it
out for yourself. So here it is, Here it is right here. And we have end over here. And our braces here, A2, B2. That's why we're having
an arrow over there. So there they are. Each one, you could say each
one represents its own thing and see that
this one represents this one. This one here represents this one right
here. You see that? So that's the
syntax essentially. That's the syntax essentially.
12. Multidimensional Arrays: So it's just worth noting that now that it's
in this fashion, it means that this mine numbers here is now an array with two arrays as
its elements, right? To access the elements
of my numbers array, you need to specify two indexes, one for the array and one for the element inside the array. So the example I could give you for this would
be something like this. For example, let's say we use exactly the same thing
that we've used here. And then we were to say int
x equals two my numbers. And I were to say,
for example, one. Let me try and just copy this so that I can be
much, much faster. Do I want to put it
in this question? Rather, I'm going to copy
this is what I want. This is what I am because
I need to specify off. The first one I'm
specifying is the array. And then the second one that I'm specifying is the
position in the array. You see that the
array and then the position in the
array, it's self. So let's say I were to
specify something like 12. Yeah. So by specifying 12, what's actually going on here? What am I? What am I
actually saying here, right? So by specifying one here, this is the one that
this first brace here, or rather square here, contains information
telling us about which, in which area are we
in, are we in array 0? Remember, this is
array an array 0. Remember this is our a 0, or we are in array one. You see that? So this is our a 0,
this is already one. This is as far as
this is concerned. The second square over
here contains information as to now that we're
in that array. Which position exactly
are we referring to to extract our
element from the sea?
13. For Loop: So we, we just
continue like this and say system dot
out, dot, print n. And we print out the value of x because it's just evaluate, it's just one value out of that, just verifying that we don't
have anything else up there. And we printed out here. Therefore, we predict if
I sell 12, it means 01. We predict ourselves
to be in here. Position two per
centroid is 012. We predict it to give
us a value of seven. So let's run it and see
seven day it is beautiful. That is, so it's
seven over there. And we can also use a for loop inside another for loop to get the elements of a
two-dimensional array. And of course, we
still have to point out to the 2x's indices rather. Definitely still
need to do that. An example of that would be, let's say using for-loop,
using follow-up, right? So an example of this
would probably be, would have to specify and create a method here and
say, call it public. I just want to verify
what method we're in. We're in public
static void already. So that's good already. Except that what we're
gonna do is we're going to come all the
way down here again. And just, we don't
need to specify the method because we
already have it up there. So it's just going to be end. And we copy our end here again. And instead of even
writing all this, I could just copy this really. Let me just copied and pasted right there and say my numbers. And instead of just my numbers
or put it as a one here, just to differentiate it from the previous scenario
that we had. And what I'll do as
well is I will just say for int I is equals to 0, I is less than my
number one, right? Because this is the
particular one that I'm actually referring to. Their length.
Remember that length? It's meant just like that. It's with the length. And then we make
an increment of I. So there it is. We have an increment of Iowa. And we open up our
for loop over here. Then we add another for
loop right in here. And we say for int j is equals to starting from 0 as well. And saying 0 rather non-zero. I prefer using j non-zero. My numbers again, dot
length. Not really done. Dot length. Let me just place it
like this instead. Let's go back. And I'll just take this here minus two squares here
and place them here. So I have my two, my square, just one of them and please my eye right in there. And that's the length
that I'm looking for. That's the length
that I'm looking for. And just keep on going
up with j really, keep on going up with j really. And for each of these scenarios, I need to be printing
out values here. And in particular,
printing out here my numbers and just having
them in terms of I and j, normally it should
work like this. So let's just run it and
see what we get from that and just better understand what's
going on here, right? So it's really just a situation whereby we're trying to deduct or come up with all
these values from here. So we have an int and
then we save for end, whereby it's because
essentially what am I saying? I'm saying, as long as i, which represents the area mean, Remember array 0, array one. As long as this is less
than its own length, which is, it has a length of
one from 0 to one, right? So it means it will take
the first 0 and come here. Because 0 is allowed here, it's definitely less than one. Then it will come here and print out what's
going on here. Print out what's going on here. In fact, the length here,
urbane mind It's actually two because it's the
length of this, it's 12. So it will take 0, check it, and see that 0 is
definitely less than two. And move on to say
that what about j? J is equals to 0. And then say j is less than, j is less than three,
as you can see there. For j, how many elements? They in day in the first place. So for j, in fact, it's not three elements. It's gonna be 1234567. So it's going to be
seven elements in there. So it's going to just take
and store that value of 0. So let me just put a
comment here just so that you can see it better. So it will take, as I said, just take the value of 0. Firstly, we will
have a 0, right? Then against that 0, it will run it and
take another 0, right? And then it will come. And let me just copy this. Print out the value that is corresponding to
that and 0 comma 0. Obviously that's
gonna give us one. Then it will come and it
will cause an increase way. It will cause an increase as
far as J is consent, right? So it will just
cause an increase wages consent, so it will be 01. And therefore 01, as
you can see, it's 0, it's in this array, one, it means true here,
so it's gonna be two. And then we come over here, we say, What about three? Here? It will now be two, which here will make it three, as we can see it over here, and so on and so forth. I can probably assumed that
you already seen a bit of a pattern here that's
definitely emerging. So let me just change
those values for us. So it's going to be three here, it's going to be four here. It's going to be four here, it's going to be five here. Then here it's
gonna be five here, leading to six here. And then here it's
going to be six here, leading to seven here. Now why will it eventually,
and by the way, when it gets up to
let me see 0123 here, normally here it's
going to change. It's going to be rather one. And then, because
it's now in this set, and then it will look for
the position here and start again from 0, right? So it's going to be like that. And then here it's
going to be one. And then here it's
going to be again one. Then here it's going
to be now one, and then here it's now
going to be two, right? So essentially this is what will then eventually give
us all these values. If you were to try and
locate them yourself, we will need a pen or
a pencil and paper. And try to locate 00. You'll see that it's one
01, it's 2023034105116127. You see that? So
essentially that's how it's actually making sense
of what's going on. Right, right here, right, right, right here, right? So what we're gonna do next is we're just going to
move on to the next area, move on to the
next thing really.
14. Methods: Alright, so the next thing that would definitely going
to look into is, has got to do with Java methods. Probably the most
important thing that you will come
across when you do Java. And to properly understand this one we're going to
define freshwater method is a method simply is a method
is just a block of chain. This block of chain, or a block of code which only runs when cold, right? So you call it, then you can
definitely make use of it. So you can pass data known
as parameters to a method. And methods are used to perform certain actions and they also
known as functions, right? Right. Yeah. And why would we
want to use methods? You're probably asking why. Well, truth is, it's just
to reuse code, really. Reuse code. Define
the code once. And you will use it as
many times as you want. As many times as you want. Alright? So that's it, right? And the key thing that you probably are wondering
now is therefore, how do we create a method? In order for a metal
to be created, it has to be firstly
declared within a class. And it is defined with
the name of the method, followed by the
parenthesis, of course. And of course, java will provide some predefined methods such as system.out.print,
line as you know. And, but you can also create your own method to
perform certain actions. An example of this would be, let's say you wanted to
create a method inside main. So what you do is you'd
just say, let's see, let's go all the way up here
and just put everything here on the comments
because we don't need it. Right, right, right here. In our public class, we'll just, instead of
even making it public, will just make it
a static class of the following manner and
then call it, call it. Let's call it myMethod. It's easier to just call it like that MyMathLab main method. And my method,
that's what it is. It's just our own method, right? And with this, our own method, what I'm going to do is I need this cos let me put
this guy right here. Make sure that everything
is under comments and that there are no errors we collecting here,
especially here. And here. We don't need any
of that, Right? Right. Excellent. So now that we have
it in this form, we'll just call it our method. And then literally here we could have pulled to be
executed, right? This is what we need to execute. So an example of this would be, let's say that you wanted to write something
overflowing nature. For example, let's say that
you wanted to say, hey, system dot out, dot print LN. And then you were to say, Hey, This is fun.
This is one, right? So he went on to say that. Then let me just decrease
this distance here. I usually prefer them
like this so that I can know what exactly
I am doing right? Then I could alternatively
just pub, public, static, void, main, and define
my string over here. My string right there. Let me just, I need some
squares or get them from here. And get my squares over here
and put my string like that. And I'm just like this, I will just call this a RGS. You can always put this
in front or behind. It's not a big deal really. Define whatever it is that
I'm trying to execute. So I'll say. Mymethod end. Here. As you can see, Java
actually recognizes that I can run this, and when I run it, it should
just say, this is one. This is one. Why is that Swedes were running this and it's essentially just, it has understood that
this is a method. When I call up on this, I'm calling upon this
to be done, right? So imagine I were to just do this and place
this here plus this, plus this, plus this here. And it's like literally using the same thing again
and again and again. So each time it will
just call again. This is one, this one,
this one doesn't. So I can use it as
many times as I want. And obviously this is
a very Lee example, but it just helps to prove
to you that a method can be called multiple times with no problem
at all, right? Which comes in very handy. More further we go in the whole aspect of
methods and all you see. So just information
can be passed. Two methods as parameters, and parameters act as variables
inside the method really. And parameters are
specified after the method name and
inside the parentheses. And you can add as many
parameters as you want. Just separate them with a comma. That's the key thing really. And then this example that
I'm about to show you has a method that takes a string called I'm F9 as a parameter. For example, when the
method is called, we pass along a
first-name which is used inside a method to
print out the full name. So let's go right into it
and see it ourselves, right? So let's say that we
have public static void, and then we have a string here. We call it fname,
like I just said, F name literally just
means first-name, right? Then here we'll just say fname. We're calling it again. And plus Reference runs. When I do that, I
can come over to my public static void over here. And now still specify it because I should specify what's going
on, what happened in here. So I need to give
names, for example, tom, Tom in there. But remember Tom is a string. So I should have it.
Tom as a string. And there it is, It's already identifying him as a string. And then in here as well, Lindy. Lindy, yeah. Then in here I have Sam. And then in here I have Rob. And then in here I have Lincoln. And yeah. So if I run this and let's
say that this is a mean, literally mentioning all
this for one family. So let me say it's a family of Thompson's
Thompson's sending. I have therefore Tom Thompson, Linda Thompson's,
Sam's, Thompson's, Rob Thompson's and
lick on Thomson's. Now you're probably saying, hey, what I wanted to add space
between them, no problem. You can always just
do that by putting, by putting this right here. And when you do that, it should have a bit of
some space between them. Let me put it like that. And voila here. Tom Thompson was Linda Thompson. And sometimes it's
all one family if you want one big family. Alright? So just bear in mind that when a parameter is passed
to the method, it is called an argument. So from the example that
I've just given you here, fname is a parameter, while this name's Tom Lindy, Sam Rob Lincoln, or arguments, you see that they're
just arguments. Got the same really
even in mathematics, whatever is in the parenthesis,
we call it an argument. And just bear in mind
that you can have as many parameters as you want. And just say that
for each of them, I had wanted to specify
as well that hey, I want to know their ages so
I could even put age here. And for once I do this, I need to put a comma
and say Tom is 12. For example. Lind is the oldest one. She is 28. Sam is 20. Rob is somewhere in-between 25. And Lincoln is just for yeah. So if I run this, it's now going to as
well show their ages. Alright. It's now going to as
well show the ages. Now as you can see, it's not yet showing that a is why
is that? Why is that? It's because I did not
specify here that, Hey. I also want the agents, right? So let me just say this plus, plus and H days age. So I could even say maybe add a string that says
something like the data is. Then I say that h, So let's run it. There it is. Tom
Thompson's installed. Then this is tall, 828 sentences is 20. Rob Thompson's is a 25, and Lincoln Thompson's
is four years old. So that's the beauty of it. And you just need to bear in mind that when you are working
with multiple parameters, the method coal must have the same number of arguments
as they are parameters. The arguments must be
passed in the same order. So we could also have
some return values. So just bear in mind that when we talk about
return values here, we use the void keyboard, we use it in, we've used
it in the examples above. It indicates that the method
should not return a value. If you want the method
to return a value, you can use a primitive
data type such as an end or a shower
instead of void, and use the return keyword
inside the MathLab, Right? An example of this would be, Let's say that we have
something like this. Let me see. Let's put this in comments here. We don't need this one. So instead of static void, what I'll do is I'll
just say static int. You see that? So when I say static int, it means I'm very sure
that I want to work with ints if you
know what I mean. So I can just do this
and just say int age. And here in so doing, what I'm actually saying is
that I'm saying that, Hey, I also want you
to return my age. Alright? So I want you to return age. Could even say age plus a number of example
is plus seven. And it can, it can
definitely work. And then I definitely will
not be needing this for now. So let me remove it. And
then when it comes to the static, remove that mistake. Yeah, when it comes to
this void over here, what I'm gonna do
is I'm going to say system dot out, dot print. And I'm going to print
out my method of age. And I give my value of age, I give it as five for example. And by giving it five, it means when it
will detect that all ages five and it's going to
say five plus seven and get, any artist should
get 12th normally, it should get 12,
normally. Beautiful. So there it is. It's gotten 12, right? This example definitely returns the sum of the methods,
two parameters. You can be as creative
as you want really in definitely
working around this. And just a more
elaborate example of this would be what if I had said and why as
well is also there? Rather not just in age, let me call it H1 and H2. Be more practical. Age two should make sense. Now. H1 and H2, H1 is then too rather, let me put it as H12. But what does just
as two really, that's, that's really
all that I need. And by essentially doing this, what I'm doing is I'm saying
return is one plus j2, for example, which
should be five plus two, which should be a seven. So let's see what we
get. We get a seven. I could still even add a value, seven plus three, which
should be a ten now. So there we go, it ten now, right? So that's, that's really how we really are able to make
use of the two parameters. Yeah, you can also just store
the result in a variable, which is recommended of course, as it is easier to
read and maintain, its easier to read and maintain. So just a more elaborate example of this that I'm
talking about would be, let's say that you
had, for example, x here and y here and the
shortest and the most, the ones that we love the
most when we talk about mathematics is y here, and there's an x over here. And what we wanna do
is we can just simply specify when a z here if we want and z here we'll
call it our method. And call up on 54. Then we just print out z. Right, isn't only to print out all of those things that will
printing in the first place where all we want to do is
now we can just print out z. And let me just close this
properly as well here. And my dad. Yeah. So it should work
right away here. So let's see. It should be without my
method will just say x0, really, that's all we need. So we'll just run
it and we get nine, that's four plus five, which should just
be simply nine. So that's that's the other
value to it right there. The other venue
definitely to it. Right? Right. So the next one that we're
definitely dealing with is what we call the Java
method overload. Alright? So the Java method
overload works like this. With overloading, you can
have multiple methods that can have the same name with different
parameters, right? Example of this would be maybe if I were to say something like, hey, what if I have an end? My method? Rather let me call it med, one is equals to, not equals to or
rather just met one. And then I say int x. And yeah, instead of using x, maybe I've used it too many
times into T is better. I think entities, but
definitely I could also have a float and just
call it mad too. And I'm colored float are and have double as well. Mad three with a double
inside of calls. An a and a double of B. Yeah, definitely, this
should definitely be though, where we can now also do it. What we can do is we can
have an example of this. An example of this would be, let me just get rid of
this because there are some things in there that I want to clarify and
better with an example. So let's do it. Let's say that. Let's come back to our
static method over here. And we say is that the end plus. Let's call it a plus method. Then with this plus method, what we have is we have
int x and y definitely, and we return x plus y. And then we say static, static, double, static double. So we have a static
double Plus Math PFAD with double over here. And this is with an int here. Very important. Here it's doubled in
here, in the brackets. I'm going to put my two
doubles and call it a double x, double y. And just have this
return x plus y. Again, right? And just have that
public static. Let me remove all of
that. I don't need it. Just come back into
saying Let me just make sure that this
is where it belongs. Yeah. Yeah. So we could have right here public static method that we always use public
static, void main. And have this train. And on top of that
string we will have our squares as well. So let me just get them. As you know, with my keyboard. I have to get them
all the way down. Like here. Definitely here. So we get them string and RGS. So right there, we open
this up and opening it up. What do we say? We say int num. For example. My num one is equals
to a plus method. And we please venues like
seven comma nine for example. Why not? Then we move on to say double. And we say my num two, which is equivalent to
plus method double. And here we're going to just put something that
is a bit of a fraction, maybe five-point, let's see, maybe 5.4 with 6.9 for example. That. And then we'll just say a
system dot out dot print LN. And what we want is we want
to print our integer venue like that with a string that is, and just plays all our r value that we want to print out here, which is going to
be plus num one. Right? Then we're gonna do a similar
thing right here says system dot out, dot print LN. And right in here, we're going to have a double in a string format with alarm, alarm num two over there, right? So let's run it and
see what we get. Normally it should give us
sums of those two values. Here. It's going to be seven
plus nine, which is 16. And a double here is
going to give us, let me just put this as
well so that it's relevant. A double here is going to
give us 5.4 plus six by nine, which would be just 12.3. Alright? So definitely this is
how we can make use of overloading if you want. Alright.
15. Scope: Alright guys, so
the next thing that we definitely want to look into is what we call the
Java scope, right? So there it is, Java scope. And whenever we talk
about the Java scope, essentially what we're
talking about really is just the region that
is accessible, right? That's where variables
are found, right? So let me write it down. We're talking about something
of the following nature. In Java, we know that variables themselves
only accessible, only accessible inside,
rather inside a region. They are created. Let me make sure I put that
right side of the region. Created is a double S over
Matt said originally created. And that itself is what
we call the scope. This is our scope.
So that region that, that makes them available, That's the scope
essentially, right? So variables can be declared
directly inside a method, and therefore can be available
anywhere in the method following the line of code
in which the way declared. An example of this would be literally what I have
written right here. I just have a look at what we were doing here with
our previous method. So we have a public class main, then we have a method over here. We call it maybe, let
me talk about this one, especially static double
plus method double and then we have that
and then over here we have the interest in one. Let's put our focus right here. Focus here. We have public static,
void main, right? Then in there, what do we have? We have we put whatever
you want to put in there. Right. And it's therefore accessible. All even whatever we
declared out there, if it's able to be called
for in here, right? And if that doesn't make sense, I can always just do
this for you just to further clarify what it is
that we're talking about. Can think of it like like this. Let me just clear
out all of this. We don't need this
anymore really. All the way until
right here and delete. And let's just go
directly and say public. Not upper cap, a cap, a public static void. In fact, I should just
delete the whole thing, rather just remove this top
part here. All of that. Yeah, that's what I don't
need and I don't need that. But this I need
absolutely just to prove my point over here
of what I'm talking about. And therefore, what
are we saying here? We're saying that we could have something like this end here. The code that can be here
cannot use x, right? That's the area. That's how we have defined it. And then we could then
specify that integer of x is equivalent to 100, right? And with that, we can say, we can create something
that can use X. Code here can use x. And we call for that
code that can use x or we say a system
dot out dot print LN and then recall,
and we print out x. In this case here
we had nothing, We have not been declared. What exit solve means, you see, so we needed to
call it first and declare that variable of x and therefore be able to then call
up on it and print it out. So if we were to run this now, it should be able
to just give us what x essentially is, right? And what I'll do
is just to ensure that everything is well, let me just put that down here and put it right
here. Beautiful. So let me just run that for you and see for yourself
what we get out of this. So we're currently running it. And a 100. That's because x, we've
declared x as a 100. And this is just a method scope that we're still
doing right here, right there in something as
well called Block Scope. And a block scope, you can think of it more
like this, right? So a block of code refers to all of the code between
the curly braces, this other color braces
we're talking about. So anything that's
found in-between these two colored braces, this and this right here is
just a block of code, right? Now, variables declared
inside a block of code only accessible by the code
between the curly braces, which follows the line in which the variable was declared. To elaborate much further, let's, let's make an example. Let's say right here with the same example
that we have here. Let's say we do
something like this. Let's say we were to, instead of having x right here, Let's say that we were to do
something like this here we know that a code here can
not use x definitely. But then what if I were to do something of the
following kind to say and put curly braces
over, then open the site. So you notice whatever
that is in-between these two curly braces right
here would be a block. This is a block of code. Whatever I put in there would
just be a block of code. And that block of
code right there. It can not use x as well. Light cannot use x as
well. Can not use x. Nevertheless, if we
were to, for example, just specify and say
x is equals to a 100. Again, maybe nothing
special about a 100, just like using it. We can see that the next code that we implement
can definitely take consideration of what
access code here can use x. And like that, we can just call up on it and it's
a system dot out, dot print LN X. There is no error as we can see. Then we see therefore
that this block of code ends right here. Let me just do this. Roller. So the is equal n right here. Put a blog and here. Then the code here as well that will be outside
of the curly braces. Code here, can not use x. So you see that, You see
that effect that it's within this curly braces
allows you to access x. But the moment it comes out, you cannot access any
x anymore and datas, we have our 100 over there that we've just
specified, right? So essentially that really
is it about block scope and bear in mind that block of code may exist on its own or it can belong to an FYI
of false statement. In the case for any case off. For statements just bear in mind that variables declared in the statement itself also available inside
the block scope. It's just something
else that you want to keep in mind whenever you're
doing your coding, right? Alright, so the next
thing that I want us to focus on is what is
called and all of this. Let me just delete it. We don't need it. The next thing we want
to talk about is what we call Java recursions. Write Java recursion. So let me just put it
down for us right here. Let me just do those. Yeah, java. Sometimes when you hold shift for awhile, this
is what happens. Java recursions. Awesome. So what is a recursion? Recursion inherently is, is just the technique of making IF function call itself. So if you have a function
that keeps on calling itself, then what you've just
done over there is what we call a recursion, right? So there it is, call itself. This technique definitely
provides a way to break complicated problems down into simple problems which
are easier to solve. And of course this might be a bit difficult to understand, but the best way to
figure it out as always, just encoding,
you've got to do it. You've got to get your hands dirty and just do it yourself. You see that? So let's just check out
this example over here. Let's say we want to use, use recursion to add all the numbers. Up to ten. Want to add all numbers? Well, one thing is proper
English word here. All numbers. Up to ten, right? So how would you do this? Well, you probably want
to start first of all by just declaring what method
you're going to use. So we're going to
use static, void, main, and call string. Here, ARG is here. And right with our
string over there. What we wanna do is just wanna use the squares right here. So let me just call them that. And get back here and change my keyboard back into
its normal default mode. And then int result definitely within our
block of code that is, and we say sum of ten. That's what we're
looking into some often. And shortly we are
going to define what the sum of
ten is all about. What does that mean? What should Java understand by taking into
consideration whatever is called the sum of ten, right? So we print the result here. Then what we wanna do is, right after taking
this out like that, we want to create
another method here, the one that's going to define
what some itself means. So we'll say int sum. And then we'll just call up on the integer value of k.
Yeah, so there it is. Now we recognize it. So Java is able to interpret
and say, oh, okay, so the value of some,
there's a method that is able to
call up on the sun. So it's yet to understand
what that method actually saves and how
it should behave, right? Alright? And then we just say if k, if k is greater than 0, what should happen
is the following. We should return k and add the sum because we want it to be
recursive k minus one. Yeah, so that's where the
recursive ability comes in. And I'm gonna interpreted
this over you just sorted, you can now thoroughly understand what exactly
is going on here. Otherwise, should do this. Meaning of the condition
is being as not being met, just simply return
the value of 0. And that's about it. That's really about
it. That's about it. Let me just do
this so that I can always get us lost over here. This guy, I'm going
to take them and drag them and put them
all the way here. Just so that you remember this
guy down, down, down here. Sims, there's a bit of an
error somewhere here in this block of a block of code that we've
been dealing with. Yeah, just do that. All of the error is gone. Alright, so essentially
what are we saying here? What we're saying
essentially here, and I'm just going
to put a bit of some commentary here just so that you can better
understand what's going on. We're adding up values and by the way,
let's run it faster. We can see if it works or not, and then we can now move
on to the next thing. So there we have
it. It's 55-year. You can always do that yourself. Just try and says 0 plus one plus two plus three
plus four plus five plus six plus seven
plus eight plus nine plus ten, you get 55. Now how does this
occur actually? Well, what's going
to happen is this. I'm going to take our value, our result value here. And normally about this, it's a class obviously. And then what Java
is gonna do is gonna check for the
value of the result, the sum and say, Oh, the sum is eviction is actually
a value of ten. And then say, what does
this method of some do?
16. Halting Conditions: How does it do actually? So it will just come over here and try and understand
what Sam is all about. And it will register a
value of ten here, right? Because we have chosen
it to be ten and then say ten is greater than 0. Is that true? And then you
say, Yeah, that's true. If that's the case,
then return k, which in our case is ten. So it will return
the value of ten. And then plus the
sum of k minus one. In our case, k is ten. So ten, essentially
it's 1010 minus one. Essentially that's what's
going to happen, right? And what is ten minus one? That is actually nine. So essentially we're
talking about the sum of mine, some of mine. And it's going to take
that same value of nine. Take it all the way back. And we're still in this loop. We're not coming out yet. So it's gonna go
up here and say, nine is greater than, is nine greater than x4 and
say, yeah, that's true. So we tend to value
of nine, see that? Essentially it will
just give you, now it's no longer
a sum over there. It's just gonna be the
value of nine itself. Plus the sum of, it's going to be nine minus one, which is the sum of eight. See that? With the sum of eight, the same thing is
going to happen. It's going to go over against
the eight is greater than 0 and then return
the value of eight. So now we have the
value of eight itself. Not a, not a method of eight, but its a value of eight itself. And then it will
say sum of seven. Then the same process
keeps on happening. And this is what
makes recursive. Remember, repression
by nature is repeating a function that
keeps on calling itself. That's what we're doing here. We're making this
function of some coupon calling itself again and
again and again and again. So it will keep on doing that. And it means we'll have
plus seven plus six, plus five, plus four, plus three plus two. I'm going to space them
out plus one, right? And then when it gets to one,
what is it going to say? It's essentially
just going to say one is greater than
0. Is that true? Return one plus sum of one
minus one, It's 0, right? So it's going to say plus
sum of 0 right here. The sum of zeros. Then what it's gonna do is
gonna take the sum of 0, say the value of k
that's now defined as 0. So you will say 0
is greater than 0 and we realized
that's not true. That's false. And salt, because this is, this condition is no
longer being met, it will now skip. Skip this conditional
statement and come here and say else we tend
0, which is true. So it will just end up just adding an additional
0. You see that? So as you can see, that they
keep on adding together. So here it will just say
ten plus 919 plus eight. Thirty-seven, fifty four. And like actually,
it's going to be 19, 27 for 40, forty five, forty nine, fifty to 5455. You see that? So
that's how it keeps on summing up on this
values themselves. You will add them. It won't be recording them. I'm just recording them
like this for your sake. But what it will do actually is that each time it will keep on doing and doing something like ten plus nine indents in 19
and then send 19 plus eight. And then give you, like
I said, 2727 plus seven. And then it will give you 34. And then it will
add a 34 plus six, and we give it 4040 plus five. Give it 45, and then it
will say 45 plus four, and then it will give it 49
and then 4949 plus three. And it will give
it 5252 plus two. And we give it 54.
And it will say five to four plus
one will give it 55. See that? So that's how it's mounting
up and actually grow into a final value of 55, right? So just bear in mind that when the SUM function is called, it adds the parameter
k to the sum of all numbers smaller than
k and returns the result. So when K becomes 0, the function just returns 0
when running in the program follows the steps that
I've just shown you right above, here, right? So of course there could just
be a bit more creative and maybe even give like
a halting condition, a stopping condition of when
it should know when to stop, under which circumstances under which scenario shouldn't stop. Talking about that, talking
about the Halting condition. Halting condition. When you talk about this,
we know that just as loops can run into a problem
of infinite looping, recursive functions can
run kill a problem of infinite recursions if
the stopping condition isn't properly stated rate. So hence, why we
need to learn about the heart and conditional the stopping condition
as I like saying, I'm going to just put this
force here on this way and just put it like that. Right? Awesome. Great. Okay, it's okay now,
so we took him about the Halting condition and
infinite recursion or cares, or rather it's when
the function never stops calling itself.
Just put the dead. Infinite recursion is when a function never stops calling itself. Right? Every recursive function should have the Halting condition, which is the condition where the function stops
calling itself. In the previous example. For example, the Holton
condition is when the parameter k, as we saw, when k became 0, that was, that was how
Java had to know that, oh, we stopped
right here, right? So it's helpful to
see a variety of different examples to better
understand the concept. And in this example, the function adds a range of numbers between
a start and an end. So the hot end condition for this Lucas's function is when n is not greater
than stat, right? So we're going to make
an example here and just try to see if we
can use regression. So we're going to use recursion and we'll
put it down for us. We concussion to add want all numbers between 510. So all numbers between 510. So we're going to do that and just see how our
whole thing evolves. So we're gonna go back
here, right, right here, and just modify the same
thing that we've been using. So just the end result. And then for some here we're
going to make it a bit interesting instead
of just saying ten. Since we want it to start from five, I'm going to say 510. Yeah. So it gets to be red because as far as the method of some
had already been defined, it had only one
parameter in there, so it's a bit confused as to what's actually
happening here. So we're going to change it
when this permanent itself. We're going to have a
starting permanent of int and then ending Paramount
of India right here. Now it's okay because
we've defined it as two parameters which
have been recognized as five as a start and
tendons the end. And bear in mind that
decides just names of variables for me to know that this one is starting
the others endings. Because studying and have
particular functions that are Java is able to recognize that starting in means
a sudden endosome know, I just called them that way. I could have called them x, y, z, whatever I want, right? But what I'm gonna do is
that I'm going to say if, if the end is greater
than my start, just like we had done
on a previous case. The end is greater
than my start. Then what I want to do
is that I want to return the end and have my stat do following. It's gonna be start
and end minus one. So it's going to
keep on doing this over and over and
over and over again. And then it should
return the end. Now, literally almost like the other question that
we were just doing. If you pay attention, you
are able to see that it really is almost like what we've just done a
few minutes ago. And so we're going to go
on and run it and just see what's going to come off it. And there we have it. We have valley over there of I believe
that should be what? Forty-five. Is that it? Yeah. I think it's 45. So that just tells
you that one plus two plus three plus four
are simply just do what? They add up to ten because it's the difference
between the other. But five plus six plus
seven plus eight plus nine. Up to ten, it genuinely
gives you just what? It gives you 45 itself, right? So there we have it amazing
people than we have it.
17. Object-Oriented Programming: Alright, so the next
thing that we want to look into is what is called. And this is probably one of the core things that we
definitely want to know in Java. It's Java, OOP, object-oriented
programming, right? And that's what
all P stands for. O P essentially
just means object. Down for you. The object oriented programming. This is something
you're going to come in contact with a lot of times. So just bear in mind that procedure or
programming is about writing procedures or methods that perform operations
on the data. While object oriented
programming is about creating, creating objects that contain both data and methods. But open type over that, both data and methods, right? So object-oriented, there's
a hyphen nominee over that. Object oriented programming has several advantages over
procedural programming, right? So those advantages
are advantages of 00 p over procedure. All programming. Role programming. First one is o p is faster
really in the hole. It's able to be fastly
done and executed as well. And so that's the
definite and wind at you want to do if you are working on deadlines and all. And another thing is that
all P in simply provides clear structure for problems for programs. So there's a clear
structure for programs. And also OOP, helps to
keep the Java code dry. And by dry, what we mean is you don't have
to repeat yourself. Don't repeat. That's what drives you solve. Let me just write that down properly so that we have it. You don't have to
repeat yourself. And therefore makes
the code easier to maintain and modify
if you want to. And of course, debug
because there's code. Especially according a
lot of things you find the bugs eventually
come into the code and you want to have the value of all p's that
it allows you to easily, without much of a hustle,
really debug or removed bugs from your code and
all p as well. And makes it possible
to create reusable, reusable applications with less code and shorter development time. That's the beauty
of t right there. That's the beauty of it, right? So just bear in mind
that the DRY principle or the don't repeat
yourself principle is about reducing the
repetition of code. You should extract out the calls that are common
for the application, then place them at
a single place and reuse them instead
of putting it. You see that? So
that's the beauty of object-oriented
programming right there. When we talk about O P, we cannot talk about, we cannot talk about
OOP without talking about some understanding or a thorough understanding
of classes and objects. But it's the difference
between those two and what exactly adults. Just understand that
clusters themselves, essentially an
objects rather than the two main aspects of
object oriented programming. Classes and objects are
two main aspects of o p. And you can think
of a class really like a cluster of foods,
for example, fruits. The objects of this
class will be what? The objects will be. The different fruits in there. You see that we
could have an apple. You could have a mango, you could have a pair. You could have a banana. It could be all these things. You can think of a class really more like
something that's containing other
things inside of it. More like what you've
been hearing seeing here is a cluster and then inside the class we have different objects that
we can talk about. You see that all the class of the same
class, you see that. Another example would
probably be a class of cars. Class of cars. And inside of cars, we can talk about Volvo, we can talk about bends. We could talk about how Honda, yeah, on the end, Audi, right? So they're all costs
at the end of the day, but the class is the thing
that contains them row. So you can think of the
class as a template for objects and an object as
an instance of a class. Alright? That's the term that you'll be hearing a lot
instance of a class weight. So that's important. And then when the individual
objects are created, they inherit all the variables and methods from the class. And we'll definitely
come into this a bit later on in more depth. But for now, that's
just about it for now. Alright, so the
next thing that I want us to look into is what we call the Java
classes and objects. I mean, we're already begun
them a bit over here, but we're just going to delve
into them a bit more now. So knowing that Java itself is an object-oriented
programming language, everything in Java
is associated with classes and objects along with its attributes and methods. For example, in real
life, Kaizen object, the chi has attributes
such as weight, color, and methods such as Dr and
break almost a surf methods or C. So a class is an object constructor or a blueprint for creating
objects, right? So in order to create a class, we usually use, we use what? We use the keyword class. So an example of this would
probably be something like something like this. For example, I could say public. This is just a bit over. Just for you to
understand how you do it. Public class, as you've
seen us do at many times. May. Then, obviously we're going to have this curly braces here. No one could say x is
equals to phi, right? So that's just something that we've been
seeing so many times. He usually did have already
does this for you most times. But then as you advance their scenarios where you might
want to do this yourself. But for now this is, this is all that you might need. This is okay for
now, makes sense. And obviously we're going to have a breeze here to close it. But just because
it's an example, I will not go this far
to show that, right? But then in order to
create this as to creating a class,
it's really this. What about creating an object? And object? What to create an object? What we wanna do this. In Java, we need to
start off firstly, an unordered and in Java, an object is created
from a class. We have already created
the class named main here. That's the name of the
class, It's called main. So now we can use this
to create objects. To create an object of main, specify the class name
followed by the object name, and use the keyword. New. Example would be exactly right here in the
same class here, Let's say that we were to
create an object into say, public static, void, main, we're stating will recruit
in this object string. And I'm going to just take
this part right here. Literally the same
thing that we want. String. And then we are
just going to say in main and colon object my object. So calling a method
here and we're taking all our object and we say new, may see new main and
right there we also say system dot
out, dot print LN. And then we just seen
my all and close that. Then we could have
been subtracted from x if we weren't
really consuming. You already had a
value of x over there. And we can run that. You see. So let me just exactly copy and paste
exactly what we've done here. And let's put it right here. Not in the comments anymore, but in actual thing that will still running
at the moment. So let's say close it up here. So here we already
have a class already, so we don't need
to do that again. Then let's see, here
we already have a public static
void done for us, so we don't need to do it again. We can just literally
take the same thing here and just place
it down there. And console this console bad. Alright. And we can take this, put it above here, because it's an online
class, the general one. And we already have
everything in place. Alright, so here
the challenges that Bain mind that we have our class name as
Hello World, right? Since our class, namely, we already stated it as Hello. Hello World, what we're
gonna do is we're just going to call it the world. And when he as
well, Hello World, That's the default name
that we have over here. Of what we're talking about. This is small m, my OB gene. And it's gonna read x. It's not minus.
18. Class & Objects: Alright, so the next thing
that we want to look into is just really focusing more on the Java class
and objects, right? So we're just going to focus
more on that, especially. And just bear in mind that Java, being an object-oriented
programming language, everything that we do in
Java is associated with either these two things here
in the class or an object, along with its
attributes and methods. So for example, in real life, a car is an object
and the chi is what? It has, attributes such
as weight and color. And methods such
as Dr and break. You can think of methods
as acts. What to do. You see that? Attributes as what
you look like. You see that can I
describe the right? So I plus is like an object constructor or a blueprint for
creating objects. So for us to create a class, really all we need is just
simply to state something like this and say public
class HelloWorld, right? So we already have named by the of all class
years hello world. Usually Java would
do that for you, but there may be scenarios
where maybe you might not, you might want to
do that yourself. But for now it's okay. And then we could even say x is equals to five for example. And, um, yeah, that
could definitely work. And then if we want to maybe create an object that was the initial one was
as creating a class. But this one, we want
to create an object. In order to create an object, you have to bear in
mind that in Java, an object is created
from a class, and we've already created the class named
HelloWorld force, as you can clearly
see over here, right? So now we can use this
to create objects. So to create an object
of Hello World, what we're gonna do
is we need to specify the class name
followed by the name, and then use the keyword new. So an example of wood, this would be the following. Let's say public, static, void. Public static void. Let's try out and
just say main string. Let me just change as quickly. Insert that, come back and ERGs. And right here,
just hello world. My OBJ object, really
abbreviation for objects, new, hello-world and voila. Then we could also
just say, Hey, we want the sprint doubts
or system of our print LN. And then we print out
my OBJ dot x dot x. So whatever the object is
multiplied by x really, right? So what do we come up with? Essentially, we come up
with just five, right? So we get the value
of five over there. And so there could be a
scenario where maybe we want to create multiple
objects of one class. And in order to do that, it's still gonna be
very, very simple. Nothing complicated at all. What we wanna do
is you just really is just we wanted to
use say hello world, and then we now specify
and say my OBJ. Let's say the first one
was religious like one or just the beginning once or specified into two
and just say new. Then we say hello world. And bear in mind
that here we have object number to,
object to that. Here we have object one. Object one, right? Then what we wanna do as well as maybe we could print out
both of them really, why not? You could choose
the same system. Dot out, dot, print. And then my OBJ, the first one. The second one
because we already have the first one or with a. And then we just say by x, and normally we should have
both of them come out. That is 55. So that's that's the beauty
of it right there. That's the beauty
of it right there. Essentially, we're
telling it to print out whatever it
finds in this class, especially the perimeter of X, as we have stated
it right over there. There could be scenarios
where maybe you need to. So yeah, I hope that
definitely helps you out with that
definitely helps you out. So the next thing that we
want to look into is what is called Java's class attributes. Right? Here they are. In Java class attributes, attributes, what they do. And then in English,
it's just something that defines the characteristics
of something, right? In the previous scenario or
less than we were talking about classes and objects. And even earlier on, we even defined what invariable means. So a variable is
actually an attribute of a class that's really
worth noting down. Variable is an
attribute of the class. And so to give an example, Let's say we have a class
called remove all of this. We have a class
here, it's called Hello World for example. Therefore, with this in mind,
Let's create attributes. So it will be x. And x is close to five,
like we had before. We could even create
another one into y is equals to
seven, for example. So these two things, x and y, our two attributes,
that's what they are we have been seeing up until now that
there are variables. Now you're understanding that
they're not just variables, but they are also attributes. That's what they are,
that's what the, and. So another term for class
attributes is fields. So instead of calling
them attributes, you can always just call
them fields as well. In order to access
this attributes, you can access them by
creating an object of the class and by
using the dot syntax. This example that I'm
about to show you now will help you
create an object of our class with the name my OBJ as I've been using
them, all my object. And we use the exact tribute and object to print its value. So just to even help you understand more what
exactly I'm talking about, Let's just call up on public
static, void, main, string. Just have this RGS and
just loosen this memo. And yeah, let's have class HelloWorld
and colon, our OBJ. Remember it doesn't
necessarily have to be all. You can always call
it something else. So we call on a new class now. Then we just print that
out. You print it out. And let's see what we get
out of that beautiful, that is beautiful, beautiful. So that's how we definitely
access them attributes. Then we could also modify
the attributes that we have already defined. An example would be, let's talk about the very
one that I had done here. Let's say that I call on x, just x in the following
minutes instead of five, I just say x and x. I could modify it right here
and just say my object here, and just like that. But my object of x is
equivalent to five. Let me just put it down. So it's equivalent
to five day it is. And then I run my object of x. And I get five. If I change this to a 124, I should get 124
because I modified it. You see, initially
it was just x. And have modified it
right over there. Alright, if it was, for example, fall, we're here. Let's just run it and
just try and understand. It still gets to be a 124. That's because the
initial value it gets is really like we see for. But then it gets to this
stage and says that all the new value of x
that I should input, it should be a 124. Because it always
takes up to Java, always takes the latest
information given to it. That's the one that that overwrites the
existing information. That's, that's, that's really
what you can say about it. So I can just put
the CN comment and say that x is now 124. That's what it is, right? So if you don't want the ability to override
existing values, declare, you can always
declare the attribute as a final example right here. You could say, if you don't want it to change at any stage, you could say a final x. So as you can see now is an
error for me to change this. I'm not allowed to
change it into anything. It then for itself, That's because it's not allowing for me to take any value. Not at all, nor, nor
fresh input at all. I see. So I need to get rid of this and that's when
everything will be okay. And if I were to print this, I should get the value
of just for itself. So Betas that we haven't. Another scenario that you
could possibly find yourself working with is just maybe
working with multiple objects. And hard that happens is if you want to create multiple
objects in one class, you can change the attribute
values in one object without affecting the
attribute values in the other. Example, if you want to change
the value of extra 25 and object number two and leave x in object number
one and changed. You could do something
of the following nature. Let's say that they have this. We don't need the final
one here for now. We have two objects, so let me create
my second object. Second object being new
hello-world and datas. And then my, my second object can be specified in
the following manner. My OBJ dot x, it's 24, meaning that as far as the
face value is concerned, it will just print
out for as far as the second object is concerned,
it will print out 24. So I'm going to print
out both of them just so that we can better see. So copy and paste and just print both of
them so that you can see the difference
between the two. So the first one should be for the second interval.
So there it is. Only the first one was changed. The second one, since
it was modified. It has changed. You see that the first
one remains the same, the second one changes. So that's what happens
if you just want to have multiple objects in operation. You could also have multiple
attributes in operation. So you can specify as many
attributes as you want. And in order for you to do that, really it would involve you doing something
overflowing nature. Let's say I were to
remove all of this. And we're not talking about let me move this as
well because I don't need it. Then let's say that I would say string firstName
F9, given by Tom. Then I need to syntax
as well, strain. This is my last name here. Sheppard from the
game mass effect. So there it is. And then we talk about the age, and let's say he's 26 years old. And so what we're gonna do
is we don't need anymore. Yeah, really, it's
not important. So I could have and create
my own object, right? As you probably have
seen as too many times. Now, what I'll do
is this though. What I'm gonna do is right here, I'm gonna just want us to print out something interesting. We're going to say
just name, a name. And then with the name. We'll also go into just a colon or object
dot F name, right? And then we're going
to also call up on, alright, I give some space between them, not
really collarbone. And then have our second object, which is our last name. L for L name and then
I read delete that. And right here as well. What I'm gonna do is I'm
just going to call on the age here so that
we have the age. And we call that object and
phone to the age right here. And let's run it and
see what we get. When we do run it. There it is. We have named Tom
shepherd, age 260. So this is definitely very
valuable, very valuable, especially when you deal with stacks of code and just
so much information that you need to process and just having it all have it altogether,
have it altogether. This is some valuable
information that you definitely want
to keep in mind. Definitely want to keep in mind. Alright, so the next thing that we're definitely going
to talk about is just called Java
class methods, right? So from you've learned from Java methods
chapter or whatever, the lesson that
we're talking about, that methods are
declared within a class, that they are used to
perform certain actions. So in order for you
to create a method, in order for you to
create a method, you know that you, you definitely want to
have the following. You want to perform the
following actions, right? So let's say that
I want to create a method, call my method. Yeah, let me just
call it my method. So what I can do is I can
just say static, void, my math forward, open that and open it like that. This is method. Yeah. Then write like that. Well, I don't need
this guy anymore. Really. See, let me just drag
this guy all the way up just so that we can
always remember that everything is right there. So we've created an old method, really saw that a system
that out dot print LN. Then we could say, hey, everybody, you want it. And let's see. Right, Okay, sure, there's
a bit of an error here. Main method not found in class
HelloWorld, this happens, please define main
method public, static, void, main
method, right? So essentially, yeah, It tells you that there's
definitely a problem over here. So what we're gonna do is we're
just going to say public, static, void, main. And we call on this
train and just change our thing here and
create those quotas and ARDS. And right here, we call
on our method, my method. Now, let's run it and see
what happens when we run it. Beautiful, that is
myMethod, myMethod, right? So the error has been
fixed and this is definitely how we
deal with methyl. So whenever you call a
method recruiting methods, you want to put them in
their public static void and then run it in whatever
method that is. It will show it will definitely
function to your liking. That begs the question as to understanding the whole
thing is here itself. The public obviously
means that it's accessible to everybody, um, can be modified and changed in hardcover
ways necessarily. But then static itself
is a bit interesting. What does static mean? Well, static for us to come on to the topic of something being static or
not being static, we need to really understand
what's going on, right? So static versus non-static, that's what we're
focusing on now.
19. Static Vs Non-Static: Alright, so we move on to the static and non-static
natures of our Java programs. And you often see that
in Java programs, we could have our geography. Java programs be either static or public in their attributes
and methods, right? So in the previous example
that we did over here, we created a static
method, right? As you can see, there's
a static method. This meant that it could only be accessed without creating
an object of the class, unlike the public,
which can only be accessed by objects, right? So let's have a look at the following example just to
demonstrate the differences between static and
public methods, right? So let me just
delete all of this. We don't need it anymore. At least this part of it all the way to our public class
over there, right? So right after public class, we're just going to
name this and put it as a comment and stay
static method. And our static method will be
the following type will say static void and call
it my static method. It's going to be much
better like that. A bit of a typo over there. And then we just make sure
that we have this in brackets. And we open it up like that. Then we'll say Just system
dot out, dot print LN. And all we have to prove right up print here is
just to say something like static methods
can be called, can be called without
creating objects. Without creating objects. Just like that, right? And then, on the other hand, are what we're going to do as well is that we're going to also just showed a difference
here with a public method. I will put that as
a, as a comment. And with our public
method will just say public void, my public method. And just have this
open up like that. And we just say system
out, dot print LN. And then we just mentioned that public methods must be called by creating objects. So we need objects
with public methods. That's essentially the difference
between the two, right? That's essentially the
difference between the two. Then what I'm gonna do is I'm
just going to call this my, my main method if
you want, right? This is my main method. The main one, right? So I'll just say something
like public, static, void. Mean, as we always say. And then a right
in our thing here, we're just going
to call a string. And as you know, I need to get it over
there because I don't inherently have this
ERG S over there. And just come down here. And we just call this my
static method, right? And essentially by doing
this, what are we doing? We're calling the static method. That's what's actually
happening here. So here it is with caudal
are static method. Then if we, if we were to
maybe call our public method, let's see what would happen. Public method. So there is a public
method over there. There's a bit of an
error over there. Why is that? There is an arrow over there? Because the way that we need to call a public method
is we need to call. We need to create an object, first of all, in order
for us to access that. You see that to access
the public methods. So this is clearly
an error here. Let's call it, let's just say, let's compile an arrow. Those compiles and error, meaning that with this, what we experience is an error. But then let's call
him on main class, which is called HelloWorld. And just say my object. Then we just say new
hello-world and we close that. And then we call it
my object again. And then with this time we just said dot my public method. It's already being, you could
say dumb, false, right? That. And as you can see,
there's no error. And essentially what we're
doing here is that we're creating an object of An object of Hello World, That's a main class, right? And then right here, essentially what we're
doing is that we're calling the public method on the object we just created. So if we were to run this, let's just see what
we essentially get. Normally, as you can see,
there's no error at all. Then we should have the first one showing
static methods can be called without
creating objects. Voila. Then we also have public methods must be called by
creating objects. As we can see, we had to create an object to ride day, right? So essentially that's the difference between
the two, right? And just to further
elaborate this more, we could even talk about how we access methods within
an object, right? So let's say you are to create, let me just put the downforce access methods with an object. So let's use it to create a car. A car object named my car. You could just call on MF lot of throttle of a full
throttle and over speed right on the my car. Object and then just
simply run the program. And if that doesn't make sense, let's, let's pick
an example of that. So let me just
remove all of this. We don't we don't need
any of this for now. Probably just this
could say this has me always go back into
my second keyboard. But then for now, let's just do this. Let's just create full throttle. Create a full throttle bit
of a typo where there is food thrown with a
big T over there. And we're creating a full throttle method
over here, right? So essentially that's it. So we'll just say public, void, foo, throttle. And we have this, close it up like that. Then within this method, all this method does
is that it prints out system dot out, dot print LN, it prints out
the following statement. The car is going
as fast as it can. Yeah, because going
as fast as it can. Alright. Then what we wanna do
next is we want to create a speed method as well. Alright? And add a parameter. So we'll just call it
public void speed. The initial one most public
void, full throttle. This one uses public void speed. And then right in this bit here, which is going to
put our parameter and call it max speed. That's the name of
our integer value that should be in there. It should represent
the maximum speed. So we'll just say
system out, print LN. And right in there we
will call the max speed. And max speed is, and right outside there
will call on max speed. This is the integer value
that we want, right? Then next thing we wanna
do is we want to just, um, that is inside, of course, lower caps. Inside Hello World. What we wanna do is call the methods on my car object, right? So there we have it will say
public, static, void now. And then we have all string here with a RGS here and
there we have it. And we'll just call Hello World. Hello World, and just
say my car for example. And then we just say new
and Hello world again. So essentially here as you know, we're just creating
my car object. That's what's going on.
So the object exists. Now, if we wanted to
go public on this, we could definitely
make use of the object. So we just link this to
our method of throttle. So essentially therefore what we're doing is
that we're calling the full throttle method. Then we just say again, my car, and then we allocate
or speed at maybe 25. So by shadowing, we're
calling the speed math PFAD. That's what's going on here. Right? Then. You just want to make sure
there's no space over here. It should just be
public static void. There should be a main
here that I left out. Main. Yeah, good. And we run this and should tell us
that the car is going as fast as it can firstly and then tell us the
max speed as well. So there it is. The car is going as fast
as it can that we have it. And the mx b is 25, right? So just to elaborate what's
going on further here. Firstly, we created a
custom hello world-class. That's what's going on
with the class keyword. And then secondly, we created the full throttle method here. Not just this full throttle, but also the speed method. Except in the speed method, we had a parameter
which is an integer, is an integer, whatever that is inside of
this as a parameter. But our integer here
or at our parameter is an integer and it's called
a max speed parameter. Then we simply said in order
for us to use our mean, our, our Hello class,
HelloWorld class. And it's my fault. We
need to create an object of the hello world class, which is exactly what we did. We created an object
over here, right? Then the next thing
that we did was we said we created an object obviously using the new
keyword over there. And then we call on
the full throttle and speed methods on the mica
object and run the program. And name object
followed by the dot, followed by the
name of the method that we're interested in. And it's just worth
noting that we can add an int parameter of even if 200 inside the
speed method, right.
20. Constructors: So just remember, something
worth noting is just that. Remember that the dot is used to access the objects, objects, attributes,
and methods. So it's just worth
noting that because that comes in very
handy as you progress, especially feather
in the whole aspect of what we're still doing
at the moment, right? Awesome. So the next thing
that I want us to look into is what is called constructors. Constructors. And this is something that
will come in very handy in as you advance
further and further. So, just bear in mind that
a constructor itself. Constructor is a special method, a special method that is used to initialize objects
as simple as that. Viola. So simply, we call the constructor when
an object of a class is created and it can
be used to set initial values for
object attributes. Alright? So an example of this would
probably be something like, let's come right up here and see if I can give you
an example of this. So right now we're talking
about a constructor. So let's say that we
have something like int x being just like that. In essence, we are creating
a class attribute. Just make sure this is a comment created the class tree Butte. We have created a class
attribute over there. Then next thing we do is we
create a class constructor. Now, main focus of what
we're doing right now. For our class, which is
the HelloWorld class. Awesome. So we just say public. And then we say hello world. And then we'll just do this
and close that light that. And yeah, let's just make sure it's
opened up like that rather. And here we just name our attribute and give
it a seven, for example. Upon giving it a seven, what we next do is that we
just have our common method, public void, main, string, or NGS. And we create an object essentially which is
going to be hello world. And just send my OBJ. And then we say new hello
world and we close that. That means what have we done? We have created an
object of our class, which will be used to call
a constructor, right? So we have created
an object that will call the struct. Does the object
of our cluster BU definitely call a constructor? So right in here, what I'm gonna do is I'm
just going to set my, then we have x over the, essentially we are printing the value, value of x. So let's just see if we can
get a seven over there. That is perfect. We have our seven. So it's just worth noting that the constructor name must
match the class name and it cannot have a return type like
int void for example. And also note that the
constructor is called when the object is created and all classes have
constructed by default. And if you do not create a
class constructor yourself, Java creates one for you. However, then you're
not able to set initial values for object
attributes, right? So another thing that's
worth us looking definitely into is what is called
as constructor. Constructor param. So a constructor parameter. Constructor parameters,
essentially they can, constructors themselves can take parameters which used to any
lie attributes. Alright, so here's
an example that we can definitely use as an application of what we
are talking about here. Let's just console everything here that we have done so far. Rather up until at
least some puzzle this, we say x is equals to y. And then we have
not defined y yet. But rather what we're going
to do is that we're going to define it right there
and call it int y. And then right in here as well. What we're gonna do is we're
just going to say object. And then right in here, we're going to put a number corresponding to the value of y, because that's how
we mentioned that hello world has a parameter. It's an integer of y. And whenever I see Hello World, I need to make sure
that I have it as an integer y whenever
I see that as a class. So what I'm gonna do is I'm
just going to say my end. And then x and I run it
normally should be eight, because x is equals to y. So there it is,
just eight, right? Just ate and you can have as
many parameters as you want. So for example, here it could
be an integer called year. For example, just
an example really. And in which, for example would have to
definitely modify this. Let's say this was
like model, yeah, in terms of maybe we're
talking about costs, maybe. So this would be like
your model here. And then below it you probably want to look
into the string. I don't need this
comment for now. And then have a string
that would tell us what the name of
this car model name. And then because
we have them like that will suggest an
integer here would be the air stream just
calling on the name. Yeah. Then essentially
what we're gonna do is we're just
going to give them model name would be would essentially just be equivalent to somebody
asked him for the name. And then model would just
be tantamount to me. Just say what is the yeah. Associated with the car
we're talking about. And then I'll just say in here, main, here are the
Hello World. Here. We'll just definitely
give it the name First. Let's call it a Ford example. Then on this ford, rather, we're going to
start with the year first. So it's gonna be 1967. And we put the name
would be fought, for example, 1967 board. And it's just a matter of making sure that
we printed well now. So how will we print this out? Well, it's no longer
just an object, so I'll just call
it my car here. I call this my car dot. Then it's just a matter of us choosing what
exactly do we want. So we have it in the
following manner. Then we call on the not just the year but
the name as well modal name. So it should say 1967, Ford as well. So there it is. 1967. Ford does the output
that we get, right? So essentially that's
about it when it comes to construct this. That's the bothered
when it comes to constructors in deed, right?
21. Modifiers: Alright guys, so
the next thing that I definitely want
us to look into is what is called
modifiers, right? So let me just put that here
because there's a bit of some commentary that's going to be taking place where here. So we call them Phi is, right. And they minded modifiers, something that we've
been using already. Bear in mind that for example, whenever we say public
class HelloWorld, public is a modifier, right? So we types or other
groups of modifiers. Modifiers can be named in terms of or rather grouped in terms of the access modifiers, which is an example
of our public. We can have access
modifiers or we can have none access modifiers. Alright? So what are access modifiers? Access modifiers themselves are inherently those that control access just like the name says, Really access to whatever
coding that you have, right? And then this one is do not. They do not control access. But they provide pro vide. Other functionality. Yeah, awesome. So those are the non
access modifiers. Public, for example, public, as we've been talking about, public is an example
of an access modifier. It. And just with this
that I'm about to show you because it's a bit
of some commentary that would definitely
going to do over here, just so that you can thoroughly understand exactly
what we're doing here. And I'll give an example
starting off with the public. So essentially what public is, is that public is allowed those or rather what
it means is that we're talking about a class that is accessible by other classes. It's a class that are accessible by another glass if you want. Then we could also talk about a modifier called
default modifier. All this are access
modifiers, by the way, should probably just
make them clear that these are access modifiers. We also have default
and default. What it means is that
the class is only accessible by classes
in the same package. Class. Simple Bye classes
in the same package. Now, if you're wondering
what are same package as we could be talking
about this for example, this is a package that
we're talking about. So we could have
multiple packages and but it's a container
containing classes really. You can think of
it more like that. And we use this when we
don't specify a modifier. So it means this is used. Java automatically uses this. This is used when you don't specify by modifier, it's the default one. If you wanted to, could
definitely say that. Yeah. Alright, and then
four attributes, methods and constructors
you can use. Any of this really could
use, not just public. And as we know that P
has got to be small t. And then four attributes, attributes, methods, and constructors. We could have the following. We could still use public. Here. This would mean
that our code is accessible for all clusters. Then if it's private. If it's private, it's
a different thing. It's only code is
only accessible. We then declared class,
so it's private. And then if it's default, it means that we're
talking about. Same package. Soybeans is still the same thing as we already
mentioned over here. So I'll just do this to refer or autotrophs show
that it's same thing, but rather here just
for same package code as accessible on the
same package basis. Then we could also
have protected. And when a code or whatever you want to modify is protected, it means that code is accessible in the
same package as well. And sub classes. This will see much further
and helps very much, especially when we start
talking about inheritance, superclasses and subclasses, it's quite valuable
that you know that. Yeah. Right. So the next thing that I want
us to look into is what we call the non access modifiers. Yeah. With a non-access modifier is let me just put
them down for us. We are talking about
Nan access modifiers. And with the non access
modifiers, right? Really, you can either
use final or abstract. That's the, that's the k
is at least four classes. Classes. You can only use Final
it all up struct. And if we're talking about
something being final, what we're saying is
that the class can not, the class cannot be inherited, Can not be inherited
by the process. So again, this comes
in very handy, or rather you understand
it further as we do inheritance,
which we're yet to do. This is just me.
Building up to that. I'm here with abstracting
means that the class can not be used to create objects. So in order for you to
access an abstract class, it must be inherited
from another class. Alright? So for, this was the
case for classes. But then if we're talking
about attributes and methods, then when we're referring to something being
final, it means what? It means that that
thing or that attribute or that method cannot
be overridden. Overwritten. Show you still remember the
example that we did above. This cannot be over written
one more hour or are there some people would
even some modified. Then if it's static, we also have static. If something is static of attributes and
methods are static, it means that our attributes and methods belong to the class. Dan, Rather than an object. Belong to. They belong to the class. Rather than object. Yeah. And if it is abstract, it means what it means
that it can only be used, can only be used in
an abstract class. And can only N can only be used on methods, not on anything else. So just bear in mind that the method does not have embody. For example, something like
abstract void run so that the body itself is provided by the subclass inherited
from, right. So it's just some
extra information that you need to bear
in mind is suppose. We could also talk about how
there is also transient, which isn't very common to use, but it still exists. So it means what
attributes and methods are skipped when serializing
the object containing them? Send it skipped. Using the object
contained in them. Then we also have synchronized. So what does synchronized
essentially mean? When something is synchronized, essentially what we're saying is that I think I left
something over here. Sin colonized. Yeah. That's the right spelling. Synchronized. It means that our
methods can only be accessed by one thread
at a time, right? So it's important that
I do state that here, this is a method I'm
only referring to. Methods can only be accessed by one thread at a time. Then if it's volatility or
rather volatile, if you want. If I use volatility means that the value of
an attribute is not couched thread locally and this is always read from
the main memory, right? So again, definitely not something that you'd be seeing a lot of times. But if costs, programming
is so big with Java, sorry, it will ultimately depend on what it is that
you are quoting. This awesome concepts
that will progressively find ourselves doing locally and is always read from main memory. This itself was no
t here on Monday. Yeah, So this are the axis n none access modifiers that you definitely do
want to keep in mind. And just now we'll
just do some examples, just exploring
this much further. So an example of this would
be something like this. Let me just delete all of this. We don't need this anymore. So it say final. There's a modifier in x on an attribute here
and give it maybe 19. And then we say final double
Pi and Pi itself as we know, it's given by 3.14. Yeah, 3.14 is actually 3.1428. So let me just to aid and progressively,
and so on it goes. So we're stating our
modifiers is being finite. That means that they cannot
be overridden or modified. We cannot lead on defining
new value of X or the new value of pi, right? And then here comes on method. We say public, static, void, main, and
then this string. And we just make sure
that we have alright, keyboard over their RGS and
we ingest open that up. And right in there
we create an object. So we say hello world. And then my J is equals to new hello-world
name of my class. I just come down and then as that and start exploring
something else. So let's start like this. Let's just say my OBJ
and then I say x. And then let's see what we get from this is
gonna be interesting. Things in error. They, why is then error? Well, as we can see, error. Alright, why? Because it can not
assign value of x, y because we declared
it as final. It meant that it cannot be modified or changed
in any manner. Even if we were to try the pie and just try to modify
our Pi as well. A similar thing would definitely happened with five were to say, Oh, new value of pi is
just 3.14, for example. Nothing wrong with that,
but they still an error. Why is then error is
an error because we're modifying the value that was
declared as being final. That should not be changing. Cannot assign a
value of Pi sine, alright, Yeah,
Yeah, Yeah, right. So let me just try and print
out and just say, Hey, system, dot out, dot print. And then we print
out x for example. And we see what will happen. So if we print out x, just one here, remember we
printed out with object. So there is my object. Let's see what, what we get. Now. Obviously this one, we're going to put
it in a comment because we know that
there's an error there. We don't need that.
And we run it. And let's just see. Taking a bit off some
time here to load. Still loading. That is maintain does because we
define x as 19, object is x. And if I were to change
this for example, and just make this all pi, then let's see what
comes of this. Now. Let's see if it gives us 3.148. That is 3.1428, right? So that's an example of us
using the final modifier. Final modifier, which we know to be in non-access modifier. Now let's use another
non-access modifier, which is a static modifier. So or are they understand
it much further, right? So with that one, It's
a metal of us just understanding even every time when we have something
called static. Static method means that it can be accessed without creating
an object of a class. Unlike public where we
already spoke about this one, we're talking about
static versus non-static. Wait. Just to put this in perspective, let me just remove
everything here. I'll probably just give this guy here because I know that I
don't have one keyboard. And just create a static method. I say just static, void, my static method. And it's definitely open. And just call it a
system out, dot, print and write in there. Probably write
something like static. Methods can be called
without creating objects. Yeah.
22. Access & Non-Access Modifiers : Then just look at public method and look
into a public void. Just create something like public void and then just
call it my public void. Now, public method, that is rather here, let's print out something
like system that are pruned. So say public methods
must be cold. By creating objects. Then we have a lot. Some people would
say main method, but we'll just call it
our HelloWorld method. And this one will just
be something like this public, static, void main. And we'll talk about
this train G S over and just go down and just
call on our static method. Just make sure I have those
curly braces in there. Yeah, my static method, essentially I'm calling
this static method colin, the static method. By so doing. Then, if I
look at my public method, if I were to try and call
this, let's just see. This already is an indication that something was
wrong as it's not been suggested to me,
that there's an error. Why? Because I need to create an object in order for me to start using this right. Here is an error here. So I need to put this as a comment and just create my Hello World object. Hello. Alright, I think it's
time for a change. Now. My o, for now. I've been calling my OBJ for too much, for
too long or other. And hello world and my OH, and this colored man public method that has
been suggested for me. And essentially I'm
calling the method now. If I run this, normally
it should work. Now I should be able to print out the public
method as well, as well as the static one. So I should see both
statements smoke just one of them should see static methods can be called without
creating objects. And I should be able to see public methods must be called by
creating objects as well. Let's see. It's loading as well. There it is. We have both of
them. Static methods can be called without
creating objects. And we have public
methods can be called a must be called
by creating objects. Yeah, beautiful. So there we have it. There we have it. Then the last modified
that we'll look into it is what we call the
abstract modifier. As we know, the
abstract modifier belongs as well to the
non-access modifier as well. Yeah. And it can work for classes
and four attributes, right? So abstract. I'm just going to just
remove all of this again. And we just make sure that
we have everything in place. Okay, beautiful. Alright. So instead of
calling this public, we'll just call it
abstract obstruct. There's abstract class. And with Officer plus
we will just call on, I don't need this
static method anymore. And then I'll just
say something like maybe public String fname, which is for my firstName, and call it Will. And public end. Age as well, which
is 24 years old. And then there is public abstract void as well. Study. It means that this
is an abstract method. Yeah. This is an abstract method. Yeah, definitely. And we just make sure that
this is closed out like this. Definitely like that. Good. Then just below it, what we'll do is we'll have
something like a subclass, which really we inherit
from Hello World. And with all subclass
will just say clusters, meaning that its
default if you want, if we were to leave
it as just the cost. So if they assaults
student big S and we say extends
inheriting now, hello world. And we open that up. Possibilities are endless. Now this so we have public and Close click Command. Alright, so the next thing that I want us to look
into is what is called as Java encapsulation. So let's just have
that over here. This is Java encapsulation. So what Java encapsulation means is for us to really
understand what it means really is that
we need to understand what encapsulation
encapsulation is. It means to to make sure that sensitive data is
hidden from users. And in order for us
to achieve this, we must one, declare
class variables. Slash attributes, of course, actually beats as private. Secondly, provide,
we want to make sure that we provide Public get and set methods axis and the value of private variable. Yeah, so this is something
very important as well. So you, we already did it
that private variables can only be accessed within the same class and outside
class has no access to it. However, we know that it
is possible to access them if we provide public
get and set methods, right? So they GET method returns the variable value and the
set method sets the variable. So the syntax for
both is that they start with either
get all set followed by the name of the variable with the first letter in upper case. An example of this would be, let's say public class. As they were suggesting
already here. Hello world. Awesome. And then write in there, what we wanna do is we
want to put something like a private string and
then call it a name. And just make sure
we have that closed. So Ben, mind that
when it's private, private means restricted access. So for security reasons, we definitely do want to
have such things in place. We don't want everybody having
access to everything in your Code as they
can definitely hack your files and just get a lot
of information out of you. Then we have getName. And we open that. And we say return name. Return name. Then that's a getter. Now we have our setae as well. And with all setup, we'll just say
public void setName. And just call on this
training for new name. And have this open and just
say this name as equivalent. New name. Excellent. Excellent. So if we run this, let's just see what we get.
23. Class Project: Alright, so thank you
guys for joining me for this course and for
your class project. What I want you to
do is I want you to, with all the
trivalent right now, I will attach a file here. I want you to go
right into that file. I want to read through that. And there are some exercises that I've attached
as well that I would like you to go through
and just submit for me. Alright, cheers, and I look forward to seeing you
in the cost dashpot.