How to write a java program?

public class First { public static void main(String[] args) { System.out.println("Welcome To Java"); } } Let's breakdown the program: Line 1: public class First This line defines a class named First. In Java, everything must be inside a class. The keyword public means this class is accessible from anywhere in your program. Line 2: { This opening curly brace { marks the start of the class body. All the code inside the class will go between { and }. Line 3: public static void main(String[] args) This is the main method — the entry point of any Java program. Without this line, we can't run a java program. public: The method is accessible from outside the class. static: You don’t need to create an object of the class to run this method. void: This method does not return any value. main: The name of the method — Java looks for this to start the program. String[] args: This is used to accept command-line arguments (an array of Strings). Line 4: { This curly brace { starts the body of the main method. Line 5: System.out.println("Welcome To Java"); This line prints "Welcome To Java" to the screen. println() is a method that prints the message and moves the cursor to the next line. Final Output when you run the program: Welcome To Java

Apr 16, 2025 - 16:06
 0
How to write a java program?
public class First
{
 public static void main(String[] args)
    {
    System.out.println("Welcome To Java"); 
    }
}

Let's breakdown the program:

Line 1: public class First
This line defines a class named First.
In Java, everything must be inside a class.
The keyword public means this class is accessible from anywhere in your program.

Line 2: {
This opening curly brace { marks the start of the class body.
All the code inside the class will go between { and }.

Line 3: public static void main(String[] args)

This is the main method — the entry point of any Java program.
Without this line, we can't run a java program.

  • public: The method is accessible from outside the class.
  • static: You don’t need to create an object of the class to run this method.
  • void: This method does not return any value.
  • main: The name of the method — Java looks for this to start the program.
  • String[] args: This is used to accept command-line arguments (an array of Strings).

Line 4: {
This curly brace { starts the body of the main method.

Line 5: System.out.println("Welcome To Java");
This line prints "Welcome To Java" to the screen.
println() is a method that prints the message and moves the cursor to the next line.

Final Output when you run the program:
Welcome To Java