- Android Developers Blog Announcing Kotlin
- Install JDK if you don't have it
- Install IntelliJ
QUESTION 1 OF 3
Which of the following features of Kotlin will make your life as a developer better?
How will Kotlin help you write better code?
How do you think Kotlin might help you write better code? Write 3 reasons into the box below:
Your reflection
Things to think about
Thanks for sharing your thoughts
QUESTION 3 OF 3
Did you notice the aquarium on the table? You will see and learn more about aquariums in this course. :-) How many fish were there in the aquarium? Take a guess or go back and count.
Installing the Java Development Kit (JDK)
If you don't have the latest JDK already installed on your computer, follow the steps below. You will need to have the JDK installed to run Kotlin programs.
The JDK is freely available, and you can download it here: http://www.oracle.com/technetwork/java/javase/overview/index.html.
The JDK or the JRE?
The JRE (Java Runtime Environment) is needed for running Java and Kotlin programs. The JDK (Java Development Kit), on the other hand, includes the JRE, plus the development tools you'll need for writing and running Java programs. You need the JDK for writing Kotlin Programs.
Steps to install the JDK
1. Uninstall any older versions of the JDK/JRE
We recommend that you install only the latest JDK.
2. Download the JDK
You can download the JDK for free here: http://www.oracle.com/technetwork/java/javase/downloads/index.html.
- Click the "Download" button under the JDK for the latest Java SE version.
- Check "Accept License Agreement".
- Choose the JDK for your operating system.
3. Install the JDK (for Mac)
From either the Downloads window of the browser, or from the file browser, double-click the .dmg file to launch the install file.
- A Finder window appears with an icon of an open box and the name of the .pkg file.
- Double-click the package icon to launch the Install app, and follow the prompts as they appear.
- You might need to enter the administrator password to continue.
- Feel free to delete the .dmg file to save space after the installation is complete.
3. Install the JDK (for Windows)
Run the downloaded installer (e.g., jdk-10.0.x_windows-x64_bin.exe
), which installs both the JDK and the JRE.
By default, the JDK will be installed in the directory "C:\Program Files\Java\jdk-10.0.x", where x denotes the version number; and the JRE in "C:\Program Files\Java\jre-10.0.x".
Accept the defaults, and follow the screen instructions to install the JDK.
4. Add the JDK installation path to PATH (Windows only)
Windows searches the current directory and the directories listed in the PATH environment variable (system variable) for executable programs.
- Open "Control Panel" -> "System" -> "Advanced system settings" -> "Environment Variables".
- Under "System variables", scroll down to select "Path" and click "Edit...".
- Append to the existing Path value a semi-colon ";" then the JDK's "bin" directory (e.g. ";C:\Program Files\Java\jdk-10.0.0\bin").

Kotlin Documentation
Throughout this course you might find it useful to check the official Kotlin documentation every time you learn something new:
Read the code below, try to follow what it does, and then choose the correct answer:
var welcomeMessage = "Hello and welcome to Kotlin"
when (welcomeMessage.length) {
0 -> println("Nothing to say?")
in 1..50 -> println("Perfect")
else -> println("Too long!")
}
QUIZ QUESTION
What will the code above print?
1. "Nothing to say?"
2. "Perfect"
3. "Too long!"
4. It won't display anything because there's a syntax error
Correct Answer: "Perfect"
Read the code below, and then select the correct array initialization that will display the corresponding output.
val array = // initalize array here
val sizes = arrayOf("byte", "kilobyte", "megabyte", "gigabyte",
"terabyte", "petabyte", "exabyte")
for ((i, value) in array.withIndex()) {
println("1 ${sizes[i]} = ${value.toLong()} bytes")
}
Output:
1 byte = 1 bytes
1 kilobyte = 1000 bytes
1 megabyte = 1000000 bytes
1 gigabyte = 1000000000 bytes
1 terabyte = 1000000000000 bytes
1 petabyte = 1000000000000000 bytes
1 exabyte = 1000000000000000000 bytes
QUIZ QUESTION
Which of the statements below is the correct way to initialize the array above to give the correct output.
val array = Array(7) { 1000.0.pow(it) }
val array = Array(7) { 1000.0.pow(10) }
QUIZ QUESTION
Which of these options are good reasons to explicitly make a list immutable? There may be more than 1 correct answer.
It reduces errors in general.
Prevents accidental changing of objects that were meant to be unchangeable.
In a multi-threaded environment, makes the variable thread safe, because once it has been assigned by the initial thread, no thread can change it.
Immutable variables are the safest option when you know that a variable will never need to change values.
Practice Time
Looping over arrays and lists is a fundamental technique that has a lot of flexibility in Kotlin. Let's practice.
Looping over arrays and lists is a fundamental technique that has a lot of flexibility in Kotlin. Let's practice.
Basic example
- Create an integer array of numbers called
numbers
, from 11 to 15. - Create an empty mutable list for Strings.
- Write a
for
loop that loops over the array and adds the string representation of each number to the list.
- Create an integer array of numbers called
numbers
, from 11 to 15. - Create an empty mutable list for Strings.
- Write a
for
loop that loops over the array and adds the string representation of each number to the list.
Challenge example
- How can you use a
for
loop to create (a list of) the numbers between 0 and 100 that are divisible by 7?
- How can you use a
for
loop to create (a list of) the numbers between 0 and 100 that are divisible by 7?
Code for main():
fun main(args: Array<String>) {
println("Hello, world!")
}
0 Comments