1. Preparation
- computer with internet access
- A newer version of a web browser (such as the latest version of Chrome) installed on the computer
Important things you will learn in this section
How to create, change, understand and run the simplest Kotlin program for displaying messages.
2. Run your first program written in Kotlin
If you use a compiler to run, it may involve the installation of related compilers, the configuration of environment variables, etc., which is not very friendly to friends who are new to programming, so this summary recommends that you run https in browser-based programming tools: //developer.android.com/training/kotlinplayground
After opening the browser, you will normally see the following interface
Click the green triangle icon on the right of the compiler to run the program
At this time, the running result will be displayed below the compiler
3. Modify your program
Change the Hello World code
you can put
Change "Hello World" to the text you want, such as "Hello Lengzuiquan!"
Click the green triangle to run the program
and you will see your running results
After you click the "Run button", the system will send the code you wrote to a third-party server controlled by JetBrains for compilation.
What is the working mechanism
It seems that if we want to output a sentence, we need to use two lines of code. Some students may find it more complicated.
If you ask your friend to write "Hello World!" on a piece of paper, there may be a lot of implicit information in it, for example, they will assume that they need to use a pen, and you want them to use the alphabet to write! The computer doesn't make these assumptions, so you have to give precise instructions covering each step.
Just like English has structures, so do programming languages. If you've ever learned another language, you know the challenges of learning grammar, spelling, and perhaps a new alphabet of symbols and vocabulary. Learning to program has similar challenges, but fortunately, it's much simpler and more logical than learning (eg, English).
Understand the components of the program
Now, take a look at the code. Each part of the program has a specific purpose, and you need all of them to run the program. Let's start with the first word.
fun
fun
Represents a word in the Kotlin programming language.fun
function. A function is a part of a program that performs a specific task.
Note: Kotlin has many special words with very specific meanings. When you learn to program in the Kotlin language, you will learn these words. They are often called keywords or reserved words.
fun main
main
is the name of the function. Functions have names so they can be distinguished from each other. Call this functionmain
because it is the first or main function called when running the program. Kotlin programs are required to have amain
function name.
fun main()
- A function name is always followed by
()
two parentheses. - Inside the brackets, you can put information about the function you want to use. The inputs to the function are called "arguments" or
args
simply for short. You'll learn more about parameters later.
fun main() {
}
- Note
()
the pair of curly braces after the parentheses{}
. Inside the function is the code that does the job. These curly braces surround these lines of code. Look at the lines of code between
the curly braces :{}
println("Hello 冷醉泉!")
This line of code prints out Hello 冷醉泉!
the text.
- println tells the system to print a line of text.
- Put the text you want to print in brackets
- Note that the text to be printed must be placed in English double quotes, which tells the system that the content in the quotes must be printed exactly as given (in layman’s terms, it prints whatever is in the quotes)
To actually print the text, this entire println
command line must be placed main
inside the function
As shown below, this is a minimal Kotlin program.
fun main() {
println("Hello 冷醉泉!")
}
4. Extend your program
print multiple messages
marvelous! You have used println()
the function to output a line of text. However, you can add any line directives inside the function as needed or as required by the task.
- Copy the line
println("Hello 冷醉泉!")
of code and paste it below twice. Make sure that the pasted line of code is within curly braces of the main function. - Change one of the texts to be output to a person's name, such as "Li Hua".
- Change the other text to output to read "You are 18!".
Your code should look like the code below.
fun main() {
println("Hello 冷醉泉!")
println("李华")
println("You are 18!")
}
What do you think would be the result of running this code?
- Run your program and see what results it produces.
- Go to the output pane and you should see 3 lines of output in the console window as shown below.
Hello 冷醉泉!
李华
You are 18!
Great!
Handle errors
It's normal to make mistakes while programming, and most tools provide feedback to help you correct mistakes. In this step, make a bug and see what happens.
- In your program, remove the quotation marks
李华
around so that the line of code looks like this.
println(李华)
- Run the program. You should see the to be output
李华
displayed in red.
- Look at the output pane. A message is displayed in the pane explaining the error in the code.
- The message
Unresolved reference: 李华
tells you what the system thinks is wrong with your code. Even if you don't understand the error message, you may be able to figure out what's wrong. In this example, you know thatprintln()
the directive is used to output text. As you learned earlier, text must be enclosed in quotation marks. An error occurs if the text is not quoted. - Now add the quotes.
- Run the program to make sure it runs normally again.
Congratulations, you've run and changed your first Kotlin program!
5. Solution code
Below is the complete code for the program you write in this codelab.
fun main() {
println("Hello 冷醉泉!")
println("李华")
println("You are 18!")
}
6. Summary
- https://developer.android.com/training/kotlinplayground is a web-based interactive code editor where you can practice writing Kotlin programs.
- All Kotlin programs need to have a
main()
function :fun main() {}
println()
function to output a line of text.- Enclose the text you want to output in double quotes, eg
"Hello"
. - The repeat
println()
command can output multiple lines of text. - Errors in the program are marked in red. Error messages appear in the output pane to help you determine where and why the error occurred.
Seven, self-practice
Please do the following exercises:
- Change the println() instruction to print().
- Run the program. What will happen?
Tip: print()
The directive will only output text and will not add a newline at the end of each string.
- Please correct the text so that each part of the message is on its own line.
Tip: Use \n
to , eg "line \n break"
. Adding newlines changes the output as shown below.
Tip: You can output blank lines by providing no text: println("").
code:
fun main() {
println("no line break")
println("")
println("with line \n break")
}
output:
no line break
with line
break
Check your program:
the following is a possible solution:
fun main() {
print("Happy 冷醉泉!\n")
print("李华\n")
print("You are 18!")
}
Next: [Create birthday greetings in Kotlin]
The content of this series of articles is all from the official website of Android Studio