Site icon C1CTech

Kotlin Hello World Program

<p class&equals;"p1">In this article&comma; We will learn how to start with Kotlin Programming and will write a small print &OpenCurlyDoubleQuote;Hello World” 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 Kotlin program&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Kotlin Editor<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1">To write your Kotlin 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; Android Studio or  IntelliJ IDEA&comma; which are particularly useful when managing larger collections of Kotlin files&period;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Android Studio<&sol;b><&sol;span> − An open-source and free IDE provided by Google which supports both Java and Kotlin language&period; It 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;developer&period;android&period;com&sol;studio">https&colon;&sol;&sol;developer&period;android&period;com&sol;studio<&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> − Its Java IDE earlier but now it also supports Kotlin 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><b>I<span style&equals;"color&colon; &num;0000ff&semi;">ntelliJ IDEA<&sol;span><&sol;b> − An IDE developed by JetBrains which supports Java&comma; Kotlin&comma; Android&comma; etc 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 <span style&equals;"color&colon; &num;008000&semi;"><b>IntelliJ IDEA <&sol;b><span style&equals;"color&colon; &num;000000&semi;">Community version<&sol;span><&sol;span><b>&period; <&sol;b>Now go to the above-mentioned link to download <b><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;000000&semi;">IntelliJ 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 it<&sol;span><&sol;span><b><span style&equals;"color&colon; &num;008000&semi;">&period;<&sol;span><&sol;b><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating New Project in IntelliJ IDEA<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong>1 &period;<&sol;strong> After installation the IntelliJ platform looks like this&colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1624" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;03&sol;Screenshot-2020-03-06-15&period;58&period;54&period;png" alt&equals;"Screenshot 2020-03-06 15&period;58&period;54" width&equals;"1552" height&equals;"968" &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-1625" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;03&sol;Screenshot-2020-03-06-15&period;59&period;45&period;png" alt&equals;"Screenshot 2020-03-06 15&period;59&period;45" width&equals;"1578" height&equals;"1088" &sol;><&sol;p>&NewLine;<p><strong>3<&sol;strong> &period; In the <span class&equals;"control">New Project<&sol;span> wizard&comma; select <strong><span class&equals;"control" style&equals;"color&colon; &num;008000&semi;">Kotlin<&sol;span><&sol;strong> from the list on the left and also select <span style&equals;"color&colon; &num;008000&semi;"><strong>JVM &vert;IDEA<&sol;strong><&sol;span>&period; Click <strong><span style&equals;"color&colon; &num;008000&semi;">Next<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1628" 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;21&period;55&period;png" alt&equals;"Screenshot 2020-03-06 16&period;21&period;55" width&equals;"1610" height&equals;"898" &sol;><&sol;p>&NewLine;<p><strong>4<&sol;strong> &period; <span style&equals;"color&colon; &num;008000&semi;"><strong>Name<&sol;strong><&sol;span> the project&comma; change the <strong><span style&equals;"color&colon; &num;008000&semi;">location<&sol;span><&sol;strong> of the project if you want&comma; provide project <strong><span style&equals;"color&colon; &num;008000&semi;">SDK<&sol;span><&sol;strong> path and click &&num;8216&semi;<strong><span style&equals;"color&colon; &num;008000&semi;">Finish<&sol;span><&sol;strong>&&num;8216&semi;&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 <span style&equals;"color&colon; &num;008000&semi;"><strong>Kotlin<&sol;strong><&sol;span> file right-click and then select <strong><span style&equals;"color&colon; &num;008000&semi;">New<&sol;span><&sol;strong> then <span style&equals;"color&colon; &num;008000&semi;"><strong>Kotlin File&sol;Class<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1629" 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;33&period;46&period;png" alt&equals;"Screenshot 2020-03-06 16&period;33&period;46" width&equals;"1270" height&equals;"374" &sol;><&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-1630" 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;36&period;25-3072816636-1583492840951&period;png" alt&equals;"Screenshot 2020-03-06 16&period;36&period;25" width&equals;"443" height&equals;"232" &sol;><&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><strong>7<&sol;strong>&period; Inside the <strong><span style&equals;"color&colon; &num;008000&semi;">HelloWorld&period;kt<&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 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Kotlin &&num;8220&semi;Hello&comma; World&excl;&&num;8221&semi; Program<&sol;b><&sol;span><&sol;p>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; println&lpar;"Hello&comma; World&excl;"&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;<p class&equals;"p1"><strong>8<&sol;strong> &period; Run the program using the <span style&equals;"color&colon; &num;008000&semi;"><strong>Run<&sol;strong><&sol;span> 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;"575" height&equals;"126" &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;<p><img class&equals;"alignnone size-full wp-image-1621" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;03&sol;Screenshot-2020-03-06-15&period;04&period;48&period;png" alt&equals;"Screenshot 2020-03-06 15&period;04&period;48" width&equals;"1512" height&equals;"410" &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 Kotlin&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>The main Method<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><strong>1<&sol;strong> &period; <strong><span style&equals;"color&colon; &num;008000&semi;">fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar;&lbrace;&&num;8230&semi;&rcub; <&sol;span><&sol;strong><&sol;p>&NewLine;<p>This is the main function&comma; which is mandatory in every Kotlin application&period; The Kotlin compiler starts executing the code from the main function&period;<&sol;p>&NewLine;<p>The function takes array of strings as a parameter and returns <span style&equals;"color&colon; &num;008000&semi;"><strong>Unit<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Unit<&sol;span><&sol;strong> is same as <strong><span style&equals;"color&colon; &num;008000&semi;">void<&sol;span><&sol;strong> in Java which means that this method doesn&&num;8217&semi;t return any value&period;<&sol;p>&NewLine;<p>The main method must have this exact signature in order to be able to run our program&period;<&sol;p>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; &period;&period;&period;&NewLine;&rcub;<&sol;pre>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Note&colon;<&sol;span> <&sol;strong>In Kotlin&comma; to define a function we have to use <strong><span style&equals;"color&colon; &num;0000ff&semi;">fun<&sol;span><&sol;strong> keyword which tells the compiler that it&&num;8217&semi;s a function&period;<&sol;p>&NewLine;<p><strong>2<&sol;strong> &period; <strong><span style&equals;"color&colon; &num;008000&semi;">println&lpar;&&num;8220&semi;Hello&comma; World&excl;&&num;8221&semi;&rpar;<&sol;span><&sol;strong><&sol;p>&NewLine;<p>The <span style&equals;"color&colon; &num;0000ff&semi;"><strong>println&lpar;&rpar;<&sol;strong><&sol;span> function is an upgraded version of <span style&equals;"color&colon; &num;008000&semi;"><strong>print&lpar;&rpar;<&sol;strong><&sol;span> which prints any argument passed to it and adds a new line to the output&period;<&sol;p>&NewLine;<p>In this program&comma; it will print <span style&equals;"color&colon; &num;008000&semi;"><strong>Hello&comma; World&excl;<&sol;strong><&sol;span> and add a new line&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Note&colon;<&sol;strong> <span style&equals;"color&colon; &num;000000&semi;">There is no need to end<&sol;span><&sol;span><span style&equals;"color&colon; &num;000000&semi;"> code statements with a semicolon&lpar; as we do in Java&rpar; because Kotlin performs this internally&period;<&sol;span><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3 class&equals;"p3"><span style&equals;"color&colon; &num;000080&semi;"><b>Comparison With Java &&num;8220&semi;Hello&comma; World&excl;&&num;8221&semi; program<&sol;b><&sol;span><&sol;h3>&NewLine;<p>To learn or compare how to write Hello World Program in Java refer this <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;getting-started-with-java&sol;">article<&sol;a><&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p><strong>1<&sol;strong> &period; Unlike Java&comma; it is not mandatory to create a class in every Kotlin program&period; It&&num;8217&semi;s because the Kotlin compiler creates the class for us&period;<&sol;p>&NewLine;<p>The name of the Kotlin class is &lpar;the name of the Kotlin file&rpar; &plus; suffix &lpar;Kt&rpar;&period; For example&comma; corresponding to <span style&equals;"color&colon; &num;0000ff&semi;"><strong>HelloWorld&period;kt <&sol;strong><&sol;span>file the generated class name is <strong><span style&equals;"color&colon; &num;008000&semi;">HelloWorldKt<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p class&equals;"p1"><strong>2<&sol;strong>&period; The <strong><span style&equals;"color&colon; &num;008000&semi;">println&lpar;&rpar;<&sol;span><&sol;strong> function calls <strong><span style&equals;"color&colon; &num;008000&semi;">System&period;out&period;println&lpar;&rpar;<&sol;span> <&sol;strong>internally&period;<&sol;p>&NewLine;<p class&equals;"p1">If you are using IntelliJ IDEA&comma; put your mouse cursor next to <strong><span style&equals;"color&colon; &num;008000&semi;">println<&sol;span><&sol;strong> and press&lpar; Shortcut&colon; <span style&equals;"color&colon; &num;0000ff&semi;"><b>Ctrl &plus; B<&sol;b><&sol;span>  For Mac&colon; <span style&equals;"color&colon; &num;0000ff&semi;"><b>Cmd &plus; B<&sol;b><&sol;span>&rpar;&comma; this will open <strong><span style&equals;"color&colon; &num;0000ff&semi;">Console&period;kt <&sol;span><&sol;strong>&lpar;declaration file&rpar;&period;<&sol;p>&NewLine;<p class&equals;"p1">In <span style&equals;"color&colon; &num;008000&semi;"><strong>Console&period;kt<&sol;strong><&sol;span> file you can see that <span style&equals;"color&colon; &num;008000&semi;"><strong>println&lpar;&rpar;<&sol;strong><&sol;span> function is internally calling <span style&equals;"color&colon; &num;008000&semi;"><strong>System&period;out&period;println&lpar;&rpar;<&sol;strong><&sol;span>&period;&NewLine;

Exit mobile version