Java -5 | First Java Program | How to compile and Run Java Program

Опубликовано: 16 Июль 2026
на канале: Rishi Yadav
58
5

First Java Program
Let us look at a simple code that will print the words Hello World.
public class MyFirstJavaProgram
{
/* This is my first java program.
This will print 'Hello World' as the output */
public static void main(String []args)
{ System.out.println("Hello World"); // prints Hello World
}
}
How to save the file
Open notepad and add the code as above.
Save the file as: MyFirstJavaProgram.java.

How to compile and run the java prog.
Open a command prompt window and go to the directory where you saved the class. Assume it's C:\.
Compile the java program: -
Type 'javac MyFirstJavaProgram.java' and press enter to compile your code.
If there are no errors in your code, the command prompt will take you to the next line (Assumption : The path variable is set).
Run the java program: -  type ' java MyFirstJavaProgram ' to run your program.

Output
You will be able to see ' Hello World ' printed on the window as output
C:\\ javac MyFirstJavaProgram.java
C:\\ java MyFirstJavaProgram
Output : - Hello World
Understanding the first java program
class keyword is used to declare a class in java.
public keyword is an access modifier which represents visibility, it means it is visible to all.
void is the return type of the method, it means it doesn’t return any value.
main represents startup of the program.
static is a keyword, if we declare any method as static, it is known as static method.
The core advantage of static method is that there is no need to create object to invoke the static method.
The main method is executed by the JVM so it doesn’t require to create object to invoke the main method. So it saves memory.
String args[] is used for command line argument.
System.out.println( ) is used to print statement.

file name should be same as class name
its recommended that file name should be same as class name because : -
Suppose, when you compile a .java file in which more than one class resides, it will generate same number of .class files as there are number of classes present in .java file
At that time you will not able to identify which class need to interpret by java interpreter and which class containing entry point (main method) for the program.

Basic Syntax
About Java programs, it is very important to keep in mind the following points: -
Case Sensitivity − Java is case sensitive, which means identifier Hello and hello would have different meaning in Java.
Class Names − For all class names the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.
Example: class MyFirstJavaClass
Method Names − All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case.
Example: public void myMethodName()