Java the Easy Way

The way Java works

Java code is written in .java files. The compiler checks for errors and turns them into .class files (bytecode), which the JVM runs on any device.

Nov. 4, 2024, 6:32 a.m.

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.

2

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

dfs

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.

dsf

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.

dfsdf

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

dfsf

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