{"id":792,"date":"2019-07-08T01:26:02","date_gmt":"2019-07-08T01:26:02","guid":{"rendered":"http:\/\/kabiliravi.com\/?page_id=792"},"modified":"2019-07-21T13:48:40","modified_gmt":"2019-07-21T13:48:40","slug":"get-started","status":"publish","type":"page","link":"http:\/\/kabiliravi.com\/index.php\/software\/programming\/my-java-tutorial\/get-started\/","title":{"rendered":"Get Started with HelloWorld and HelloMe"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"helloworld\">Write your first application called HelloWorld<\/h2>\n\n\n\n<p><a href=\"https:\/\/github.com\/myjavacoretutorial\/hello-world.git\">Find it in GitHub<\/a><\/p>\n\n\n\n<p>To start writing your first application in Java, the first thing you need to know is that Java is an Object Oriented Programming language and all codes that you write are encapsulated in a concept called <strong>class<\/strong>. What is class? A class is a blueprint or definition from which individual objects are created.  <\/p>\n\n\n\n<p>Since the purpose of this section is just a simple command line application to print &#8220;Hello World!&#8221; text in console, I stop talking about OOP, class, object and other language specific concept and I jump to explain how the skeleton of a simple Java application like a HelloWorld in Java is.<\/p>\n\n\n\n<p>Follow these steps to create your first application as HelloWorld:<\/p>\n\n\n\n1. Create a folder called <strong>java-practices<\/strong> or something like that in your home directory\n\n\n\n<pre class=\"wp-block-preformatted\">$ mkdir ~\/<strong>java-practices<\/strong><\/pre>\n\n\n\n<p>2. Inside <em>java-practic<\/em> folder create another folder called <strong>hello-world<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cd ~\/java-practices\n$ mkdir hello-world<\/pre>\n\n\n\n<p>3. Inside <em>hello-world<\/em> folder, create a file called <strong>HelloWorld.java<\/strong> using an editor like vi, vim or nano, emacs, atom, vscode, notepad, notepad++ based on your OS and your interest. Here I just used touch command to create an empty file in my OSX.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cd hello-world\n$ touch HelloWorld.java<\/pre>\n\n\n\n<p>4. Put following content into the created <strong>HelloWorld.java<\/strong> file. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class HelloWorld {\n public static void main(String[] args) {\n     System.out.println(\"Hello World !!!\");\n   }\n}<\/pre>\n\n\n\n<p>Here I used echo command to insert that content there<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ echo 'public class HelloWorld {\n public static void main(String[] args) {\n     System.out.println(\"Hello World !!!\");\n   }\n}\n' &gt;&gt; HelloWorld.java<\/pre>\n\n\n\n<p>5. Now using <strong>javac<\/strong> command which is one the command inside your &lt;JDK&gt;\/bin folder, you can compile <strong>.java<\/strong> files.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ javac HelloWorld.java<\/pre>\n\n\n\n<p>6. If you list the directory that you created <em>HelloWorld.java<\/em> file and ran <em>javac<\/em> to compile it, you will see a file with the extension of <strong>.class<\/strong> which is the compiled and binary of the above source code .<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ls\nHelloWorld.class HelloWorld.java<\/pre>\n\n\n\n<p>7. Now you can run your Hello World application using following command<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ java HelloWorld\nHello World !!!<\/pre>\n\n\n\n<p>You can find the whole code in <a href=\"https:\/\/github.com\/myjavacoretutorial\/hello-world.git\">My Java Core Tutorial<\/a> github repo <a href=\"https:\/\/github.com\/myjavacoretutorial\/hello-world.git\">here<\/a>:<br><a href=\"https:\/\/github.com\/myjavacoretutorial\/hello-world.git\">https:\/\/github.com\/myjavacoretutorial\/hello-world.git<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Define your fist class<\/h3>\n\n\n\n<p>The first line which you start defining a class you need at least two words: <strong>class<\/strong> and the <strong>class name<\/strong> in front of it. The <strong>public<\/strong> keyword is optional and at the end you open a curly brace <strong>{<\/strong> for class code block.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class HelloWorld {<\/pre>\n\n\n\n<p>In this line you have <strong>public<\/strong> keyword that indicates this class is a public class and can be accessible through all other classes even in different packages. To define a class you need to put <strong>class<\/strong> keyword before the <strong>class name<\/strong>. The next word is the <strong>class name<\/strong> that I named it <em>HelloWorld<\/em> in this example.<br>The <strong>class name<\/strong> should be the same as the <strong>.java<\/strong> file name that you define this class in it. <br>Usually Java programmers will follow <strong>Pascal Case<\/strong> naming convention to name the class names and Java file names. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><a href=\"http:\/\/wiki.c2.com\/?CamelCase\">Camel Case<\/a><\/strong>&nbsp;is the practice of writing phrases such that each word or abbreviation in the middle of the phrase begins with a&nbsp;capital letter, with no intervening spaces or punctuation.&nbsp;<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><a href=\"http:\/\/wiki.c2.com\/?PascalCase\">Pascal Case<\/a><\/strong><em>&nbsp;is a subset of <\/em><strong>Camel&nbsp;Case<\/strong><em>&nbsp;where the first letter is also capitalized.<\/em><\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Define <em>main<\/em> method<\/h3>\n\n\n\n<p>In the second line, you define the starting point of application which we call it <strong>main<\/strong> method. In Java syntax <strong>main<\/strong> method is a <strong>public<\/strong> and <strong>static<\/strong> method. It can accept an array of string as its inputs which maps to the command line arguments that you pass from command line when you execute your command line. <br>Refer to <a href=\"http:\/\/kabiliravi.com\/index.php\/software\/programming\/my-java-tutorial\/get-started\/#hellome\">HelloMe sample<\/a> to learn more. Finally it should return void. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public static void main(String[] args) {<\/pre>\n\n\n\n<p>Java is not like C\/C++ that its main method can also return an exit code as integer. In case you set something else, you can compile it using <strong>javac<\/strong> but you will receive this error, when you run it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ java HelloWorld\nError: Main method must return a value of type void in class HelloWorld, please\n define the main method as:\n    public static void main(String[] args)<\/pre>\n\n\n\n<p><strong>public<\/strong> keyword indicates that this method is publicly accessible. If you missed to put <strong>public<\/strong> keyword to define your <strong>main<\/strong> method you will get following error:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ java HelloWorld\nError: Main method not found in class HelloWorld, please define the main method as:\n    public static void main(String[] args)\n or a JavaFX application class must extend javafx.application.Application<\/pre>\n\n\n\n<p><br>When you want to define a method to be accessible without necessarily instantiating the class that owns that method, such a utility method to convert a date to string or vice versa, you need to make that method <strong>static<\/strong>. Learn more about static method here.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ java HelloWorld\nError: Main method is not static in class HelloWorld, please define the main method as:\n    public static void main(String[] args)<\/pre>\n\n\n\n<p>As I mentioned above, the <strong>main<\/strong> method should also have an argument of <strong>String array <\/strong>(String[]). The name of the argument can be <strong>anything<\/strong> you like, however usually the java codes use <strong>args<\/strong>. But for type of the argument, if you define something else, for example if you miss to put arrays braces after String type, <em>java<\/em> says, it cannot find the <em>main<\/em> method, you will get following error:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ java HelloWorld\nError: Main method not found in class HelloWorld, please define the main method as:\n    public static void main(String[] args)\n or a JavaFX application class must extend javafx.application.Application<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Print text in console<\/h2>\n\n\n\n<p>The third line is helping you to print &#8220;Hello World&#8221; in the console. Using a static method called <strong>System.out.println<\/strong>. Learn more about <strong>System.out.println<\/strong> here.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"hellome\">Hello to yourself in your second practice<\/h2>\n\n\n\n<p>In this example, you simply put your name as the input argument of your java application command line tool and it prints<strong> Hello Your Name<\/strong> in the console.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ java HelloMe Nasim\nHello Nasim<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>To start learning the core functionality of a programming language, I highly recommend you to chose an editor such as <a href=\"https:\/\/atom.io\/\">atom<\/a> or <a href=\"https:\/\/code.visualstudio.com\/\">vscode<\/a> and NOT an IDE such as <a href=\"https:\/\/www.jetbrains.com\/idea\/download\/\">IntelliJ Idea<\/a> or <a href=\"https:\/\/www.eclipse.org\/downloads\/\">Eclipse<\/a>.<\/p><\/blockquote>\n\n\n\n<p>Here are the steps:<\/p>\n\n\n\n<p>1. Create a folder. You can call it<strong> hello-me<\/strong> and change your current directory to it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ mdkir hello-me\n$ cd hello-me<\/pre>\n\n\n\n<p>2. Here I used <strong>atom<\/strong> editor. Under hello-me folder run following command to open atom with having the current folder as an <em>atom<\/em> project.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">~\/hello-me\/$ atom .<\/pre>\n\n\n\n<p>And you will see your project folder structure in <em>atom<\/em> editor like this: <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"737\" src=\"http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-1024x737.png\" alt=\"\" class=\"wp-image-895\" srcset=\"http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-1024x737.png 1024w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-300x216.png 300w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-768x553.png 768w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-210x151.png 210w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-1536x1105.png 1536w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image.png 1540w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>3. Create a file called HelloMe.java. Right click, select New File menu <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"330\" src=\"http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-2-1024x330.png\" alt=\"\" class=\"wp-image-898\" srcset=\"http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-2-1024x330.png 1024w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-2-300x97.png 300w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-2-768x247.png 768w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-2-210x68.png 210w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-2-1536x495.png 1536w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-2.png 1546w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>and enter HelloMe.java in &#8220;Enter the path for the new file&#8221; text input<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"225\" src=\"http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-1-1024x225.png\" alt=\"\" class=\"wp-image-897\" srcset=\"http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-1-1024x225.png 1024w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-1-300x66.png 300w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-1-768x168.png 768w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-1-210x46.png 210w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-1.png 1532w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>4. Type following content there:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class HelloMe {\n     public static void main(String[] args) {\n         System.out.println(\"Hello \" + args[0]);\n     }\n }<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"272\" src=\"http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-3-1024x272.png\" alt=\"\" class=\"wp-image-901\" srcset=\"http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-3-1024x272.png 1024w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-3-300x80.png 300w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-3-768x204.png 768w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-3-210x56.png 210w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-3-1536x408.png 1536w, http:\/\/kabiliravi.com\/wp-content\/uploads\/2019\/07\/image-3.png 1542w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>As you see, I defined a <em>public class<\/em> called HelloMe which is the same as java file HelloMe.java. It has a main method that should be <em>public static <\/em>and it does not return anything and it has <em>void<\/em> type for its return type. It also has an argument of String[] (string array) and I called that argument as <strong>args<\/strong>.<br>In the body of the <em>main<\/em> method, I am printing the word <strong>Hello<\/strong> with a space after that as &#8220;Hello &#8221; and I concat this string with the first element of <strong>args array<\/strong>. The <em>args array<\/em>, as I mentioned before, holds the arguments that you pass to your application as java command line arguments. Like this<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> $ java HelloMe arg1 arg2 arg3 argN<\/pre>\n\n\n\n<p>In this example, I just pick the first element of args which is the index 0 and it is reachable by<em> args[0]<\/em> and I contacted it with &#8220;Hello &#8221; and I print the whole contacted string to console. Here is the code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">System.out.println(\"Hello \" + args[0]);<\/pre>\n\n\n\n<p>5. Now using <strong>javac<\/strong> command compile <em>HelloMe.java<\/em> file and it will generate a <strong>.class <\/strong>file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ javac HelloMe.java\n$ ls\nHelloMe.class HelloMe.java<\/pre>\n\n\n\n<p>6. Using following <strong>java<\/strong> command, you can run your HelloMe command line alication, but this time you need to pass your name or somebody&#8217;s name or some text at least to run it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ java HelloMe Nasim\nHello Nasim<\/pre>\n\n\n\n<p>Otherwise, if you just run your application without passing anything, you will get an exception of <em>java.lang.ArrayIndexOutOfBoundsException<\/em> in line 3, because your application tries to get the zero index element value from args array using <em>args[0] <\/em>and since your args array is empty because you did not pass any input argument to your command, it will throw &#8220;java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0&#8221;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ java HelloMe\nException in thread \"main\" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0\n     at HelloMe.main(HelloMe.java:3)<\/pre>\n\n\n\n<p>You can play with whatever you learned here, print some random text or think about another simple console or command line application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write your first application called HelloWorld Find it in GitHub To start writing your first application in Java, the first thing you need to know is that Java is an Object Oriented Programming language and all codes that you write are encapsulated in a concept called class. What is class? A class is a blueprint [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":596,"menu_order":4,"comment_status":"open","ping_status":"closed","template":"","meta":{"ngg_post_thumbnail":0,"footnotes":""},"class_list":["post-792","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/comments?post=792"}],"version-history":[{"count":79,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/792\/revisions"}],"predecessor-version":[{"id":909,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/792\/revisions\/909"}],"up":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/596"}],"wp:attachment":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/media?parent=792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}