You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
11 KiB

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Learning Website</title>
<link href="/styles/styles.css" rel="stylesheet" type="text/css">
<link href="/android/styles/styles.css" rel="stylesheet" type="text/css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="banner">
<h1 class="courselink"><a href="https://www.linkedin.com/learning/kotlin-essential-training-2018">Kotlin Essential Training</a></h1>
<h2 class="lecturer">LinkedIn Learning : David Gassner : March 2018</h2>
<h2 class="episodetitle">Chapter 1 - Setting Up the Development Environment</h2>
</div>
<article>
<h2 class="sectiontitle">Create Your First Kotlin Project</h2>
<p>There are no major differences between creating a Java project and creating a Kotlin project but there are a couple of things worth mentioning</p>
<p>When you select File &gt; New &gt; Project, you can then select Kotlin project but you also have several further options, so you can create more than one different type of Kotlin project.</p>
<p>At the moment, there are three options available in the version I am running and these are Kotlin/JVM, Kotlin JS and Kotlin (Multiplatform -Experimental). At this stage, I’m not completely sure of the difference but it seems that the first is going to create an app that will run on any Java machine and I guess that the second is going to be used for creating a web application.</p>
<p>For the time being and until I know the difference between these I will work on the assumption that the first option is the one needed and that when I study Android Development with Kotlin, either the difference will become apparent or I will just continue with this option.</p>
<p>Adding a Kotlin class is also similar to the process we would see with Java – that is, right click where you want to place the file which will be either in the src folder or in a package.</p>
<p>One difference (as far as I’m aware) is that in the new class we can type main to generate a template main class which is quite cool.</p>
<h2 class="sectiontitle">Check Out the Exercise Files From GitHub</h2>
<p>First, go to the <a href="https://github.com/davidgassner/KotlinEssentialTraining">GitHub page</a> and copy the URL (ensuring that the Master Branch has been selected). Next, go to IntelliJ, select New on the File menu and then Project from Version Control and finally GitHub. You can log into GitHub via the dialog box that appears and select either login from password or login from token, I’m not sure about the login from token option so I used my password.</p>
<figure>
<img src="images/figure1.png" alt="">
<figcaption>Figure 1 - Clone Repository Dialog Box</figcaption>
</figure>
<p>Figure 1 shows what the dialog box looks like after you have logged in. Note that the repository URL has been pasted in here and the parent directory and directory name specify where the projects will be saved on the local machine.</p>
<p>Changes to the files can be committed locally but with the exercise files, we won’t be able to push these back to the repository since we are not authorised to do that. Commit is performed by clicking on Version Control (bottom left) and clicking the commit button which has (on my version) the letters VCS in the icon. From here, we can add a commit message and select Commit or Commit &amp; Push (which won’t work in this case!). There is also a Create Patch option which I will ignore for now.</p>
<p>If we want to check out the exercise file for a particular chapter, these are all separate branches in the repository. We can select a new branch in a couple of ways. Firstly, we can click on Branch Master in the bottom right corner and a list of available branches will appear (see figure 2).</p>
<figure>
<img src="images/figure2.png" alt="">
<figcaption>Figure 2 - list of branches under the branch master</figcaption>
</figure>
<p>Alternatively, we can select the VCS menu followed by Git and then Branches and this will bring up the same list (it may be necessary to expand the Remote Branches to actually see the list. The numbering system is the same as for any Lynda video course so, for instance, the branch to obtain the file for chapter 4 part 2 will be origin/04_02 or origin/04_02_e (I guess these are the starting point and the completed file for each exercise).</p>
<h2 class="sectiontitle">Use Command Line Arguments</h2>
<figure>
<pre class="inset">
fun main(args: Array&lt;String&gt;) {
println("Welcome to Kotlin Essential Training!")
}</pre>
<figcaption>Figure 3 - Example of a main function</figcaption>
</figure>
<figure>
<img src="images/figure4.png" alt="">
<figcaption>Figure 4 - The Edit Configurations Wizard</figcaption>
</figure>
<figure>
<pre class="inset">
fun main(args: Array&lt;String&gt;) {
println("Hi, " + args[0] + ". Welcome to Kotlin Essential Training!")
}</pre>
<figcaption>Figure 5 - The main function showing how an argument passed to it via the runtime configuration is used</figcaption>
</figure>
<p>In figure 4, we can see the Edit Configuration wizard and note that I have added a string as a program argument.</p>
<p>Bearing in mind the fact that the arguments passed to the function are in the form of an array, in this case called args[] (note, that it is conventional but not compulsory to call this array args), within the program the argument is accessed using simple array syntax and we can see this in figure 5 with the reference to args[0].</p>
<p>Note that the output is generated using a conventional form of string concatenation which will be familiar to Java programmers. However, Kotlin actually provides a slightly more elegant alternative to string concatenation and that is the string template. In essence it means that we can incorporate our string variable within the output string simply by referencing it with a dollar sign followed by the string reference inside a pair of braces.</p>
<figure>
<pre class="inset">
fun main(args: Array&lt;String&gt;) {
println("Hi, ${args[0]}. Welcome to Kotlin Essential Training!!!")
}</pre>
<figcaption>Figure 6 - The same function as the one in figure 5 but using a string template in place of string concatenation</figcaption>
</figure>
<p>If we compare figures 5 and 6, both show essentially the same main function, but the one in figure 6 makes use of the string template. Note that this syntax is not specific to an argument passed to the program. The only requirement is that the contents of the braces returns a string. You can even put a string literal in there (so we would have something like ${“Philip”} and it would work although this is obviously pointless!</p>
<p>We can of course use a simple variable name here and it is worth noting that if we do, the braces can be omitted but the dollar sign must always be present.</p>
<h2 class="sectiontitle">Use the API Documentation</h2>
<p>Whenever there is a class or interface in your code in IntelliJ, you can hover over it and press the control key and this will display a little dialog box that tells you where this is defined, particularly whether it is defined as a Kotlin or a Java class.</p>
<p>If we take String as an example, we can see that the word String becomes a link and we can then click on it to call up the code showing how this is defined. For instance, we can see from this that String implements two interfaces, Comparable and Char Sequence. We can also press control Q for documentation.</p>
<p>More information can be found on the <a href="https://kotlinlang.org/docs/reference">Kotlin documentation website</a>.</p>
<p>There is also a pdf with the Kotlin documentation which is quite handy and can be downloaded from <a href="https://kotlinlang.org/docs/kotlin-docs.pdf">here</a>.</p>
<h2 class="sectiontitle">Test Your Knowledge with Kotlin Koans</h2>
<p>The website, <a href="https://try.kotlinlang.org">try.kotlinlang.org</a> has a number of useful resources for learning Kotlin including an interactive interpreter and multiple examples including the examples from Kotlin in Action and a series of exercises called Kotlin Koans. These can be accessed via the webpage but it is also possible to introduce the examples from Kotlin in Action and Kotlin Koans in IntelliJ.</p>
<p>For reference, there is a free online version of <a href="https://www.manning.com/books/kotlin-in-action">Kotlin In Action</a>.</p>
<p>To do this, go to File &gt; Settings and select Plugins. Next, search for EduTools, select the version you want to install and click Install. When the Welcome appears, which should happen when IntelliJ starts or when a new project is created, you should see a screen asking whether you want to use the plugin as a learner or an educator. I didn’t see this so I have checked the plugins and I can see that EduTools has not been activated due to the fact that a required plugin, com.intellij.modules.androidstudio has not been enabled.</p>
<p>Searching plugins for Android Studio returns two, both of which are installed and enabled so I’m not sure what the problem is here. This would be a nice feature but is not critical so I will worry about it later!</p>
</article>
<div class="btngroup">
<button class="button" onclick="window.location.href='getstarted.html'">
Chapter 2
</button>
<button class="button" onclick="window.location.href='variables.html'">
Chapter 3
</button>
<button class="button" onclick="window.location.href='flow.html'">
Chapter 4
</button>
<button class="button" onclick="window.location.href='classes.html'">
Chapter 5
</button>
<button class="button" onclick="window.location.href='data.html'">
Chapter 6
</button>
<button class="button" onclick="window.location.href='inheritance.html'">
Chapter 7
</button>
<button class="button" onclick="window.location.href='appendixa.html'">
Appendix A
</button>
<button class="button" onclick="window.location.href='appendixb.html'">
Appendix B
</button>
<button class="button" onclick="window.location.href='code.html'">
Code Samples
</button>
<button class="button" onclick="window.location.href='/android/android.html'">
Android Page
</button>
<button class="button" onclick="window.location.href='/index.html'">
Home
</button>
</div>
</body>
</html>