• HTML
  • CSS
  • JAVASCRIPT
  • JAVA
  • HTML
  • CSS
  • JAVASCRIPT
  • JAVA

First Java Program

In this section, we will learn to write a simple Java program. But for this, we need to have JDK installed on our PC.

To create a Java Program, we need to create a class that has the main method. Let’s look at the requirements to write the Hello World Program.

The requirement for Java Hello World Example

For compiling and executing any Java program, we should have the following software installed on our PC.

  • Install the JDK if you haven’t installed it. (Preferably JDK 11)

  • Set the path of the jdk/bin directory. (Link)

  • Create the program

  • Compile and run the java program.

Creating Hello World Example

Let’s write the Java program to display Hello World:

class HelloWorld{

public static void main(String args[]){

System.out.println(“Hello Java”);

}

}

Save the above file as HelloWorld.java

To compile:

javac HelloWorld.java

After running this command in the command prompt, the javac tool compiles the source code into the byte code.

To execute:

java HelloWorld

This command is typed in the command prompt to execute the bytecode generated by the javac tool after compilation of HelloWorld.java

Output :

The output is shown in the same terminal window as follows:

Hello Java

Parameters used in First Java Program

Let’s discuss about some of the keywords used in our program.

  • class keyword is used to declare a class in Java.
  • public keyword is an access modifier that sets the visibility. It means this method is visible to all the classes and other methods and can be accessed from outside the package.

  • static is a keyword in Java. For an example, if we declare a method as static, we do not need to create a new object to invoke the static method.

  • void is the return type of the method. It means that the method isn’t returning any value.

  • main means that it is the starting point of the program.

  • String[] args or String args[] is used as parameters in the main method. These are for the command line arguments.
  • System.out.println is used in Java to print the statement. In this line, System is a class, the word out is an object of the PrintStream class in Java where println() is the method under this class.

To write the HelloWorld Program, you need to open notepad by Start Menu -> Type Notepad on the Search Bar -> Click on Notepad and write a simple HelloWorld program as below:

helloWorld

Now to compile and execute the above written program, you need to open command prompt by Start Menu -> Type cmd on Search bar and click on Command Prompt.

output

In how many ways we can write a Java program?

 

We can write Java program in many ways. The valid ways to modify the Java program are:

1. By changing the sequence of the modifiers, method prototype is not changed in Java.

          For example, the main method can be written as static public void main(String[] args)

2. The subscript notation in the Java array can be used after the type, before the variable or after the variable.

          Let’s see the different codes to write the main method.

  • public static void main(String[] args)
  • public static void main(String []args)
  • public static void main(String args[])

3. You can provide variable arguments support to the main() method by passing 3 dots (ellipses)

         For an example, public static void main(String… args)

4. Semicolon at the end of the class is optional.

 

Valid Java main() method signature

  • public static void main(String[] args)  
  • public static void main(String []args)  
  • public static void main(String args[])  
  • public static void main(String… args)  
  • static public void main(String[] args)  
  • public static final void main(String[] args)  
  • final public static void main(String[] args)  
  • final strictfp public static void main(String[] args) 

Invalid Java main() method signature

  • public void main(String[] args)  
  • static void main(String[] args)  
  • public void static main(String[] args)  
  • abstract public static void main(String[] args)  

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Course Content

  • JAVA
    • C++ vs Java
    • Features of Java
    • First Java Program
    • How to set Java path?
    • Internal Working of Java Program
    • Java Control Statements
    • Java Datatypes
    • Java Operators
    • Java Variables
    • JDK, JRE and JVM
    • Keyword
    • What is java

© 2025. ITTrainingWala