{"id":2767,"date":"2017-05-28T16:01:17","date_gmt":"2017-05-28T10:31:17","guid":{"rendered":"http:\/\/www.java2blog.com\/?p=2767"},"modified":"2023-11-09T22:46:07","modified_gmt":"2023-11-09T17:16:07","slug":"java-hello-world-program","status":"publish","type":"post","link":"https:\/\/java2blog.com\/java-hello-world-program\/","title":{"rendered":"Java Hello World Program"},"content":{"rendered":"<h2><span id=\"Introduction\">Introduction<\/span><\/h2>\n<p>In this post, we will see how to write your first java program. This post is intended only for java beginners to acquaint them with the steps to write a simple java program. Simply put, we will write a code that outputs <code>\"Hello World!\"<\/code> to your console\/screen.<\/p>\n<p>For Folks having some knowledge in other programming languages, this is a basic program to print a statement on your console as you did earlier.<br \/>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Introduction\">Introduction<\/a><\/li><li><a href=\"#Prerequisite_for_running_8220Java_Hello_World_Program8221\">Prerequisite for running &#8220;Java Hello World Program&#8221;<\/a><\/li><li><a href=\"#How_does_8220Java_program_to_print_Hello_World8221_works\">How does &#8220;Java program to print Hello World&#8221; works?<\/a><ul><li><a href=\"#Class_declaration\">Class declaration<\/a><\/li><li><a href=\"#Main_method\">Main method<\/a><\/li><li><a href=\"#Print_statement\">Print statement<\/a><\/li><li><a href=\"#Semicolon\">Semicolon<\/a><\/li><\/ul><\/li><li><a href=\"#Compile_and_run_the_program\">Compile and run the program<\/a><\/li><li><a href=\"#In_how_many_ways_can_you_write_a_Java_Program\">In how many ways can you write a Java Program?<\/a><ul><li><a href=\"#1_By_Changing_the_Subscript_Notation_of_Input_Array_in_Main_Method\">1. By Changing the Subscript Notation of Input Array in Main Method<\/a><\/li><li><a href=\"#2_By_Changing_the_Sequence_of_Modifiers_in_the_method_without_modifying_Method_Prototype\">2. By Changing the Sequence of Modifiers in the method, without modifying Method Prototype<\/a><\/li><li><a href=\"#3_By_Replacing_Array_Subscript_Notation_with_3_ellipses_Dots_for_var-args_support\">3. By Replacing Array Subscript Notation with 3 ellipses (Dots) for var-args support<\/a><\/li><\/ul><\/li><li><a href=\"#Resolving_Error_8220javac_is_not_recognized_as_an_internal_or_external_command8221\">Resolving Error: &#8220;javac is not recognized as an internal or external command&#8221;?<\/a><\/li><li><a href=\"#Resolving_Error_8220reached_end_of_file_while_parsing8221\">Resolving Error: &#8220;reached end of file while parsing&#8221;<\/a><\/li><li><a href=\"#Exercise\">Exercise<\/a><\/li><li><a href=\"#Important_points\">Important points<\/a><\/li><\/ul><\/div>\n\n<h2><span id=\"Prerequisite_for_running_8220Java_Hello_World_Program8221\">Prerequisite for running &#8220;Java Hello World Program&#8221;<\/span><\/h2>\n<p>This Java hello world program will be a very simple program that will print <code>Hello, World!<\/code> to console.<\/p>\n<p>Before running the program, you need to make sure java is properly installed on your machine.<\/p>\n<ul>\n<li>Install the JDK if you don&#8217;t have it installed, <a href=\"http:\/\/www.oracle.com\/technetwork\/java\/javase\/downloads\/index.html\" target=\"_blank\" rel=\"noopener noreferrer\">download the JDK<\/a> and install it.<\/li>\n<li>Set path of the JDK\/bin directory. you can follow <a href=\"http:\/\/www.javatpoint.com\/how-to-set-path-in-java\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">this<\/a> link for setting the path<\/li>\n<\/ul>\n<p>You can either write a program in IDE such as <a href=\"https:\/\/www.eclipse.org\/downloads\/packages\/release\/2021-09\/r\/eclipse-ide-java-developers\" target=\"_blank\" rel=\"noopener\">Eclipse<\/a> or you can simply write it in text editor\/Notepad save your code as a <strong>.java<\/strong> file. You can then compile java source code and execute your program in Command-Line. A detailed explanation is given below for this program.<\/p>\n<pre class=\"lang:default decode:true java\">\/\/ First java hello world program\r\npublic class HelloWorld {\r\n    public static void main(String[] args) {\r\n        System.out.println(\"Hello, World!\"); \r\n    }\r\n}\r\n<\/pre>\n<p>You need to save this file as <code>HelloWorld.java<\/code>.<\/p>\n<h2><span id=\"How_does_8220Java_program_to_print_Hello_World8221_works\">How does &#8220;Java program to print Hello World&#8221; works?<\/span><\/h2>\n<p>Let&#8217;s see a detailed explanation of the Hello world program.<\/p>\n<h3><span id=\"Class_declaration\">Class declaration<\/span><\/h3>\n<pre>\/\/ First java hello world program<\/pre>\n<p>This is a <a href=\"https:\/\/java2blog.com\/comments-java-code\/\" target=\"_blank\" rel=\"noopener noreferrer\">comment<\/a> in java and this statement will be ignored at run time.<\/p>\n<pre class=\"lang:java decode:1 \">public class HelloWorld {<\/pre>\n<p>As java is object-oriented programming, every java application should have a class definition.<\/p>\n<ul>\n<li><strong>Class declaration:<\/strong> A class declaration includes the name and visibility of the class.<code>class HelloWorld<\/code> is a declaration of class that includes the keyword, followed by an identifier <code>HelloWorld<\/code><\/li>\n<li>The class declaration is followed by curly braces <code>{}<\/code>, which define the class&#8217;s definition.<\/li>\n<\/ul>\n<h3><span id=\"Main_method\">Main method<\/span><\/h3>\n<pre>public static void main(String[] args) {<\/pre>\n<p>This is called the <a title=\"main method in java\" href=\"https:\/\/java2blog.com\/public-static-void-main-string-args-java-main-method\/\">main method in java<\/a>. This is the entry point for this program.<\/p>\n<p><code>public<\/code>: This is an <a href=\"https:\/\/java2blog.com\/access-modifiers-java\/\" target=\"_blank\" rel=\"noopener noreferrer\">access modifier<\/a> that is used to define visibility. Here the main method will be visible to all other classes.<br \/>\n<code>static<\/code>: static members belong to the class rather than any specific object. It simply means you can access the static members without creating any objects.<br \/>\n<code>void<\/code>: void is another keyword that defines the return type of the main method. It simply means the main method does not return anything before terminating the program.<br \/>\n<code>main<\/code>: <strong>main<\/strong> is the method name here.<br \/>\n<code>String args[]<\/code>: You can pass arguments to the java program using the args[] array. It is used to take user input using the command line.<\/p>\n<h3><span id=\"Print_statement\">Print statement<\/span><\/h3>\n<pre>System.out.println(\"Hello, World!\")<\/pre>\n<p><strong><a href=\"https:\/\/java2blog.com\/system-out-println-java\/\">System.out.println<\/a><\/strong> is used to print <code>literals<\/code> in double-quotes(<code>\"\"<\/code>) to console. As we have passed &#8220;Hello, World!&#8221; here, it will print <code>Hello, World!<\/code> to console.<\/p>\n<h3><span id=\"Semicolon\">Semicolon<\/span><\/h3>\n<p>As you can see, each statement is terminated with a <code>semicolon(;)<\/code>. You can put <a title=\"new lines\" href=\"https:\/\/java2blog.com\/new-line-character-java\/\">new lines<\/a> or spaces in the code but the statement has to be ended by a semicolon.<\/p>\n<h2><span id=\"Compile_and_run_the_program\">Compile and run the program<\/span><\/h2>\n<p>If you run this program in eclipse ide, you can simply right click and on <code>run as java application<\/code>.<br \/>\nYou can compile this java program using the command line as below:<br \/>\nOpen the command prompt and go to the location where you have saved <code>HelloWorld.java<\/code><\/p>\n<div style=\"background-color: black; color: white; padding: 10px;\">$ javac HelloWorld.java<\/div>\n<p>You can run the program using the command line as below:<\/p>\n<div style=\"background-color: black; color: white; padding: 10px;\">$ java HelloWorld<\/div>\n<p>When you run the above program, you will get the below output:<\/p>\n<div class=\"content-box-blue\">\n<pre class=\"lang:default decode:true\">Hello, World!<\/pre>\n<\/div>\n<p>When we execute a java program, we need to give the full class name without <code>.java<\/code> extension.<\/p>\n<h2><span id=\"In_how_many_ways_can_you_write_a_Java_Program\">In how many ways can you write a Java Program?<\/span><\/h2>\n<p>We can formulate different ways to write a Java program by doing some modifications to the main method&#8217;s signature. Below some examples are shown on how we can achieve this.<\/p>\n<h3><span id=\"1_By_Changing_the_Subscript_Notation_of_Input_Array_in_Main_Method\">1. By Changing the Subscript Notation of Input Array in Main Method<\/span><\/h3>\n<p>We can change the position of subscript notation of the input array passed in the main method for command-line input. We mean the <code>args[]<\/code> array&#8217;s subscript notation can be used after Data type String, before or after the variable name.<\/p>\n<p>To be more precise the above code for printing &#8220;Hello World!&#8221; can also be written in the following ways:<\/p>\n<pre class=\"lang:default decode:true\">public class HelloWorld {\r\n    public static void main(String args[]) {\r\n        System.out.println(\"Hello, World!\"); \r\n    }\r\n}\r\n<\/pre>\n<pre class=\"lang:default decode:true\">public class HelloWorld {\r\n    public static void main(String []args) {\r\n        System.out.println(\"Hello, World!\"); \r\n    }\r\n}\r\n<\/pre>\n<p>You see we have written the <strong>args<\/strong> array subscript notation before and after the variable name. The output for each case will be the same, we just change the signature of the main method.<\/p>\n<h3><span id=\"2_By_Changing_the_Sequence_of_Modifiers_in_the_method_without_modifying_Method_Prototype\">2. By Changing the Sequence of Modifiers in the method, without modifying Method Prototype<\/span><\/h3>\n<p>We can also write the program in alternate ways by changing the sequence of modifiers and keywords. Consequently changing the method declaration.<\/p>\n<p>In simple terms, we can interchange the position of keywords <strong>public<\/strong> and <strong>static,<\/strong> still the program would work fine. As the order of keywords does not matter. Likewise, we can write the above program as :<\/p>\n<pre class=\"lang:default decode:true\">public class HelloWorld {\r\n    static public void main(String []args) {\r\n        System.out.println(\"Hello, World!\"); \r\n    }\r\n}\r\n<\/pre>\n<p><strong>Note:<\/strong> The position of the return type keyword should not be mingled with. The return type of the method should always come after the modifiers and other keywords. Hence this an <strong>Invalid<\/strong> method declaration in java:<\/p>\n<p style=\"padding-left: 40px;\"><strong>public void static main(String args[])<\/strong><\/p>\n<h3><span id=\"3_By_Replacing_Array_Subscript_Notation_with_3_ellipses_Dots_for_var-args_support\">3. By Replacing Array Subscript Notation with 3 ellipses (Dots) for var-args support<\/span><\/h3>\n<p>We can also replace the Array Subscript notation in the <strong>args array <\/strong>and provide 3 dots instead for taking command line argument support. So instead of writing the main method like this: <strong>public static void main(String[] args)<\/strong><\/p>\n<p>We can alternatively write the whole program as:<\/p>\n<pre class=\"lang:default decode:true\">public class HelloWorld {\r\n    public static void main(String... args) {\r\n        System.out.println(\"Hello, World!\"); \r\n    }\r\n}<\/pre>\n<h2><span id=\"Resolving_Error_8220javac_is_not_recognized_as_an_internal_or_external_command8221\"><b>Resolving Error<\/b><b>: &#8220;javac is not recognized as an internal or external command&#8221;?<\/b><\/span><\/h2>\n<p>Suppose, while compiling the program shown in the above examples if you get the following error given below:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-17640 aligncenter\" src=\"https:\/\/java2blog.com\/wp-content\/uploads\/2017\/05\/path_second-300x123.png\" alt=\"\" width=\"520\" height=\"213\" srcset=\"https:\/\/java2blog.com\/wp-content\/uploads\/2017\/05\/path_second-300x123.png 300w, https:\/\/java2blog.com\/wp-content\/uploads\/2017\/05\/path_second.png 581w\" sizes=\"(max-width: 520px) 100vw, 520px\" \/><\/p>\n<p>So, if you encounter this error, it means that the Command Line or DOS does not recognize the command <strong>javac.<\/strong> To resolve this issue we need to set the path of Java Resources to the <strong>bin Directory <\/strong>of your Java Development Kit. It is a good practice to set the path but it is not required if you save all your java source files inside the <strong>bin<\/strong> Folder.<\/p>\n<h2><span id=\"Resolving_Error_8220reached_end_of_file_while_parsing8221\">Resolving Error: &#8220;reached end of file while parsing&#8221;<\/span><\/h2>\n<p>Again, while compiling any program due to some shallow mistakes, you might encounter the below given error:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-17641 aligncenter\" src=\"https:\/\/java2blog.com\/wp-content\/uploads\/2017\/05\/Screenshot-231-300x62.png\" alt=\"\" width=\"627\" height=\"130\" srcset=\"https:\/\/java2blog.com\/wp-content\/uploads\/2017\/05\/Screenshot-231-300x62.png 300w, https:\/\/java2blog.com\/wp-content\/uploads\/2017\/05\/Screenshot-231.png 683w\" sizes=\"(max-width: 627px) 100vw, 627px\" \/><\/p>\n<p>This is a common error in Java and you need not worry about this. To resolve this error you just need to check whether all the Curly Braces i.e. <code>{<\/code> and <code>}<\/code> are opened and closed properly.<\/p>\n<p>It is a good practice to check the braces while coding otherwise, for huge lines of code finding the missing braces can be a worrisome task.<\/p>\n<h2><span id=\"Exercise\">Exercise<\/span><\/h2>\n<p>You need to print <code>Yeah!! I executed my first java program<\/code> on console and name of the class should be <code>MyFirstJavaProgram<\/code><\/p>\n<div id=\"toc_container\">\n<div id=\"question1-link-2767\" class=\"sh-link question1-link sh-hide\"><a href=\"#\" onclick=\"showhide_toggle('question1', 2767, 'Show Answer', 'Hide Answers'); return false;\" aria-expanded=\"false\"><span id=\"question1-toggle-2767\">Show Answer<\/span><\/a><\/div><div id=\"question1-content-2767\" class=\"sh-content question1-content sh-hide\" style=\"display: none;\">\n<pre class=\"lang:java decode:1 \">public class MyFirstJavaProgram {\r\n    public static void main(String[] args) {\r\n        System.out.println(\"Yeah!! I executed my first java program\"); \r\n    }\r\n}\r\n<\/pre>\n<p>There are two changes with respect to our Hello world program.<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>The Name of the class is <code>MyFirstJavaProgram<\/code><\/li>\n<li>We are passing <code>Yeah!! I executed my first java program<\/code> to <a href=\"https:\/\/java2blog.com\/system-out-println-java\/\" target=\"_blank\" rel=\"noopener noreferrer\">System.out.println<\/a><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<h2><span id=\"Important_points\">Important points<\/span><\/h2>\n<p>Let&#8217;s go through some important points about <code>Hello world program<\/code>.<\/p>\n<ul>\n<li>Any java source file can have multiple classes but it can have only one public class.<\/li>\n<li>Java source file name should be the same as the public class name. That&#8217;s why I said above &#8220;you need to save this file as <code>HelloWorld.java<\/code>&#8220;.<\/li>\n<li>When you compile the java file, it is converted to byte code with <code>.class<\/code> extension, so you should be able to see <code>HelloWorld.class<\/code> in the directory.<\/li>\n<li>When you execute the program, <a href=\"https:\/\/java2blog.com\/java-virtual-machine-architecture\/\" target=\"_blank\" rel=\"noopener noreferrer\">JVM<\/a> looks for the <a href=\"https:\/\/java2blog.com\/public-static-void-main-string-args-java-main-method\/\" target=\"_blank\" rel=\"noopener noreferrer\">main method<\/a> and will execute it. You need to be very careful with the signature of the main method otherwise, the program may throw <code>java.lang.NoSuchMethodError: main.<\/code><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsIntroductionPrerequisite for running &#8220;Java Hello World Program&#8221;How does &#8220;Java program to print Hello World&#8221; works?Class declarationMain methodPrint statementSemicolonCompile and run the programIn how many ways can you write a Java Program?1. By Changing the Subscript Notation of Input Array in Main Method2. By Changing the Sequence of Modifiers in the method, without modifying [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[180],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/2767"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=2767"}],"version-history":[{"count":1,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/2767\/revisions"}],"predecessor-version":[{"id":25354,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/2767\/revisions\/25354"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=2767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=2767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=2767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}