Think of creating a file just like an Excel or Word document. An Excel file uses .xls or .xlsx, a Word file uses .doc or .docx, and images use .jpg or .png. In the same way, you create a file with the .java extension—that’s where you write your Java source code. When you run the program, the Java compiler first checks your code for syntax errors. If everything is correct, it converts your .java file into bytecode and produces a .class file. Finally, the Java Virtual Machine (JVM) reads that bytecode and runs your program on the end user’s machine.

First, create a file with the.java extension. Open the file and add below code.

Then, open the terminal and go to the folder where your Java file is saved.
This will compile your code. The compiler will check if there are any mistakes (errors).In our code, we made a mistake by leaving a character literal unclosed. This will result in the following output.

Now, open your file and replace 'Hello World' with "Hello World". In terminal than again enter 'javac HelloWorld.java'. If everything is correct, it will create a new file with the .class extension.

That .class file has bytecode inside it, which is not readable like normal code.
To run your program, type:

This will use the Java Virtual Machine (JVM) to run your program and show the result.