Hello World or your first Java application (tutorial)

Do you want to program your first Hello World Java application but don’t know how to get started? This article walks you step-by-step through creating your first hello world Java program called TimeNow, which displays the current date and time.

Hello World or your first Java application (tutorial)
Hello World or your first Java application (tutorial)

V článku sa dozvieš:

    Usually, the first lesson in programming is to deal with listing the Hello World! greeting to the console. And since I like original ideas, as the first Java application we will create a TimeNow program that will work like a digital watch – it will print the current date and exact time when the program is started. Whether you’re a beginner or a programmer, still in school or already working, everyone can do it with our tutorial. So let’s get started!

    Basically, it is the simplest possible program that demonstrates how to write, translate and run code in a given language. Hello world programs are also useful for verifying that the development environment is properly installed and working.

    Preparing for Java application development

    In order to start developing Java applications, we first need to download and install two basic programs:

    1. JDK (Java Development Kit) – contains the tools needed to compile and run Java programs.
    2. Development environment (IDE) – provides a comfortable environment for writing code, testing it and easy launching of programs without complicated settings. In addition, it automatically highlights errors in the code and offers suggestions for fixes.

    For Java, we have several environments to choose from. The most well-known development environments (IDEs) include IntelliJ IDEA, Eclipse and Netbeans.

    Whichever one you choose, after installing the JDK and the development environment following our instructions, you’re ready to start programming in Java. The following procedure is the same for all environments. The images used are from the IntelliJ IDEA development environment.

    Creating a new Java project

    1. Start your installed development environment.
    2. If the welcome screen opens, click on New Project – New Project. Otherwise, go to the main menu File -> New Project.
    3. In the New Project window, select Java from the list on the left.
    4. Name your project, e.g. “TimeNow”.
    5. Make sure you have the JDK installed and see the latest version there.
    6. Uncheck Add sample code. You won’t need the sample code, you can create your own.
    7. Check the following picture to see if you have followed the correct procedure. If everything is as you see below, click the Create button and the IDE will create and load a new Java project.

    Creating a new project in the IntelliJ IDEA development environment

    Creating a new project in the IntelliJ IDEA development environment

    Empty Java project in IntelliJ IDEA development environment

    Empty Java project in IntelliJ IDEA development environment

    Creating the first Java class

    1. Right-click on the src tab and select New -> Java class from the context menu.

    1. Right-click on the src tab and select New -<encoded_tag_closed /> Java class from the context menu.

    2. For example, name it “Main” and press Enter.

    For example, name it “Main” and press Enter

    3. IntelliJ creates the Main.java file.

    IntelliJ creates the Main.java file

    4. Edit this file as follows. Feel free to copy this code and explain each line.

    1.	import java.time.LocalDateTime;
    2.	import java.time.format.DateTimeFormatter;
    3.	
    4.	public class Main {
    5.	    public static void main(String[] args) {
    6.	        DateTimeFormatter formatovacCasu = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss");
    7.	        LocalDateTime teraz = LocalDateTime.now();
    8.	        String formatovanyCas = teraz.format(formatovacCasu);
    9.	        System.out.println("Presný čas je: " + formatovanyCas);
    10.	    }
    11.	}

     

    This is what the resulting TimeNow “Hello World” application looks like in Java

    This is what the resulting TimeNow “Hello World” application looks like in Java

    Explaining the code of your first Java application

    • Line 1: This line says that we want to use the LocalDateTime class, which is part of the java.time package. This class allows us to work with the current date and time.
    • Line 2: This line imports the DateTimeFormatter class, which is used to format dates and times into a more readable text format according to a specified pattern.
    • Line 4: Here begins the definition of our main class called Main. In Java, you need to have at least one class that contains a main() method where the program starts.
    • Line 5: This is the beginning of the main() method, which is the entry point of the application. When you run the program, this method is executed.
    • Line 6: In this line, we create an instance of the DateTimeFormatter class and call it ‘formatovacCasu’. We use the ofPattern method to set the date and time format. The pattern “dd.MM.yyyy HH:mm:ss” means that the date will be in the form day.month.year and the time will be in the form hours:minutes:seconds.
    • Line 7: Create an object – now of type LocalDateTime – that contains the current date and time. The now() method of the LocalDateTime class retrieves these values from the system.
    • Line 8: This line reformats the current date and time according to the set format. The format() method uses our formattedCas and stores the result in the formatovacCasu string.
    • Line 9: Finally, this line outputs text to the console that contains the string “The exact time is:” associated with our formatted date and time.
    • Lines 10 and 11: These lines enclose blocks – the first encloses the main() method and the second encloses the entire Main class.

    Running an application in Java

    Now you can finally run your Hello World Java application via Run (in IntelliJ it’s a green triangle) and a message will appear in the console with the exact date and time.

    Starting a completed TimeNow program

    Starting a completed TimeNow program

    How next?

    If you like programming in Java and want to program something a bit more complex, try our next tutorial on how to program a Java game. After all, we learn the most when we play.

    On msgprogramator.sk we have other tutorials – not only for Java games, but even for AI – but these are intended for more advanced programmers. You can also learn design patterns that will definitely come in handy when developing applications.

    Conclusion

    This article takes you step-by-step through the creation of your first hello world Java application called TimeNow, which displays the current date and time. You learned how to install the necessary tools, create a new project, write and run the code. This simple example will give you a solid foundation for future Java programming and open the door to more complex projects. If you enjoy programming, continue with more tutorials and games to help you develop your skills.

    FAQ

    Why is the hello world programme important?

    This program is important because it teaches you basic programming concepts such as syntax and output. It’s an easy way for you to become familiar with a new programming language.

    How can I run a hello world program in Java?

    To run the hello world program in Java, you need to install the JDK and the IDE. Then you create a new Java project, add a class with the main() method, and use System.out.println(“Hello, World!”).

    What does the simplest “Hello, World!” code look like? in Java?

    Basically, it is the simplest possible program that demonstrates how to write, translate and run code in a given language. “Hello world” programs are also useful for verifying that the development environment is set up and working properly. In Java, such a program would look like the following:

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

    This code creates a class called HelloWorld, which contains the main() method. When you run this program, the text “Hello, World!” is displayed on the console. This simple example will help you understand how Java works and how to write basic programs.

    What are the next steps after programming the hello world program?

    Once you’ve programmed a hello world program, you can try creating more complex programs, such as simple games or apps. You can also learn design patterns and other advanced programming concepts.

     

    About the author

    Jozef Wagner

    Java Developer Senior

    I have been programming in Java for more than 10 years, currently I am working in msg life Slovakia as a senior Java programmer and I help customers to implement their requirements into Life Factory insurance software. In my free time I like to relax in the mountains or play a good computer game.

    Let us know about you