Last Updated: 11 August, 2023
On this tutorial page, we are going to learn how to write a Java program as well as how to compile and run it step-by-step. For demonstration purposes, we will write a simple Java program and then compile and run it.
Before we can start writing a Java program, we first ensure that the JDK is installed and the path is set on our local system.
If JDK is not installed, click the link below and follow the instructions to install it.
👉 Java Environment SetupActions: ✅ Write a Java Program ⇨ ✅ Compile ⇨ ✅ Run
Java file extension is .java so save the above file as HelloJava.java
To compile the Java source code file, open the command prompt, then type javac followed by the file name with .java extension, and press enter.
javac is an abbreviation for Java Compiler. It is a compilation tool that is available in the JDK.
javac HelloJava.java
During the compilation phase, if the Java compiler (javac) encounters any error, it throws a compile-time error message and stops the further process. If there is no error, the Java compiler converts the source code into byte code and generates a new file called HelloJava.class
To run the Java program, we use a tool called "java". The syntax is java file_name. Write the file name without the extension.
java HelloJava
Output of the Program
Welcome to Java Programming World
The Java source code file extension is .java
Java allows us to write any number of classes in one source code file.
There will be only one public class in one source code file.
We can write a Java program in any text editor like Notepad or in an IDE (Integrated Development Environment) such as Eclipse, NetBeans, etc.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is javac?
Ans. javac is a Java Compiler. It is an important tool of the JDK. Whenever we compile a Java source code file (.java), the Java compiler is responsible for compiling the Java source code, and once successful compilation is done, it generates a .class file. The .class file is the bytecode that is platform-independent. Then the JVM executes the bytecode to run the program.
For compiling the Java source code file, we write the command given below.
javac Test.java
First, we write a java compiler name that is javac followed by a java file name with a .java extension.
Can the main method be Overloaded in Java?
Ans. Yes, the main method can be Overloaded in Java. We can write any number of Overloaded main methods, but at run time, the JVM will call only the main method with the definition of-
public static void main(string[] args)
Overloaded main method example:
class Test { public static void main(String args[]) { System.out.println("main method with string argument"); } public static void main(int[] args){ System.out.println("main method with int argument"); } public static void main(char[] args){ System.out.println("main method with char argument"); } public static int main(double[] args){ System.out.println("main method with double argument"); } public static void main(float args){ System.out.println("main method with float argument"); } }
Output
main method with string argument
Why is the main method static in Java?
A static method belongs to a class, and for calling the static method, an object of the class is not required.
In Java, the main method is static, and it is the starting point from where the compiler starts programme execution. An object is not required for calling the method therefore the main method is made static in Java.
Can we write more than one public class in a single Java file?
Ans. No, because when we declare a class as public, then we have to save the java file with the same name as the name of the public class and also the public class should contain the main method, as the compiler will check for the main method first.
A compile-time error will be generated if we have more than one public class in a single file.
The purpose of including multiple classes in one source file is to bundle related support functionality (internal data structures, support classes, etc.) together with the main public class.