Site icon C1CTech

Getting started with Java

<p>In this article&comma; We will learn how to start with Java Programming and will write a small print &&num;8220&semi;Hello World&&num;8221&semi; program&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Steps&colon;<&sol;strong> <&sol;span><&sol;h3>&NewLine;<ol>&NewLine;<li>IDE &lpar;Java Editor&rpar; installation<&sol;li>&NewLine;<li>Writing Hello World Java program&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Java Editor<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1">To write Java programs&comma; you will need a text editor&period; There are different IDEs &lpar;Integrated Development Environment&rpar; available in the market like Eclipse&comma; Netbeans or  IntelliJ IDEA&comma; which are particularly useful when managing larger collections of Java files&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Netbeans<&sol;b><&sol;span> − A Java IDE that is open-source and free which can be downloaded from <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;www&period;netbeans&period;org&sol;index&period;html"><span class&equals;"s2">https&colon;&sol;&sol;www&period;netbeans&period;org&sol;index&period;html<&sol;span><&sol;a><&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Eclipse<&sol;b><&sol;span> − A Java IDE developed by the eclipse open-source community and can be downloaded from <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;www&period;eclipse&period;org&sol;"><span class&equals;"s2">https&colon;&sol;&sol;www&period;eclipse&period;org&sol;<&sol;span><&sol;a><&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><b>IntelliJ IDEA<&sol;b> <&sol;span>− A Java IDE developed by JetBrains and it&&num;8217&semi;s free and open-source which can be downloaded from <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;www&period;jetbrains&period;com&sol;idea&sol;">https&colon;&sol;&sol;www&period;jetbrains&period;com&sol;idea&sol;<&sol;a><&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>I will prefer to use the <span style&equals;"color&colon; &num;008000&semi;"><b><span style&equals;"color&colon; &num;008000&semi;">IntelliJ IDEA <&sol;span> <&sol;b><span style&equals;"color&colon; &num;333333&semi;">Community version<&sol;span><&sol;span><b>&period; <&sol;b>Now go to the above-mentioned link to download <strong>IntelliJ<&sol;strong><b><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;000000&semi;"> IDEA<&sol;span><&sol;span><&sol;b><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;000000&semi;"> and install<&sol;span><&sol;span><span style&equals;"color&colon; &num;008000&semi;">&period;<&sol;span><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating a New Project in IntelliJ IDEA<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong>1<&sol;strong>&period; After installation the IntelliJ platform looks like this&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1605" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;03&sol;Screenshot-2020-03-04-12&period;20&period;31-1105199846-1583316146673&period;png" alt&equals;"Screenshot 2020-03-04 12&period;20&period;31" width&equals;"1322" height&equals;"954" &sol;><&sol;p>&NewLine;<p><strong>2<&sol;strong>&period; Click on <span style&equals;"color&colon; &num;008000&semi;"><strong>Create New Project<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1606" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;03&sol;Screenshot-2020-03-04-12&period;21&period;23-239678958-1583316194966&period;png" alt&equals;"Screenshot 2020-03-04 12&period;21&period;23" width&equals;"2407" height&equals;"1435" &sol;><&sol;p>&NewLine;<p><strong>3<&sol;strong>&period; I am going to create Java projects so I will select <strong><span style&equals;"color&colon; &num;008000&semi;">Java<&sol;span><&sol;strong> from the available left side options and then click <strong><span style&equals;"color&colon; &num;008000&semi;">Next<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p><strong>4<&sol;strong> &period; Now give your project a name and then click on <strong><span style&equals;"color&colon; &num;008000&semi;">finish<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p><strong>5<&sol;strong> &period; Inside the <span style&equals;"color&colon; &num;008000&semi;"><strong>src<&sol;strong><&sol;span> folder&comma; to create a new java file right-click and then select <strong><span style&equals;"color&colon; &num;008000&semi;">New<&sol;span><&sol;strong> then <strong><span style&equals;"color&colon; &num;008000&semi;">Java Class<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1631" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;03&sol;Screenshot-2020-03-06-16&period;35&period;32&period;png" alt&equals;"Screenshot 2020-03-06 16&period;35&period;32" width&equals;"559" height&equals;"203" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><strong>6<&sol;strong>&period; Now named the class which you want In my case I will name it as <span style&equals;"color&colon; &num;008000&semi;"><strong>HelloWorld<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1632" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;03&sol;Screenshot-2020-03-06-17&period;05&period;36-3294088077-1583494606254&period;png" alt&equals;"Screenshot 2020-03-06 17&period;05&period;36" width&equals;"443" height&equals;"232" &sol;><&sol;p>&NewLine;<p><strong>7<&sol;strong>&period; Inside the <strong><span style&equals;"color&colon; &num;008000&semi;">HelloWorld&period;java<&sol;span><&sol;strong> file&comma; I will write the following code to print &&num;8220&semi;Hello&comma; World&excl;&&num;8221&semi; to the screen&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><b>Java &&num;8220&semi;Hello&comma; World&excl;&&num;8221&semi; Program<&sol;b><&sol;span><&sol;p>&NewLine;<pre>public class HelloWorld &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"Hello&comma; World&excl;"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p><strong>8<&sol;strong> &period; Run the program using the <strong><span style&equals;"color&colon; &num;008000&semi;">Run<&sol;span><&sol;strong> button&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1614" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;03&sol;Screenshot-2020-03-06-12&period;21&period;31&period;png" alt&equals;"Screenshot 2020-03-06 12&period;21&period;31" width&equals;"548" height&equals;"121" &sol;><&sol;p>&NewLine;<p>When you run the program&comma; You can see the output of the program in the Console screen&comma; the output will be&colon;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<p><img class&equals;"alignnone wp-image-1616" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;03&sol;Screenshot-2020-03-06-12&period;45&period;56&period;png" alt&equals;"Screenshot 2020-03-06 12&period;45&period;56" width&equals;"594" height&equals;"166" &sol;><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Understanding Code&colon;<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>Let&&num;8217&semi;s understand how &&num;8220&semi;Hello&comma; World&excl;&&num;8221&semi; program works in Java&period;<&sol;p>&NewLine;<p><strong>1<&sol;strong> &period; <span style&equals;"color&colon; &num;008000&semi;"><strong>public class HelloWorld &lbrace; &&num;8230&semi; &rcub;<&sol;strong><&sol;span><&sol;p>&NewLine;<p>In Java&comma; every application begins with a class definition&period; In the program&comma; <strong><span style&equals;"color&colon; &num;008000&semi;">HelloWorld <&sol;span><&sol;strong><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;000000&semi;">is the name of the class and class definition is&colon;<&sol;span><&sol;span>&period;<&sol;p>&NewLine;<pre>public class HelloWorld &lbrace;&NewLine;&period;&period;&period;&period;&NewLine;&rcub;<&sol;pre>&NewLine;<p>For now&comma; just remember that every Java application has a class definition&comma; and the name of the class should match the filename in Java&period;<&sol;p>&NewLine;<p>This above line declares a class named <strong><span style&equals;"color&colon; &num;008000&semi;">HelloWorld<&sol;span><&sol;strong>&comma; which is public&comma; which means that any other class can access it&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Notes&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<ol>&NewLine;<li>A class should always start with an uppercase first letter&period; For example&comma; if you want to name your class as helloworldexample you have to write like this <strong><span style&equals;"color&colon; &num;008000&semi;">HelloWorldExample<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<li>Java is <strong><span style&equals;"color&colon; &num;008000&semi;">case-sensitive<&sol;span><&sol;strong>&colon; means It will treat &&num;8220&semi;HelloWorld&&num;8221&semi; and &&num;8220&semi;helloworld&&num;8221&semi; differently&period;<&sol;li>&NewLine;<li class&equals;"p1">When we declare a public class &lpar;HelloWorld&rpar;&comma; we must declare it inside a file with the same name &lpar;HelloWorld&period;java&rpar;&comma; otherwise we&&num;8217&semi;ll get an error when compiling&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<h4><&sol;h4>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>The main Method<&sol;strong><&sol;span><&sol;h4>&NewLine;<p class&equals;"p1"><strong>2<&sol;strong> &period; <span style&equals;"color&colon; &num;008000&semi;"><strong>public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &rcub;<&sol;strong><&sol;span><&sol;p>&NewLine;<p class&equals;"p1">This is the main method&period; Every application in Java must contain the main method&period;<&sol;p>&NewLine;<p class&equals;"p1">The main method is the entry point of our Java program which means It tells the compiler to start executing the code from here&period;<&sol;p>&NewLine;<p class&equals;"p1">The main method must have this exact signature in order to be able to run our program&period;<&sol;p>&NewLine;<pre>public static void main&lpar;String&period;&period;&period; args&rpar;&lbrace;&NewLine; &period;&period;&period;&NewLine;&rcub;<&sol;pre>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;008000&semi;"><strong>public<&sol;strong><&sol;span> means that anyone can access it&period;<&sol;li>&NewLine;<li class&equals;"li1"><strong><span style&equals;"color&colon; &num;008000&semi;">static<&sol;span><&sol;strong> means that you can run this method without creating an instance of HelloWorld class&period;<&sol;li>&NewLine;<li class&equals;"li1"><strong><span style&equals;"color&colon; &num;008000&semi;">void<&sol;span><&sol;strong> means that this method doesn&&num;8217&semi;t return any value&period;<&sol;li>&NewLine;<li class&equals;"li1"><strong><span style&equals;"color&colon; &num;008000&semi;">main<&sol;span><&sol;strong> is the name of the method&period;<&sol;li>&NewLine;<li>The main method takes <strong><span style&equals;"color&colon; &num;008000&semi;">array of String<&sol;span><&sol;strong> as an argument which you can specify with any name and with any of the following syntax &colon;<&sol;li>&NewLine;<&sol;ul>&NewLine;<pre>String&lbrack;&rsqb; args or<&sol;pre>&NewLine;<pre>String args&lbrack;&rsqb;<&sol;pre>&NewLine;<p><strong>3<&sol;strong> &period; <strong><span style&equals;"color&colon; &num;008000&semi;">System&period;out&period;println&lpar;&&num;8220&semi;Hello World&&num;8221&semi;&rpar;&semi;<&sol;span><&sol;strong><&sol;p>&NewLine;<p>The following code prints the string inside quotation marks <strong><span style&equals;"color&colon; &num;0000ff&semi;">Hello&comma; World&excl;<&sol;span><&sol;strong> to standard output &lpar;your screen&rpar;&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><strong><span style&equals;"color&colon; &num;008000&semi;">System<&sol;span><&sol;strong> is a pre-defined class that Java provides us and it holds some useful methods and variables&period;<&sol;li>&NewLine;<li class&equals;"li1"><strong><span style&equals;"color&colon; &num;008000&semi;">out<&sol;span><&sol;strong> is a static variable within <span style&equals;"color&colon; &num;008000&semi;"><strong>System<&sol;strong><&sol;span> that represents the output of your program &lpar;stdout&rpar;&period;<&sol;li>&NewLine;<li class&equals;"li1"><strong><span style&equals;"color&colon; &num;008000&semi;">println <&sol;span><&sol;strong>method is an upgraded version of <strong><span style&equals;"color&colon; &num;008000&semi;">print&lpar;&rpar;<&sol;span><&sol;strong> inside <strong><span style&equals;"color&colon; &num;008000&semi;">PrintStream<&sol;span><&sol;strong> which prints any argument passed to it on the screen and adds a new line to the output&period; In this program&comma; it will print Hello&comma; World&excl; and add a new line&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Note&colon;<&sol;strong> <&sol;span>Each code statement must end with a semicolon&period;<&sol;p>&NewLine;<p>There is no need to worry about <strong><span style&equals;"color&colon; &num;008000&semi;">class&comma; static&comma; modifier&lpar;public&rpar;<&sol;span><&sol;strong> we will talk about it later in detail&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Conclusion<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>For now&comma; just remember the below points&colon;<&sol;p>&NewLine;<p>1 &period;Every Java application has a class definition&comma; and if the class is public then it is mandatory that the name of the class should match with the Java file name&period;<&sol;p>&NewLine;<p>2 &period;Every application in Java must contain the main method which tells the compiler to start executing the code from here&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version