Wednesday, 8 June 2022

3. Compilation in Java

Java program compilation process

After completion of writing the java program, we need to save it by using the .java extension.

A compiler is a software that converts the source code to a class file that is the binary language of 0s and 1s.

The standard syntax to compile the java program is:

<javac>    <class name/file name>  .java

The standard syntax to execute a program in java is:

<java>    <classname/file name>

In java, we should save the file with the class name, which means the file name and class name should be identical.

Compilation example:

javac Filename.java

Execution example:
java Filename

For example:

public class My_Program {

-----------------------------

---------------

-----------------------------------

}

In the above program, the class name is My_Program

Save the program with file name using .java extension

===> My_program.java

After the completion of saving the program, it needs to be compiled. Java compiler is responsible for compiling the java program 

===> javac My_Program.java

Whenever we compile the program, if there are no errors in the program .class file is going to be generated. If there are errors in the program .class file will not generate.

ls command ( list ) is used to find the list of files presented.

>>> ls

Whenever we type ls command it will show list of files available. It will generate .java and .class files

===> My_Program.java

===> My_Program.class

After compilation is done the .class file needs to be executed by the JVM and produce the result.

===> java My_Program

check your knowledge

I hope you have learned something new today!

Now it's time to check your knowledge!!

Let's answer the below queries

Write answers to the following questions in the comments section

  1. What is compilation?
  2. What is the standard syntax for compilation and execution of java program?
  3. Is class name and file name should be same?
  4. Write a simple java program with class definition and write compilation and execution steps of your program with your file name?
  5. What is ls command?

If you are struggling somewhere to answer the above questions please review the post once again and try it.

😊HAPPY LEARNING👍
👉Please Subscribe, share, follow and comment🙏💓

THANKING YOU ALL

No comments:

Post a Comment

4. Main method in Java

Main method In order to write a program in java, we need to follow 3 standard steps Write a program Validate/compile Execute Examp...