DEV Community

Alimam Miya
Alimam Miya

Posted on

How to write my first Java Program

n this tutorial, we will start by writing our A simple Java program. It will cover the very basic structure of a program and also the rules that are followed to write a successful Java program. We will be using a console/terminal to compile our program, run and display the output of our program.

Let’s write our first simple Java program that prints “Hello world !” message in the console. At first, open your favorite text editor. Some of the freely available text editors are Notepad++, Atom, and Visual Studio Code which you can use for this tutorial.

Create a new hello.text file & type the following program.

class FirstProgram{
public static void main(String args[]){
System.out.println("Hello World !");
}}

The above program is composed of two components:

  • The class block
  • The main function
  • The class block is kind of a wrapper block that contains all the code of a class.
  • A class basically contains all code that relates to each other. For now, our class is named as FirstProgram.
  • The class block has a main() function that is the starting point of the program. (Ignore other keywords as they will be covered later)
  • The statement System.out.println(“Hello World !”); prints the actual message in the console. Feel free to change the message between double-quotations to anything you want to print in the console.

Save the following program in your preferred directory and name it FirstProgram.java

Once saved, open cmd prompt and go to the directory where you have saved the program

Type the following line and press Enter: javac FirstProgram.java

The above line will use the java compiler to compile the Java program. It will fail to compile if there is an error in the program. If there are 0 errors, then it will generate FirstProgram.class file in the same directory.

After a successful compilation of the program, Type the following line and press Enter. java FirstProgram

This actually executes the Java program and print the output on the next line.

Congratulations! you have successfully written and executed your first Java program.

Note:

  1. We have named our class as the first program, you can name it anything you want. Make sure you give it a valid name as per the name rules.
  2. By convention, a class name starts with a capital case and subsequent internal words are started with capital letters too. Example: CalculatorProgram, MyJavaProgram, HelloWorldProgram, etc.
  3. We cannot use a special character in the class name such as !,*,^,%, etc. Only underscore (_) is allowed.
  4. The first character of a class name should always be an underscore or an alphabet. The class name should not start with a number or other special characters.
  5. For this tutorial, we have saved the file with the name FirstProgram.java which is exactly the same as the class name which makes it easy to remember for compiling and executing.
  6. When we compile the program with the javac command, it generates compiled .class files for that program.
  7. For example, if you have a file MyProgram.java which has a class with the name Calculator, then you will compile the program as javac MyProgram.java
  8. To execute the program, you will type: java Calculator
  9. The words public, static, void, class are keywords that should be in small-casing.
  10. The words String and System are pre-defined classes.

Top comments (0)