Core JAVA

Hello World Example in Java

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">In this tutorial&comma; we will discuss how to write the hello world java program&period; Creating hello world java example is too easy&period; Here&comma; we have created a class named&nbsp&semi;<b>MyFirstJavaProgram&nbsp&semi;<&sol;b>that contains only main method and prints a message &&num;8220&semi;<b>hello world<&sol;b>&&num;8220&semi;&period; It is the simple program of java&period; <&sol;div>&NewLine;<p>&NewLine;<b>Requirement for Hello World Java Example&colon;<&sol;b><&sol;p>&NewLine;<p><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>create the java program&period;<&sol;li>&NewLine;<li>install the JDK if you don&&num;8217&semi;t have installed it&comma; <a href&equals;"http&colon;&sol;&sol;www&period;oracle&period;com&sol;technetwork&sol;java&sol;javase&sol;downloads&sol;index&period;html" target&equals;"&lowbar;blank"><b>download the JDK<&sol;b><&sol;a> and install it&period;<&sol;li>&NewLine;<li>set path of the bin directory under jdk&period;<&sol;li>&NewLine;<li>compile and execute the program&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p>&NewLine; <b>Creating hello java example&colon;<&sol;b><br &sol;>&NewLine;Let&&num;8217&semi;s create the hello world java program&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">public class MyFirstJavaProgram &lbrace; &NewLine; &NewLine; &sol;&ast; This is my first java program&period; &NewLine; &ast; This will print 'Hello World' as the output &NewLine; &ast;&sol; &NewLine; &NewLine; public static void main&lpar;String &lbrack;&rsqb;args&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;"Hello World"&rpar;&semi; &sol;&sol; prints Hello World &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p>&NewLine;Lets look at how to save the file&comma; compile and run the program&period; Please follow the steps given below&colon;<&sol;p>&NewLine;<p><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>Open notepad and add the code as above&period;<&sol;li>&NewLine;<li>Save the file as &colon; <b><i>MyFirstJavaProgram&period;java&period;<&sol;i><&sol;b><&sol;li>&NewLine;<li>Open a command prompt window and go o the directory where you saved the class&period; Assume its&nbsp&semi;F&colon;Javas&period;<&sol;li>&NewLine;<li>Type &&num;8216&semi; <b><i>javac MyFirstJavaProgram&period;java <&sol;i><&sol;b>&&num;8216&semi; and press enter to compile your code&period; If there are no errors in your code the command<&sol;li>&NewLine;<li>prompt will take you to the next line&period;&lpar; Assumption &colon; The path variable is set&rpar;&period;<&sol;li>&NewLine;<li>Now type &&num;8216&semi; <b><i>java MyFirstJavaProgram<&sol;i><&sol;b> &&num;8216&semi; to run your program&period;<&sol;li>&NewLine;<li>You will be able to see &&num;8216&semi; <b>Hello World<&sol;b> &&num;8216&semi; printed on the window&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi;"><b>output&colon;<&sol;b><br &sol;>&NewLine;&gt&semi;<br &sol;>&NewLine;C &colon; &gt&semi; javac MyFirstJavaProgram&period;java<br &sol;>&NewLine;C &colon; &gt&semi; java MyFirstJavaProgram <br &sol;>&NewLine;<b>Hello World<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<p>&NewLine;<b>Understanding first java program&colon;<&sol;b><br &sol;>&NewLine;Let&&num;8217&semi;s see what is the meaning of <b>class&comma; public&comma; static&comma; void&comma; main&comma; String&lbrack;&rsqb;&comma; System&period;out&period;println&lpar;&rpar;<&sol;b>&period;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b>class <&sol;b>is used to declare a class in java&period;<&sol;li>&NewLine;<li><b>public <&sol;b>is an access modifier which represents visibility&comma; it means it is visible to all&period;<&sol;li>&NewLine;<li><b>static <&sol;b>is a keyword&comma; if we declare any method as static&comma; it is known as static method&period; The core advantage of static method is that there is no need to create object to invoke the static method&period; The main method is executed by the JVM&comma; so it doesn&&num;8217&semi;t require to create object to invoke the main method&period; So it saves memory&period;<&sol;li>&NewLine;<li><b>void <&sol;b>is the return type of the method&comma; it means it doesn&&num;8217&semi;t return any value&period;<&sol;li>&NewLine;<li><b>main <&sol;b>represents startup of the program&period;<&sol;li>&NewLine;<li><b>String&lbrack;&rsqb; args<&sol;b> is used for command line argument&period; We will learn it later&period;<&sol;li>&NewLine;<li><b>System&period;out&period;println&lpar;&rpar;<&sol;b> is used print statement&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" height&equals;"289" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;02&sol;hellojava&period;png" width&equals;"600"&sol;><&sol;div>&NewLine;<p>&NewLine;<&sol;div>&NewLine;<p>&NewLine;As displayed in the above diagram&comma; write the <b>MyFirstJavaProgram<&sol;b> program of java in notepad and saved it as <b>MyFirstJavaProgram&period;java&period; <&sol;b>To compile and run this program&comma; you need to open command prompt by <b>Start -&gt&semi; All Programs -&gt&semi; Accessories -&gt&semi; command prompt<&sol;b>&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" height&equals;"292" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;02&sol;hellojava1&period;png" width&equals;"600"&sol;><&sol;div>&NewLine;<&sol;div>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-basic-syntax&sol;">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;variable-and-datatype-in-java&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-basic-syntax&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;variable-and-datatype-in-java&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '564'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago