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. 🙂

Java Introduction and Installation

Applications

Java is a well-known programming language. There are so many applications created by using it.
– Web application
– Mobile application
– Desktop application
– Games
– Database connection
– Much more

Properties

  • Java is an independent platform language. It means that the application created by using Java can be run on any platform (Windows, Mac, Linux) thanks to Java Virtual Machine (JVM).
  • Object-Oriented Programming language. It is a based paradigm that Java supports. Every element in Java are objects, except primitive data types.
  • Interpreted and compiled code. The source code is written in .java files, then they are compiled into bytecode format in .class files. At run time, JVM reads the bytecode instructions and interpret to the native hardware platform which JVM was written.
  • Self memory management: Java allocates and de-allocate objects in memory automatically. JVM will delete the objects which do not have active pointer exists, it’s called Garbage Collector.

Installation

To run Java programs, you must have either Java Runtime Environment (JRE) or Java Development Kit (JDK). JDK contains everything JRE has and much more tools such as the compilers and debuggers necessary for developing applets and applications. Thus, it is recommended that you should install JDK for playing more with Java.

enter image description here

For JDK, there are some options that you can choose to develop various types of applications, such as Java Platform, Standard Edition, Enterprise Edition.

For the next articles, I would suggest to use basically JDK Standard Edition (SE) version 8, even the latest version is 12.

You can find the installation files for your OS in here.

If you got any trouble in installation, please leave your comment below. I would be happy to help you.

Next article, we will start creating a simple java application and more details about parts of a program.