/ Java

Write your First Program in Java

After learning about the Java History and installing JDK. So, We’ll now write the first Java Hello World Program. Before proceeding, we must ensure the following requirements are fulfilled or not.

Basic Requirements

Here is the list of requirements you need to have to execute the program.

– Install the Java Development Kit (JDK) based on your computer, download the JDK here, if you don’t have installed it.

You also need the Notepad text editor to write your program.

Simple Hello World Program

Write the following code in the Notepad Text editor.

//Java Hello World Program

class Hello{

public static void main(String args[])

{

System.out.println("Hello World! ");

}

}
  • Save the file in your preferred location as Hello.java
  • Now open the Command Prompt and go to the preferred location where your program lies.
  • Now compile your program by typing javac Hello.java
  • If you don’t get any error you’ll see the class file in your preferred location.
  • Now type java Hello to execute the file.

Output:

Hello World

Explanation of Hello World Program ( First Java Program )

You’ve written your first Java Program. You’ll get confused about different sentence used in the program. So let us discuss them in details by explaining every part of the program.

  1. //Java Hello World Program: // denotes the comment in Java. It is useful when you want to understand the program better. Comments don’t execute in the program.
  2. class: class is the reserved keyword in Java which is used to declare the class of Java Program. In our example class Hello denotes the Hello class.
  1. {: This denotes the scope in the programming. Firstly { denoting the start of class scope and secondly { denotes the start of the main scope.
  2. public: public keyword is the access modifier and represents the visibility of the method. The main method is always public.
  3. static: static is a keyword that is used when we don’t want to create the object to invoke a method. It saves memory by the direct call. JVM directly calls the main method so it must be static.
  4. void: void is the return type of the method. Since the main doesn’t need to return any value return type is always void.
  5. main: Main keywords denotes the main function of the class from where the program starts to execute.
  6. String args[]: String args[] is the String argument which is certainly used to provide the command line arguments to the program.
  7. System.out.println(): This is used to print the line specified in its argument.
  8. }: Finally, this is the closing scope of the program. Firstly closing specifies the closing scope of the main method and secondly } denotes the closing scope of class Hello.

Variations of representing the main method.

The main method signature certainly be altered providing the correct output. Following are some of the variations:

  1. By changing modifiers sequence
static public void main(String args[])
  1. Changing subscript notation in Java array
public static void main(String[] args)

public static void main(String args[])

public static void main(String []args)
  1. Passing three dots as arguments (ellipse)
public static void main (String ... args)
  1. Optionally put a semicolon (;) at class end scope
class Hello{

public static void main(String args[]){

System.out.println("Hello");

}

};

 

Valid and Invalid Java main method signature

These are Java valid main method signatures:

static public void main(String args[])

public static void main(String[] args)

public static void main(String args[])

public static void main(String []args)

public static 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)

 

In contrast, these are invalid Java main method signatures:

public void main(String[] args) 

static void main(String[] args) 

public void static main(String[] args) 

abstract public static void main(String[] args)

 

javac Error and How to resolve it in your Computer.

Though you have installed JDK compiler in your computer you may get following error while compiling the Java Program.

javac error

This is possibly caused by the unrecognized command by your Operating system. The best solution for this is to set up a path on your computer.

 

Download the article
Dipendra Laxmi Bahadur Chand Thakuri
CEO @ Vine Software Innovation Company (Pvt.) Ltd. (VSIC)
View More

Comments:

No Comments!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

back to top button
error: Please be respectful of copyright. Unauthorized use is prohibited.