Beginning Java programming with Hello World

Java8Java Programming Object Oriented ProgrammingJava Regex

<p style="">Let us look at a simple code that will print the words <em><strong>Hello World.</strong></em></p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/5AD3Ad" rel="nofollow" target="_blank">Live Demo</a></p><pre class="prettyprint notranslate">public class MyFirstJavaProgram { &nbsp; &nbsp;/* This is my first java program. &nbsp; &nbsp; &nbsp; &nbsp;* This will print &#39;Hello World&#39; as the output &nbsp; &nbsp; &nbsp; &nbsp;*/ &nbsp; &nbsp;public static void main(String []args) { &nbsp; &nbsp; &nbsp; System.out.println(&quot;Hello World&quot;); // prints Hello World &nbsp; &nbsp;} }</pre><p>Let&#39;s look at how to save the file, compile, and run the program. Please follow the subsequent steps &minus;</p><ul class="list"><li><p>Open notepad and add the code as above.</p></li><li><p>Save the file as MyFirstJavaProgram.java.</p></li><li><p>Open a command prompt window and go to the directory where you saved the class. Assume it&#39;s C:\.</p></li><li><p>Type &#39;javac MyFirstJavaProgram.java&#39; and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set).</p></li><li><p>Now, type &#39;java MyFirstJavaProgram&#39; to run your program.</p></li><li><p>You will be able to see &#39;Hello World&#39; printed on the window.</p></li></ul><h2>Output</h2><pre class="result notranslate">C:\&gt; javac MyFirstJavaProgram.java C:\&gt; java MyFirstJavaProgram &nbsp; Hello World</pre><h2>Basic Syntax</h2><p>About Java programs, it is very important to keep in mind the following points.</p><ul class="list"><li><p><strong>Case Sensitivity</strong> &minus; Java is case sensitive, which means identifier <strong>Hello</strong> and<strong>&nbsp;hello</strong> would have a different meaning in Java.</p></li><li><p><strong>Class Names</strong> &minus; For all class names, the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word&#39;s first letter should be in Upper Case.</p><p><strong>Example:</strong> <em>class MyFirstJavaClass</em></p></li><li><p><strong>Method Names</strong> &minus; All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word&#39;s first letter should be in Upper Case.</p><p><strong>Example:</strong><em>&nbsp;public void myMethodName()</em></p></li><li><p><strong>Program File Name</strong> &minus; Name of the program file should exactly match the class name.</p><p>When saving the file, you should save it using the class name (Remember Java is case sensitive) and append &#39;.java&#39; to the end of the name (if the file name and the class name do not match, your program will not compile).</p><p><strong>Example:</strong> Assume &#39;MyFirstJavaProgram&#39; is the class name. Then the file should be saved as &#39;<em>MyFirstJavaProgram.java&#39;</em></p></li><li><p><strong>public static void main(String args[])&nbsp;</strong>&minus; Java program processing starts from the main() method which is a mandatory part of every Java program.</p></li></ul>
raja
Updated on 18-Jun-2020 12:02:35

Advertisements