fix(guide): simplify directory structure

This commit is contained in:
Mrugesh Mohapatra
2018-10-16 21:26:13 +05:30
parent f989c28c52
commit da0df12ab7
35752 changed files with 0 additions and 317652 deletions

View File

@ -0,0 +1,70 @@
---
title: Hello World in Kotlin
---
A Hello World program is a very simple program that outputs the string "Hello World!". It is often used to show the basic syntax of a programming language.
In this tutorial we are going to analyse the syntax of a Hello World program written in Kotlin.
If you still haven't installed Kotlin you should check this tutorial: https://guide.freecodecamp.org/kotlin
## Hello World Program
```kotlin
// This is a simple Hello World program written in Kotlin
fun main(args : Array<String>) {
println("Hello, World!")
}
```
As you should expect, when you run this program the output should be "Hello, World!".
## Syntax
### Comments
```
// This is a simple Hello World program written in Kotlin
```
Comments are text written by a developer that are added with the purpose of making the code easier to understand by other developers.
In Kotlin comments can be single-line comments (using //) or multi-line comments (using /**/).
```
// Single line comment
/* This is a
Multi-line comment
*/
```
### Main function
```kotlin
fun main(args : Array<String>) {...}
```
The main function is a mandatory function that tells the compiler where it should start executing our code. It takes an array of strings as parameter and returns the type Unit which corresponds to the type ```void``` in languages like Java.
As we can see, functions are declared with the use of the keyword ```fun``` and its body should be written inside curly braces.
Functions without a explicitly declared return type will return the type ```Unit```, therefore, the above code is equivalent to
```kotlin
fun main(args : Array<String>): Unit {...}
```
### Print Statement
The println function takes a string as an argument and prints it to the screen. In this case we are printing the string "Hello, World!". Note that string literals are declared using double quotes ```"String"```.
If you'd like to know more about Kotlin Syntax and start writing awesome programs you should check the awesome Kotlin Official Documentation: https://kotlinlang.org/docs/reference/
Hope you liked this tutorial,
Cheers
## Kotlin course for beginners
Udacity provides the free [Kotlin Bootcamp for Programmers](https://www.udacity.com/course/kotlin-bootcamp-for-programmers--ud9011) course.

View File

@ -0,0 +1,175 @@
---
title: Kotlin
---
**What is Kotlin?**
<a href='https://kotlinlang.org/'>Kotlin</a> is a programming language developed by <a href='https://www.jetbrains.com'>Jetbrains</a>, the company behind some of the world's most popular IDEs like IntelliJ and Pycharm.
It serves as a replacement for Java and runs on the JVM. It has been in development for close to 6 years and it hit 1.0 just a year ago.
The developer community has embraced Kotlin to such an extent that Google announced first class support for the language for Android Development at <a href='https://blog.jetbrains.com/kotlin/2017/05/kotlin-on-android-now-official/'>Google I/O 2017</a>.
## Version
As of this writing, the latest stable release of Kotlin happens to be <a href='https://blog.jetbrains.com/kotlin/2018/09/kotlin-1-2-70-is-out/'>version 1.2.71</a>
## Installation
Before proceeding with the installation instructions for Kotlin, you need to make sure that you have set up **JDK (Java Development Kit)** set up on your system.
If you do not have JDK installed on your computer, head over to the **Installation section** on <a href = 'https://guide.freecodecamp.org/java'>this link to learn</a> how to set it up.
Kotlin works with **JDK 1.6+** so make sure you get the correct version installed. Once you are done setting up JDK, proceed with the following steps.
* ## IntelliJ IDEA
The quickest way to get Kotlin running on your machines is by using it alongside **IntelliJ IDEA**. This is the recommended IDE for Kotlin because of the tooling support that is provided by Jetbrains. You can grab the <a href='http://www.jetbrains.com/idea/download/index.html'>Community Edition</a> of IntelliJ from <a href='https://www.jetbrains.com'>JetBrains</a>.
Once you have installed IntelliJ you can basically get started with your first project in Kotlin without any further configurations.
Create a **New Project** and make sure you select the Java Module. Select the Kotlin checkbox on that screen
![new project screen](https://kotlinlang.org/assets/images/tutorials/getting-started/new_project_step1.png)
Give your project a name and click Finish.
![project name ](https://kotlinlang.org/assets/images/tutorials/getting-started/project_name.png)
You will now be taken to the main editor where you will see your project files organized in the following manner.
![project structure ](https://kotlinlang.org/assets/images/tutorials/getting-started/folders.png)
In order to verify your installation, create a new Kotlin file in the **src** folder and name it **app** (or anything else that suits you)
![project structure ](https://kotlinlang.org/assets/images/tutorials/getting-started/new_file.png)
Once you have the file created, type out the following cremonial Hello World code. Don't worry if it doesn't make sense right away, it will be dealt with in detail later on in the guide.
```
fun main (args: Array<String>) {
println("Hello World!")
}
```
![project structure ](https://kotlinlang.org/assets/images/tutorials/getting-started/hello_world.png)
You can now run this program by either clicking on the Kotlin icon on the gutter (left side of your editor with line numbers)
![hello world ](https://kotlinlang.org/assets/images/tutorials/getting-started/run_default.png)
If everything goes fine, you should see the message Hello World! in your Run window as shown below
![run window ](https://kotlinlang.org/assets/images/tutorials/getting-started/run_window.png)
* ## Eclipse
While IntelliJ is the recommended IDE for developing with Kotlin, it is definitely not the only option out there. **Eclipse** happens to be another popular IDE of choice among Java developers and Kotlin is supported by Eclipse as well.
After setting up the **JDK** on your system, follow the instructions below.
Download <a href='https://www.eclipse.org/downloads/'>**Eclipse Neon** </a>for your operating system and once you have successfully installed it on your system, download the **Kotlin Plugin** for Eclipse from the <a href='http://marketplace.eclipse.org/content/kotlin-plugin-eclipse'>**Eclipse Marketplace**</a>.
![eclipse marketplace ](https://kotlinlang.org/assets/images/tutorials/getting-started-eclipse/marketplace.png)
***NOTE: You can also do the same by going into Help -> Eclipse Marketplace and then search for Kotlin Plugin***
Once, the plugin is installed you are pretty much done but it would be a good idea to take the IDE for a spin with a quick Hello World sample.
Create a new Kotlin Project by clicking on File -> New -> Kotlin Project
![new kotlin project ](https://kotlinlang.org/assets/images/tutorials/getting-started-eclipse/new-project.png)
An empty project will be created with a directory structure quite similar to a Java project. It would look something like this
![empty kotlin project ](https://kotlinlang.org/assets/images/tutorials/getting-started-eclipse/empty-project.png)
Go ahead and create a new Kotlin file in the **src** folder
Once that is done go ahead and type out the following code. Don't worry if it does not make sense right now, it will be covered later in the guide.
```
fun main (args: Array<String>) {
println("Hello World!")
}
```
![eclipse hello world ](https://kotlinlang.org/assets/images/tutorials/getting-started-eclipse/hello-world.png)
Now that you are done typing out the Hello World code, go ahead and run it. To run the file, right click anywhere inside the editor and click on ***Run As -> Kotlin Application***
![eclipse run app](https://kotlinlang.org/assets/images/tutorials/getting-started-eclipse/run-as.png)
If all goes well, the console window would open to show you the output.
![eclipse run app](https://kotlinlang.org/assets/images/tutorials/getting-started-eclipse/output.png)
* ## Using the standalone compiler on the terminal
If you are someone who prefers doing things in a more manual way and do not want to tie yourself down to an editor/IDE you might wanna use the Kotlin compiler.
### Downloading the compiler
With every release of Kotlin, Jetbrains ship a standalone compiler which can be downloaded from the <a href='https://github.com/JetBrains/kotlin/releases/tag/v1.1.51'>GitHub releases</a>. Version 1.1.51 happens to be the latest at the time of this writing.
</br>
**Manual Installation**
Once you have downloaded the compiler you need to unzip it and proceed with the standard installation using the installation wizard. Adding the **bin** directory to the system path is an optional step. It contains the scripts that are necessary to compile and run Kotlin on Windows, Linux and macOS.
</br>
**Installation via Homebrew**
You can install the compiler on macOS using <a href='http://brew.sh/'>Homebrew </a>which is a package manager for macOS. Launch the Terminal app and issue the following commands
```
$ brew update
$ brew install kotlin
```
**Installation via SDKMAN!**
Another simple way of installing the Kotlin compiler on macOS, Linux, Cygwin, FreeBSD and Solaris is by using <a href='http://sdkman.io/'>SDKMAN!</a>. Launch the terminal and issue the following commands
```$ curl -s https://get.sdkman.io | bash```
Follow the instructions on screen and once SDKMAN! is setup issue the follwoing command inside terminal
```$ sdk install kotlin```
As with all previous installation options, it would be a good idea to test run the installation.
Open a text editor of your choice and write a basic Kotlin program given below
```
fun main(args: Array<String>) {
println("Hello, World!")
}
```
Save this file with a **.kt** extension. You are now ready to compile it and see the results. To do so, issue the following command
```$ kotlinc hello.kt -include-runtime -d hello.jar```
the ```-d``` option tells the compiler what you want the output to be called. The ```-include-runtime``` option makes the resulting .jar file self-contained and runnable by including the Kotlin runtime library in it.
If there were no compilation errors, run the application using the following command
```$ java -jar hello.jar```
If all goes well, you should see **Hello World!** printed on your terminal screen
```
$ java -jar hello.jar
Hello, World!
```
Congratulations you have successfully set up the Kotlin compiler and development environment on your system. We will cover all of the intricacies and fun parts of Kotlin in this guide, but you can get a headstart if you want by going to the <a href='https://try.kotlinlang.org/'>Try Kotlin</a> website and going through the exercises there.
## Documentation
One of the greatest things about Kotlin is it's comprehensive and well structured documentation. Even if you are new to programming, you will find yourself right at home with the docs. They do a pretty amazing job at laying it all out in a well structured manner. You can check out the official documentation at <a href='https://kotlinlang.org/docs/reference/'>this link</a>.

View File

@ -0,0 +1,254 @@
---
title: Strings
---
# Strings
A string is a basic data type in a programming language. Strings are represented by the type `String`. Strings are immutable. Kotlin has a rich API for working with strings.
### Basic Usage
#### Declaration
```kotlin
// Explicit type declaration
var firstName : String = "Elon"
// or Implicit type declaration and will still compile
val lastName = "Musk"
```
In addition, notice the usage of `val` variable type, here is how it behaves
```kotlin
firstName = "Mark" // can be changed
lastName = "Zuckerberg" // cannot be changed
lastName = 12 // Error: type mismatch
```
#### String Concatenation
Shown in the code snippet, just like Java, appending `Int` to
`String` will result to a `String` output
```kotlin
var str = "abc" + 1
println(str + "def")
```
Output:
```shell
abc1def
```
Even without explicitly converting `Int` value 1 to `String` object first, the resulting output is still a `String`.
#### String with Multiple Lines
Programmers can declare `String` variables with multiple lines by using triple quotes instead of double quotes
```kotlin
var str = """
This is line 1
This is line 2
This is line 3
"""
println(str)
```
Output:
```shell
This is line 1
This is line 2
This is line 3
```
or with `.trimIndent()`
The use of `trimIndent()` will additionally help to provide a clean output format by removing excess and unnecessary indentions on each line. Examine the code snippet below:
```kotlin
var str = """
This is line 1
This is line 2
This is line 3
""".trimIndent()
println(str)
```
Output:
```shell
This is line 1
This is line 2
This is line 3
```
### Accessing Characters of a String
#### Index Access
Programmers can access elements (characters) of a string using index access operator:
```kotlin
var str = "Example"
println(str[2])
```
Output:
```shell
a
```
It's just like accessing an element from an array, you get:
```kotlin
var str = "Example"
println(str[9]) // Error: index out of bounds
```
#### Iterate through a String
Elements of a string are characters that can be accessed by the indexing operation: `s[i]`.
```kotlin
var str = "Example"
for (c in str) {
println(c)
}
```
Output:
```shell
E
x
a
m
p
l
e
```
### Immutability of a String
Just like Java, you cannot change individual elements of a `String`
```kotlin
var str = "Example"
str[2] = "b" // Error
```
#### Re-assigning String values
```kotlin
var str = "Example"
println(str)
str = "Example was changed"
println(str)
```
Output:
```shell
Example
Example was changed
```
### String Properties
#### Determining length of a String
```kotlin
var str = "Example"
println(str.length)
```
Output:
```shell
7
```
### String Functions
These are some of the common `String` functions available from the current Kotlin version
### compareTo
Compares this object with the specified object for order. Returns zero if this object is equal to the specified other object, a negative number if it's less than other, or a positive number if it's greater than other.
```kotlin
var str = "Example"
var str2 = "Example123"
var str3 = "Example12345"
println(str.compareTo(str2))
println(str.compareTo(str3))
println(str3.compareTo(str))
println(str.compareTo("Example"))
```
Output:
```shell
-3
-5
5
0 # Equal
```
### equals
Indicates whether a `String` object is exactly equal to another `String` object
```kotlin
var str = "Example"
var str2 = "Example2"
println(str.equals("Example"))
println(str2.equals("Example"))
```
Output:
```shell
true
false
```
### get
Returns the character at the specified index in this character sequence.
``` kotlin
var str = "Example"
println(str.get(3))
```
Output:
```shell
m
```
### toString
Returns a string representation of the object.
```kotlin
println(9.toString() + 10)
println(9 + 10)
```
Output:
```shell
"910"
19
```
#### Resources
* [Kotlin Basic Types](https://kotlinlang.org/docs/reference/basic-types.html)
* [Kotlin String Reference](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)