First simple Java program

Previous article introduced fundamental information about Java programming language. Now let’s start create a simple application.

Prerequisite

Before starting this section, please ensure you have some software installed on your machine:

  • JRE or JDK
  • Setup JAVA_HOME for System environment variable. Please follow the instruction here.
  • Text editor (like Notepad++, Emacs, Vim) or IDE (like Eclipse, IntelliJ Idea)
    (for this site, I will use IntelliJ Idea Community Edition for creating program)

Let’s start

Create a java project

  • Launch IntelliJ Idea Community Edition > Create a new project
  • Select Java
  • Set project SDK: browse to JDK directory if the text is not loaded successfully.
  • Next
  • Next
  • Set project name and location

It’s done for creating project. IntelliJ Idea will show the project like below.

Now, It’s time for writing code. We need:

Create a package

  • Right-click on src > New > Package
  • Type package name. The name should be in lower case.

Create a java class and write some code

  • Right-click on package that you’ve just created.
  • Enter the name of class

Run the program

Finally, it’s time to write simple lines of code. Just copy and paste below code to your created class.

package firstProgram;

public class FirstProgram {
    public static void main(String[] args) {
        System.out.println("Hello tandt53. This is the first java program created by you.");
    }
}

Let’s run this code by right-click on the code and select Run ‘FirstProgram.main()’ and check the result.

Conclusion

Phew!!!! It was a lot of works, right? Now, take 5 mins to review what you’ve done:

  • Created a brand new java project
  • Add a package and a java class inside
  • Write some code and execute it

Those are common steps that you need to do when you’re going to write a java program. However, there are some question inside you mind now.

  • Why do you need to follow all of them?
  • What are package and class in java?
  • What was exactly you just created in java class?

I will go details in the next post to help you clear all above questions. Now take you time to rest. 🙂