The variables in Kotlin are different from those in Java: var for val (KAD 02) and kotlinkad

Source: Internet
Author: User

The variables in Kotlin are different from those in Java: var for val (KAD 02) and kotlinkad

Original article title: Variables in Kotlin, differences with Java. var vs val (KAD 02)

By Antonio Leiva

Time: Nov 28,201 6

Link: http://antonioleiva.com/variables-kotlin/

 

The second course of the Kotlin series: variables in Kotlin are different from those in Java: var for val.

In the second section, we will understand how variables work in Kotlin, what are val and var, and when to use them.

 

I started from this because understanding this part is very useful for subsequent discussions on how to create "fields" in the class (you can also understand that they are not fields ).

 

Kotlin variable

As in Java, As long as code is executed within the scope of the variable, the Kotlin variable can be assigned, modified, and used in different places in the program.

 

However, we will focus on its different aspects from Java.

 

  • Variables are variable and immutable.

In Java, variable and immutable are also divided (if you do not want the variable to be changed, you can describe the variable as final). However, in Kotlin, more concise and commonly used: as long as possible,InThe preferred value in Kotlin is unchanged..

 

In fact, using immutable variables in most parts of a program can bring many benefits, such as predictable behavior and thread security.

 

  • Declare variables with val or var to indicate that they are immutable or mutable.

What is interesting in Kotlin is that in most cases, you do not need to describe the type of the object you are using, but the compiler just deduce it.

 

Therefore, we only need to enter var or val Based on the variable type to be generated, and its type can be inferred. We can also explicitly specify the variable type.

 

Some examples:

1 var x = 72 var y: String = "my String"3 var z = View(this)

Note: As you can see, you do not need to use new to create a new instance of an object.

 

  • Type conversion is completed automatically.

At any time, the compiler can detect that there are no other options to automatically complete type conversion. Great!

1 val z: View = findViewById(R.id.my_view)2 3 if (z is TextView) {4     z.text = "I've been casted!"5 }

Have you seen that I have not called setText? This will be explained in the next article!

 

  • In Kotlin, all objects are

No basic type, no void. If sometimes no return value is returned, the Unit object is actually returned. In most cases, Uint can be omitted, but it does exist and is hidden.

 

Therefore, all these ratios are objects:

1 val x: Int = 202 val y: Double = 21.53 val z: Unit = Unit

 

  • Simple numeric types cannot be assigned to complex types.

For example, an integer cannot be assigned a long variable. The following code cannot be compiled:

1 val x: Int = 202 val y: Long = x

 

You need to explicitly convert:

1 val x: Int = 202 val y: Long = x.toLong()

 

Conclusion

These are the obvious differences between Java and Kotlin. Generally, the Kotlin variable providesMore flexible,Secure(Because of any possible use of val conventions) andClearer,Concise code.

 

Still have questions? Read the next article!

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.