{"id":7908,"date":"2025-03-12T10:50:20","date_gmt":"2025-03-12T10:50:20","guid":{"rendered":"https:\/\/msgprogramator.sk\/?p=7908"},"modified":"2026-04-01T11:55:27","modified_gmt":"2026-04-01T11:55:27","slug":"hello-world-java","status":"publish","type":"post","link":"https:\/\/msgprogramator.sk\/en\/hello-world-java\/","title":{"rendered":"Hello World or your first Java application (tutorial)"},"content":{"rendered":"<p><strong>Do you want to program your first Hello World Java application but don&#8217;t know how to get started? This article walks you step-by-step through creating your first hello world Java program called <em>TimeNow<\/em>, which displays the current date and time. <\/strong><\/p>\n<p>Usually, the first lesson in programming is to deal with listing the <strong>Hello World!<\/strong> greeting to the console. And since I like original ideas, <strong>as the first Java application<\/strong> we will create a TimeNow program that will work like a digital watch &#8211; it will print the current date and exact time when the program is started. Whether you&#8217;re a <a href=\"https:\/\/msgprogramator.sk\/en\/become-a-developer\/\" target=\"_blank\" rel=\"noopener\">beginner or a programmer<\/a>, still in school or already working, everyone can do it with our tutorial. So let&#8217;s get started!<\/p>\n<div class=\"inside\">\n<div class=\"inside\" style=\"text-align: center;\"><\/div>\n<\/div>\n<p>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.<\/p>\n<h2>Preparing for Java application development<\/h2>\n<p>In order to start developing Java applications, we first need to download and install two basic programs:<\/p>\n<ol>\n<li><strong>JDK<\/strong> (Java Development Kit) &#8211; contains the tools needed to compile and run Java programs.<\/li>\n<li><strong>Development environment (IDE)<\/strong> &#8211; 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.<\/li>\n<\/ol>\n<p>For Java, we have several environments to choose from. The most well-known development environments (IDEs) include IntelliJ IDEA, Eclipse and Netbeans.<\/p>\n<div class=\"inside\">\n<div class=\"inside\" style=\"text-align: center;\"><\/div>\n<\/div>\n<p>Whichever one you choose, after installing the JDK and the development environment following our instructions, you&#8217;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.<\/p>\n<h2>Creating a new Java project<\/h2>\n<ol>\n<li>Start your installed development environment.<\/li>\n<li>If the welcome screen opens, click on New Project &#8211; <em>New Project<\/em>. Otherwise, go to the main menu <em>File -&gt; New Project<\/em>.<\/li>\n<li>In the <em>New Project<\/em> window, select <em>Java<\/em> from the list on the left.<\/li>\n<li>Name your project, e.g. \u201c<em>TimeNow\u201d.<\/em><\/li>\n<li>Make sure you have the JDK installed and see the latest version there.<\/li>\n<li>Uncheck <em>Add sample code. <\/em>You won&#8217;t need the sample code, you can create your own.<\/li>\n<li>Check the following picture to see if you have followed the correct procedure. If everything is as you see below, click the <em>Create<\/em> button and the IDE will create and load a new Java project.<\/li>\n<\/ol>\n<p><em>Creating a new project in the IntelliJ IDEA development environment<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4985 size-full aligncenter\" src=\"https:\/\/msgtester.sk\/wp-content\/uploads\/2025\/03\/Vytvorenie-noveho-projektu-TimeNow.webp\" alt=\"Creating a new project in the IntelliJ IDEA development environment\" width=\"624\" height=\"569\" \/><\/p>\n<p><em>Empty Java project in IntelliJ IDEA development environment<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4982 size-full aligncenter\" src=\"https:\/\/msgtester.sk\/wp-content\/uploads\/2025\/03\/prazdny-projekt-timenow.webp\" alt=\"Empty Java project in IntelliJ IDEA development environment\" width=\"624\" height=\"466\" \/><\/p>\n<h2>Creating the first Java class<\/h2>\n<p><strong>1. Right-click on the src tab and select <em>New -&gt; Java class<\/em> from the context menu.<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4979 size-full aligncenter\" src=\"https:\/\/msgtester.sk\/wp-content\/uploads\/2025\/03\/Vytvorenie-prvej-Java-triedy.webp\" alt=\"1. Right-click on the src tab and select New -&lt;encoded_tag_closed \/&gt; Java class from the context menu.\" width=\"624\" height=\"405\" \/><\/p>\n<p><strong>2. For example, name it <em>\u201cMain\u201d<\/em> and press Enter.<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4976 size-full aligncenter\" src=\"https:\/\/msgtester.sk\/wp-content\/uploads\/2025\/03\/Vytvorenie-prvej-Java-triedy-2.webp\" alt=\"For example, name it \u201cMain\u201d and press Enter\" width=\"354\" height=\"241\" \/><\/p>\n<p><strong>3. IntelliJ creates the Main.java file.<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4973 size-full aligncenter\" src=\"https:\/\/msgtester.sk\/wp-content\/uploads\/2025\/03\/IntelliJ-vytvori-subor-Main.java_.webp\" alt=\"IntelliJ creates the Main.java file\" width=\"503\" height=\"224\" \/><\/p>\n<p><strong>4. Edit this file as follows. Feel free to copy this code and explain each line. <\/strong><\/p>\n<pre><code class=\"language-java\" data-line=\"\">1.\timport java.time.LocalDateTime;\n2.\timport java.time.format.DateTimeFormatter;\n3.\t\n4.\tpublic class Main {\n5.\t    public static void main(String[] args) {\n6.\t        DateTimeFormatter formatovacCasu = DateTimeFormatter.ofPattern(&quot;dd.MM.yyyy HH:mm:ss&quot;);\n7.\t        LocalDateTime teraz = LocalDateTime.now();\n8.\t        String formatovanyCas = teraz.format(formatovacCasu);\n9.\t        System.out.println(&quot;Presn\u00fd \u010das je: &quot; + formatovanyCas);\n10.\t    }\n11.\t}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>This is what the resulting TimeNow \u201cHello World\u201d application looks like in Java<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4991 size-full aligncenter\" src=\"https:\/\/msgtester.sk\/wp-content\/uploads\/2025\/03\/vysledny-program-timenow.webp\" alt=\"This is what the resulting TimeNow \u201cHello World\u201d application looks like in Java\" width=\"624\" height=\"155\" \/><\/p>\n<h2>Explaining the code of your first Java application<\/h2>\n<ul>\n<li><strong>Line 1<\/strong>: This line says that we want to use the <em>LocalDateTime<\/em> class, which is part of the java.time package. This class allows us to work with the current date and time.<\/li>\n<li><strong>Line 2<\/strong>: This line imports the <em>DateTimeFormatter<\/em> class, which is used to format dates and times into a more readable text format according to a specified pattern.<\/li>\n<li><strong>Line 4<\/strong>: Here begins the definition of our main class called <em>Main<\/em>. In Java, you need to have at least one class that contains a main() method where the program starts.<\/li>\n<li><strong>Line 5<\/strong>: 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.<\/li>\n<li><strong>Line 6<\/strong>: In this line, we create an instance of the <em>DateTimeFormatter<\/em> class and call it &#8216;formatovacCasu&#8217;. We use the <em>ofPattern<\/em> method to set the date and time format. The pattern \u201cdd.MM.yyyy HH:mm:ss\u201d means that the date will be in the form day.month.year and the time will be in the form hours:minutes:seconds.<\/li>\n<li><strong>Line 7<\/strong>: Create an object &#8211; now of type <em>LocalDateTime<\/em> &#8211; that contains the current date and time. The now() method of the <em>LocalDateTime<\/em> class retrieves these values from the system.<\/li>\n<li><strong>Line 8<\/strong>: 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.<\/li>\n<li><strong>Line 9<\/strong>: Finally, this line outputs text to the console that contains the string \u201cThe exact time is:\u201d associated with our formatted date and time.<\/li>\n<li><strong>Lines 10 and 11<\/strong>: These lines enclose blocks &#8211; the first encloses the main() method and the second encloses the entire Main class.<\/li>\n<\/ul>\n<h2>Running an application in Java<\/h2>\n<p>Now you can finally run your Hello World Java application via Run (in IntelliJ it&#8217;s a green triangle) and a message will appear in the console with the exact date and time.<\/p>\n<p><em>Starting a completed TimeNow program<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4988 size-full aligncenter\" src=\"https:\/\/msgtester.sk\/wp-content\/uploads\/2025\/03\/Spustenie-aplikacie-timenow.webp\" alt=\"Starting a completed TimeNow program\" width=\"624\" height=\"309\" \/><\/p>\n<h2>How next?<\/h2>\n<p>If you like programming in Java and want to program something a bit more complex, try our next tutorial on <a href=\"https:\/\/msgprogramator.sk\/en\/java-game-coding\/\" target=\"_blank\" rel=\"noopener\">how to program a Java game<\/a>. After all, we learn the most when we play.<\/p>\n<p>On <em>msgprogramator.sk<\/em> we have other tutorials &#8211; not only for Java games, but even for AI &#8211; but these are intended for more advanced <a href=\"https:\/\/msg-life.sk\/praca\/programator\/\" target=\"_blank\" rel=\"noopener\">programmers<\/a>. You can also learn <a href=\"https:\/\/msgprogramator.sk\/en\/java-design-patterns\/\" target=\"_blank\" rel=\"noopener\">design patterns<\/a> that will definitely come in handy when developing applications.<\/p>\n<h2>Conclusion<\/h2>\n<p>This article takes you step-by-step through the creation of your first<strong> hello world Java application<\/strong> called <em>TimeNow<\/em>, 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.<\/p>\n<h2>FAQ<\/h2>\n<h3>Why is the hello world programme important?<\/h3>\n<p>This program is important because it teaches you basic programming concepts such as syntax and output. It&#8217;s an easy way for you to become familiar with a new programming language.<\/p>\n<h3>How can I run a hello world program in Java?<\/h3>\n<p>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(\u201cHello, World!\u201d).<\/p>\n<h3>What does the simplest \u201cHello, World!\u201d code look like? in Java?<\/h3>\n<p>Basically, it is the simplest possible program that demonstrates how to write, translate and run code in a given language. \u201cHello world\u201d 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:<\/p>\n<pre><code class=\"language-java\" data-line=\"\">public class HelloWorld { \npublic static void main(String[] args) { \nSystem.out.println(&quot;Hello, World!&quot;); \n   } \n}<\/code><\/pre>\n<p>This code creates a class called HelloWorld, which contains the main() method. When you run this program, the text \u201cHello, World!\u201d is displayed on the console. This simple example will help you understand how Java works and how to write basic programs.<\/p>\n<h3>What are the next steps after programming the hello world program?<\/h3>\n<p>Once you&#8217;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.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you want to program your first Hello World Java application? This article will guide you through creating your first Java program. <\/p>\n","protected":false},"author":14,"featured_media":5254,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[57],"tags":[],"class_list":["post-7908","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/7908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/comments?post=7908"}],"version-history":[{"count":2,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/7908\/revisions"}],"predecessor-version":[{"id":42902,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/posts\/7908\/revisions\/42902"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media\/5254"}],"wp:attachment":[{"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/media?parent=7908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/categories?post=7908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/msgprogramator.sk\/en\/wp-json\/wp\/v2\/tags?post=7908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}