Kotlin variables differ from Java:var to Val (KAD 02)

Source: Internet
Author: User

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

Antonio Leiva

Time: Nov 28, 2016

Original link: http://antonioleiva.com/variables-kotlin/

Lesson two of the Kotlin series: Kotlin variables differ from Java:var to Val.

In this second chapter, we'll look at how variables in Kotlin work, what Val and Var are, and when to use them.

I started here because understanding this section is useful for subsequent discussions on how to create fields in a class (and also to know that they are not fields).

Kotlin variable

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

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

    • Variables are variable and immutable.

In Java it is also mutable and immutable (if you do not want the variable to be changed, you can describe the variable as final), but in Kotlin, it is more concise and more common: as long as possible, the invariant value is preferred in Kotlin .

In fact, the use of immutable variables in most parts of the program can bring many benefits, such as predictable behavior and thread safety.

    • Declare variables with Val or Var to indicate that they are immutable or mutable

What's interesting in Kotlin is that you don't need to describe the type of object you're using, and the compiler infers it.

So, we just need to fill in Var or val based on the type of variable we want to generate, and the type is usually inferred. We can also explicitly specify variable types.

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 create new instances of objects using new.

    • Type conversions are auto-completed

At any time, the compiler is able to detect that there are no other options to automatically complete the type conversion. Very good!

1 val z:view = Findviewbyid (r.id.my_view)23if  (Z is TextView) { C6>4     z.text = "I ' ve been casted!" 5 }

See I didn't call SetText ()? This will be explained in the next article!

    • In Kotlin, all of them are objects

There is no base type, no void. If there are sometimes no return values, the Unit object is actually returned. In most cases, a uint can be omitted, but it does exist and is hidden.

Therefore, all of these are objects than where:

1 val x:int =2 val y:double = 21.53 val z:unit = Unit

    • A concise numeric type cannot be assigned to a complex type

For example, shaping cannot assign long variables. The following code cannot be compiled:

1 val x:int =2 val y:long = x

You need to make a clear transition:

1 val x:int =2 val y:long = X.tolong ()

Conclusion

These are the obvious differences that can be found between Java and Kotlin. Typically, the Kotlin variable provides a more flexible , secure (because of any possible Val conventions) and clearer, more Concise code .

Still have questions? Prepare to read the next chapter!

Kotlin variables differ from Java:var to Val (KAD 02)

Related 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.