Writing First Java Program

Praveen Kumar
3 min readAug 4, 2022

--

Any Java Program can be written in using simple text editors like Notepad or Wordpad. In Java, the filename should be same as the name of the classname. The extension to the file name should be .java.

Let's create our first Java file, called HelloWorld.java,

public class HelloWorld{

/* 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! My First Java Program."); // prints Hello World
}
}

To run the code above,

  • Save the code in Notepad as “HelloWorld.java”. Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type “javac HelloWorld.java”: C:\Users\Your Name>javac HelloWorld.java
  • This will compile your code. If there are no errors in the code, the command prompt will take you to the next line. Now, type “java HelloWorld” to run the file: C:\Users\Your Name>java HelloWorld
  • The output should be: Hello World! My First Java Program.

Program Explanation:

1. Class definition

This line uses the keyword class to declare that a new class is being defined.

class HelloWorld {
//
//Statements
}

2. HelloWorld

It is an identifier that is the name of the class. The entire class definition, including all of its members, will be between the opening curly brace “{” and the closing curly brace “}“.

3. main method:

In the Java programming language, every application must contain a main method. The main function(method) is the entry point of your Java application, and it’s mandatory in a Java program. whose signature in Java is:

public static void main(String[] args)
  • public: It is a access mode of the main() by which the class visibility can be defined. So that JVM can execute the method from anywhere.
  • static: The main method is to be called without an object. The modifiers public and static can be written in either order.
  • void: The main method doesn’t return anything.
  • main(): Name configured in the JVM. The main method must be inside the class definition. The compiler executes the codes starting always from the main function.
  • String[]: It is a Class name. The main method accepts a single argument, i.e., an array of elements of type String.
  • args[]: It is an array which receives the command line arguements when the program runs.

4. System.out.println command:

  • System: It is a predefined class.
  • out: It is an output stream which is related to console.
  • println(): It is a method, used to print any message to the console. After println method, a newline is invoked. That means, if there is any other output to be printed, it will be printed on the new or next line.

5. Comments:

They can either be multiline or single-line comments.

// This is a simple Java program. 
// Call this file "HelloWorld.java".

This is a single-line comment. This type of comment must begin with // as in C/C++.

For multiline comments, they must begin from /* and end with */

/* This is a simple Java program. 
Call this file "HelloWorld.java".
*/

Also, remember one fact while writing the Java programs that it is a case sensitive programming language like C or C++.

Next part coming soon. Happy Learning!

--

--