Kotlin Learning (2) -- basic syntax function variables, string template conditional expressions, null, type detection, for, while, when, range, set, kotlinnull

Source: Internet
Author: User

Kotlin Learning (2) -- basic syntax function variables, string template conditional expressions, null, type detection, for, while, when, range, set, kotlinnull

Author:Liu Ren programmer

Blog:Http://blog.csdn.net/qq_26787115

Statement: Do not reprint without the permission of the original author


1. Basic syntax


Many concepts of Kotlin are similar to those of JAVA, so I should not

It's as detailed as my JAVA journey, but don't worry. You'll see it clearly. I also learned it based on official documents.


Create a project Kotlin02 in IDEA


1. Functions (fun)


By default, we have a main function, that is, main.


Like JAVA, it is the main entry of the program. Of course, we also have common functions.

Here we can have a clear understanding of the Kotlin function. First, there is a Unit in our no-argument return. This is the flag.
This function has no return value. It is the same as void in JAVA and can be omitted. Note that its value is
Is to define the variable first, and then point to the type with a colon, such as a: Int B: String, and the return value is also pointed to with a colon, such as: Int


In addition, we can also use the expression as the function body and return value type, such:


2. Variable (val & var)


There are three variables: local variables, top-level variables, and variable variables.

But there are two modifiers: val and var.


After the val modifier is used, only one value can be assigned before the read-only variable is programmed. in JAVA, it can be understood as a constant. How can this problem be solved? It can be said that the final modifier is the same in JAVA, let's give an example.

The meaning here is that I define an Int Type a and then output a + 2. the compiler will prompt an error because there is no value after val modification and cannot be operated, then I assign him a value of 3 to output it correctly.


Another thing to note here is that we can define a variable like val c: Int without assigning a value first, but if you do not specify a value and do not define a type, such as val a, it is wrong, the compiler cannot know what operations you need


Let's take a look at the variable. The usage is the same, but he is variable.

Here we can understand that input a 2, then a ++, and output 3


There is also the last top-level variable. The top-level variable is actually the global variable in JAVA. There is nothing to say.

There's nothing to say about it.


3. String template ($)


The string template is also relatively simple, that is, character reference. Let's take a look at an example.


Run and Output


It can be seen that the variables boy and girl are referenced in the output here. You only need to add $ in front of them, which is a bit similar to the escape characters in JAVA.


Of course, there are some more advanced usage, such


Here we can see the output


That is to say, we can operate directly. Of course, such escaping is essentially no different from the following.

What kind of convenience do you think?


4. conditional expression (if else)

If else has always been written in this way. Of course, if it is a simple judgment, we can write it like this.


5. Check for null values and null values (?)


Remember how we write functions in java? For example


This is a piece of JAVA code. We can see that if we can assign values to y after logical operations, but in the same way, null may be returned, but in kotlin, we need special processing

Note that if the returned value may be null, You need to mark it? Question mark. Otherwise, an error will be reported when you return null.


6. type detection and implicit type conversion (is)


In JAVA, instanceof is used for type detection. In kotlin, is can be used. Let's look at an example.

In this Code, I will explain that Any has a common parent class for all classes, which is similar to an Object. However, it is not as big as an Object.


In this Code, first, I used is to judge the String. Here we can not only judge, but also convert the type. If it is true, we can output. length, but before calling is, there is actually no length method.


7. for Loop (for in)

In this Code, we define a list as a, and then use in To Go To The for loop. Here I will talk about the application scenario of the string template.


For example, if the output result requires a line break, print (B + "\ n") is incorrect when you use the "+" symbol. print ("$ B \ n") is required "), in Kotlin, in is responsible for the for loop, that is, the maximum number of times is the list length.


Of course, if you want to ask, what should I do if I want to know the subscript?

In fact, in is just a means to provide a loop. To really loop things, you have to decide on your own. So here, I will loop through the subscripts of list a. indices


In this way, I can get the subscript and the corresponding value. Of course, we can write this output in a more official way:


Similarly, we can see the print


8. while loop (while)

There is nothing to say about the while loop. You just need to follow the normal steps.


9. when expression (when)

From this code, you can see something. That's right. In fact, the when expression in Kotlin is the switch statement block in JAVA-> it is equivalent to the case


10. range)

We can use int to determine whether x is in the range of 1-10. Of course, the interval usage is still quite large, such as series iteration. We will discuss this in detail later.


11. Set

The set has already appeared in the for loop above us.

Collections are widely used. We will discuss them later. Here is an example:


Let's take a look at the output.

This question is a bit rough. You can understand it. It means that we only have three kinds of fruits for three people who like to eat, and they will give me corresponding scores, if the score exceeds 5 points, we can analyze this question.


First, we define a set of fruit to represent my current fruit, and then I define fractions to accumulate scores, start to loop and determine who like to eat, give a few minutes, finally, determine whether the value is greater than 5,


The question is simple, but our logic starts from here.


This is probably a rough introduction of Kotlin. In fact, this beautiful language is more than just the content. When you know this, we can start the next step, at the beginning, there is no detailed explanation of any knowledge point.


If you are interested, you can come to Github to participate.

Kotlin:

Https://github.com/LiuGuiLinAndroid/Kotlin


One life, two luck, three feng shui, four repair Yin de Five study!

Liu Guilin

No.: Android_LiuGuiLin

Sina Weibo: @ Liu, a programmer



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.