This video is for Windows.
If you're using a mac computer, please go to the previous lesson.
So far.
You installed Visual Studio Code, a text editor where you can write code and you installed Java inside
your computer.
So now you can run Java code.
In this lesson.
That's exactly what you're going to do.
You will write and run your first Java code.
Step one is to create a folder.
Simple enough inside of your desktop.
Make a new folder.
And call the folder section.
One.
This folder is going to contain your Java file.
Step two is to launch the folder from Visual Studio Code.
Here I have Visual Studio code.
Open.
Go to file open folder.
Find the section one folder that you just created.
Mine is inside of desktop and here select the folder section one.
Once you select it, press select folder in order to launch it.
Step three is to create a Java file.
When creating your Java file, you must name it using the camel case format where the first letter of
every word is capitalized and the words themselves are joined together.
And your file.
For it to be considered a Java file, it needs to end with the extension Java.
Let's do just that.
I'm going to create a new file by pressing the plus icon, and the file is going to be called Hello
Java again using Camel case, where the first letter of every word is capitalized, followed by the
DOT Java extension.
And here we've created a hello Java file.
Beautiful.
Step four is to create a Java class inside the Java file.
In Java.
You will write all of your code in a class.
And there are two parts to a class the class keyword.
Followed by the class name.
Inside brackets is where you would write your code.
And furthermore, the class name needs to always match the file name that it's in.
So if the file name is called Hello, Java and Java, the corresponding class is Hello, Java.
Inside your Java file, you can create your Java class.
So start with the class keyword.
Followed by the class name itself.
Hello, Java.
And inside brackets is where you're going to write all of your code.
Perfect.
We made a lot of progress.
Let us delete all of our progress.
We're going to delete our Java file.
But wait a second.
Ran.
We made all of this progress, and now we're just going to delete it.
Just trust me.
So go here, delete your file.
Because we already created a Java file.
Now these code knows that this workspace is meant for Java development.
So the second time we create a Java file, remember to use camel case.
Now VS code is ready and it's going to auto generate the class for you by default.
It adds a public keyword which makes our Hello Java class publicly accessible to other classes that
may exist inside of this workspace.
But there are no other classes in this workspace, so public is not really doing anything.
You can keep it, you can remove it.
It doesn't really matter.
It's up to you.
I'm just going to keep it.
Step five Your class needs a main method.
Maine is the entry point of a Java application.
This is where Java starts running your code when you run your application.
Java starts by looking for the main method so inside of your class.
Press enter after the first bracket to get proper indentation and then right main.
Choose the first option.
And by clicking this option, the code automatically writes the main method for you.
Now, this might look like a lot of code and it might look a bit scary, but rest assured, the main
method always looks like this.
All you need to know is that this is the entry point.
This is where Java starts running your code.
Inside of Main, we're going to print a message and you can do this using the print line function.
So system.
Dot out dot print line.
Instead of double quotes, I'm going to write.
Hello, Java.
Before we do anything else, let's break this line of code down.
Print line is a function that prints a message to the console.
In parentheses is where you place the message that you want to print.
And the semi-colon is very important.
The semicolon means end of statement.
Every statement in Java, every line of code needs a semicolon at the end.
If you forget your semicolon.
Java is going to throw a big red error.
If you hover on the error, it tells you that you forgot a semicolon, so you need to insert it.
On the topic of errors.
If you misspell the function, you're going to get an error.
Java has no idea what function this is.
It has no idea what you're talking about.
And last but not least, code in any programming language is sensitive.
If you capitalize a letter that shouldn't be capitalized, you're also going to get an error.
Java does not recognize the print capital T function, so please be careful.
Your syntax needs to always be exact.
It needs to always be perfect.
And you'll remember that the class name needs to always match the file name.
So if I were to misspell the class by capitalizing a random letter that I shouldn't here, Java tells
us that this class name does not match the file name that it's in, so we need to fix it.
So please make sure that your syntax is always exact.
Step six is to turn on autosave.
The white dot means that you have unsaved changes.
So what you would do is go to file.
And then press save.
Now your file is saved.
And if I were to change the string, for example, if I were to add a bunch of exclamation marks.
The white dot reappears.
And now I have to save my code again.
Now, this can be a bit annoying, so just turn on autosave.
And now every time I make changes, the application saves by itself.
The final step is to run your application.
So once you're certain that your code is good, there are no syntax errors, there are no red underlines.
You can execute it the run button.
Execute your code.
And there you go.
It prints out our message.
When you execute your code, Java looks for the entry point, it looks for main tries to find it, and
once it does, it's going to run the code inside.
The first line of code inside main prints.
Hello Java.
Then it looks for more code inside main, but there isn't any, so it reaches the end.
If you have any syntax errors, your code is not going to run.
Here.
It's telling me that I have an error inside of line number four and I need to fix it.
If I rerun my code now.
It works beautifully.
And now I can keep running my code to my heart's content.
All right.
With that being said, congratulations.
You wrote and ran your first Java code.
In this lesson, you learn that all of your code must exist in a Java class.
You created a class called Hello.
Java Main is the entry point.
This is where Java starts running your code.