(piano music)
-: So let's solve this problem.
So we are not able to run this code, right?
This is not working.
We want to get hello world on the screen.
It was working with J Shell
because JShell says you just a parse a statement,
I will work.
But then when you build an application,
when you run the application,
how exactly it works behind the scene.
Once we understand that, we can write this code.
So in your machine you will be having something
called JVM.
Now by default, when you say Java is platform independent is
because of JVM, because in your machine you will be having
JVM irrespective of which OS you use.
It can be Windows, Linux based OS, or Mac
or whatever famous OS we have.
It will have JVM installed.
And of course if you have JVM, you can run Java application
and if you don't have JVM, you can't run Java application.
Okay, so we got JVM here, right, now what JVM says,
hey, I will work.
But then this one question, when you say JVM will work,
but JVM works on which environment?
So underlying we have your OS.
So this is your OSS
and then your OS is working on a hardware, right?
So you'll be having an hardware, it can be a laptop,
it can be a desktop, it can be a phone, doesn't matter,
you have an hardware, on top of that we have an OS.
On top of that we have something called JVM.
Now, JVM says, okay, my job is to execute a Java code, okay.
So whatever java code you create, JVM will execute that.
But then JVM will not accept the code directly.
See what happens is Java goes in multiple steps in Java
application, when you as a programmer,
so let's say you are a programmer here
and when you write a code,
you will not directly run write a code which will
run on JVM, okay?
And we don't even do that, right?
We don't even write a code which directly works on the
hardware and we don't even write a code which works
directly on OS.
Java says if you want to make it work, you have
to run your code on JVM,
but then JVM will not understand what you are talking about.
And one more, one more thing we have talked about, right?
Java is platform independent.
So what does platform independence means?
It simply means that you can run your application
on any machine.
So when you write a Java code,
it will run on any machine irrespective
of the hardware or the OS.
But the only thing it requires is
that machine need to have JVM.
So irrespective of which platform I'm talking about,
if it has JVM, your application becomes
platform independent.
But JVM itself is platform dependent, okay?
So example, you can't run JVM on iOS.
So we can't say Java is platform independent when JVM itself
is dependent.
So yes, your application is independent, JVM is not.
So JVM need to be built for a particular OS.
And let's say if it is windows in my,
so in my machine it is Mac,
but let's say if it is Windows,
we have JVM available on Windows.
So your JVM says, Hey, I will run your code
but you have to submit me, not the Java code,
but something called a byte code.
So you have to convert your code into a unreadable format
for a normal human being.
And this is called a byte code
because JVM only understands byte code.
But can you write byte code?
Of course not.
You only understand English type of languages, right?
So whatever we have return the code,
we have a return system dot out dot print,
it's English type language, right?
So what you create is a Java code.
So this is your Java file or Java code,
and then you have to convert this Java code.
So this is Java code, which is more of readable.
This is not something which you can read.
So basically you have to convert this into a Java
code or byte code.
So for that we have something in between here,
which is called a compiler.
Now since this is a Java compiler, we can call it
as Java C, which is Java compiler.
So basically you will write this code.
So this code will be getting compiled into a byte code
and that byte code goes on the JVM, it'll run.
Makes sense, right?
So that means when you write something it will
go into byte code
and then byte code will run on JVM.
Now the only thing is see your project can be a big project,
right, here I'm writing only one file.
It can be 10 lines of code.
It can ,it can be 10 files, it can be a hundred files,
it can be thousand files.
Out of all these files, there will be only one file
with JVM will execute, okay?
JVM will start with the first file
and then of course that file will depend upon
other files and it will execute.
So JVM says you need to tell me what that file is, okay?
So if you, even if you have thousand files which you want
to run in your project, you have to give the first file.
So in this case, what you will do is you will specify, hey,
that's my first file.
So when you run the code, you will specify the first file
and also that file need
to have something called a main method, okay?
So as I, as I mentioned before, right?
We have print in the same way we have main, so your file,
the first file need to have a main method.
So when I say first file, it is not like you have
to follow a sequence, create this file, then that,
that file, out of all the files in your project,
the execution will start from the first file which you
specify, okay?
And that file need to have mean, not just mean,
it is looking for a specific signature.
Okay, so it has a signature, it means the entire method
signature, the method name,
what are the values you are parsing and all this stuff.
And of course all this term you will, you will get used
to in some of these sessions.
Don't worry.
So first you have to make sure
that you provide a mean and mean has a particular signature.
Again, what each word means, that we'll see later.
So the signature is this, it is public static wide mean.
So you have to remember, always parse this method.
I know there are four words,
but then this is the thing you have to remember
and mean will accept some arguments, some parameters.
So you have to parse string A.
Now again, we'll discuss this, what this string is,
what is public static void.
We'll discuss that in some time,
but mean is looking for the signature.
So your code will not work without the signature.
So remember this, so that makes sense.
What are the steps you have to follow?
So the first step is
as a programmer you will be creating a Java file, okay?
Now once your java file is ready, you will put
that into compiler, then compiler will compile it.
Then that code becomes a byte file, a byte code basically.
Then you will take this byte code, you will run that on JVM.
Now, once you, once your JVM says, okay,
I'm running the code, JVM will look
for this particular syntax.
It should be there in your code
and then you will get the output, right?
Simple stuff, okay.
Now there's a space in
between here if you can see, this is a space I have left
that space intentionally
because we'll add certain things there.
So at this point, just remember there's a space here,
there's a box meaning here.
We'll add that box in some time.
Now once we have understood all those things, it's time
to expand this code.
Now if you remember, we have
to write everything in a main, right?
So this section should be a part of,
remember what J will ask you for,
J will ask you for mean.
So we have to put that into mean.
So why the syntax for mean, it's very simple, it is public,
static wide mean.
We have seen that, of course these words will define
that words in the upcoming session.
And once you understand all these terms,
you are java programmer, right?
And then this will say string A and that's it.
This is a syntax you have to follow.
Now there's one more thing.
In Java, now Java is object oriented.
Now what is object oriented means we'll discuss that later.
But in simple terms,
object oriented means everything should be an object.
And to create object we need to class, okay?
Again, we'll discuss all those
things in the upcoming session.
But a class is missing here.
In fact, if you compile this code, this is what your
Java say will tell you.
Java say says, Hey, where is your class?
Your class is missing.
Okay, let's do that.
So let's put that a class here and we'll give a class name.
At this point we'll keep the class name and file them same.
And we'll say, open bracket and close,
and will give an indentation.
So basically we have to follow this
thing, always give a tab.
So every time you open a (indiscernible),
give a tab, it looks good, okay?
There's no compulsion, it looks good
and we love to build stuff which also runs and looks good.
Okay, this looks good, right?
Ignore this X and these suggestions,
these are the suggestions coming from your VS code.
But you know that part in fact is it
because happening of this extension,
let me just remove this.
So let's not use that
extension because it will give you a suggestion.
It's good for development but not for teaching purpose.
So it adds extra text there.
Okay, cool.
So this is how you write your code, okay?
We got a class, a file name,
and then public static void mean, in bracket
you have to mention the parameters.
Again, we'll discuss that later.
And the only thing,
important statement here is system.out.printHelloWorld.
Okay, now let's see if this works.
Let's create the screen and compile.
You can see there's no error.
But the moment I compiled,
if you can see on the left hand side,
we got this extra file.
We got hello.class.
Now this is your byte code.
Remember we have talked about a byte code here.
This is a file which got created automatically.
So when you create a Java file, when you send it
for the compilation, it will create the byte code,
and the extension for the byte code is dot class file.
The extension for java code is dot java file.
And now it is compiled done.
Right, now it's time to run your code.
So remember when I mentioned to run, you have to use JVM,
and for that you have to use a command, which is Java,
and you have to mention the class name, not a file name
this time, just a class name.
The class name is hello.
And say enter.
You can see we got hello world.
So yeah, that's how it works.
So simple stuff, that's how we can able to complete our code
and we are able to run this code.
Now, if you have done until now, give round of applause
or a pat on your back because you deserve it.
So this makes sense, right?
So there's a gap here, right?
So let's remove this gap.
So what we have here, so basically, you know when you,
when you run your code, basically think about your cooking
something in a kitchen, right?
So where do we cook?
Of course in a kitchen, right?
Why not in a hall or in a bedroom?
It's because in the kitchen to cook something,
you need extra things, right?
Example, if you make a bouillon,
I don't know if you know the recipe,
I don't know the recipe,
but if you make a bouillon,
you have to add extra ingredients.
Now those things are kept in the shelf.
So in the kitchen you'll not just have
that resources which are there in the platform,
but also you'll be having extra stuff.
Now, the same way, if you want to run your Java application,
you need extra libraries and you also need a runtime.
The runtime is something which,
where you can run things, right,
now you can run something in a JVM.
So that is part of run.
But then what if you need some extra libraries?
You have to, you need, you need a environment as well.
So we have one more layer here, which is about your OS,
which is called JRE.
So when you want to run something, you use JRE to run it.
Now JRE stands for Java Runtime Environment.
And what is your JVMJ?
JVM stands for Java Virtual Machine.
Okay, so basically we have two things.
We have a JVM and we have a JRE.
Now JVM is a part of JRE.
Now what else you'll be having inside your JVM,
you will be having some libraries.
So JVM with libraries is a part of JRE.
So whenever you run something, it runs inside the JRE
and JVM is actually responsible to run it.
Okay, and by default, when you, when you want
to run your application on any platform, it need
to have JRE, which will have JVM.
Now not every machine will have Java C.
Java C is only for development purpose, right?
So we can imagine this in this section.
So basically we have an outer box, which is your JDK,
which is used by developers.
Inside, which you'll be having something called JRE.
And in this you'll be having JVM.
So as a developer you'll be installing JDK.
JDK will have JRE.
JRE will have JVM.
But to run something on any other machine,
let's say if you have made the application, you want
to run this on your friends machine.
Your friends machine will only have JRE
and JVM.
JDK is not required.
And that's why we say Java is vora, which is write once
and run anywhere, right?
So you just have write on your machine,
you can run it on any other machine, provided you have JRE
and JVM installed.
So that's how your Java works behind the scene.
I hope it makes sense now
and we'll write some more code in the upcoming videos.