I am IT king demon king this is my 5th IT series original

I set out to write the most suitable article for beginners to get started

There are a lot of articles on the Internet, and they are all well written but I think they have one common feature (a strength, but also a weakness) so I’ll use the word feature

I generally don’t like to talk nonsense (this is nonsense, sorry). If you can solve it with pictures, try not to use words

Write above: The variables described in this article are global variables

Declare variables in Java

/** * Public class person {String name; int age; }Copy the code

You all have ID, so I’m not going to explain these lines of code.

Now, how do we declare variables in Kotlin

1. Do not declare variable types (implicit declaration)

/** ** Kotlin Penguin */ class Penguin {var name = */ class Penguin {var name = */"Running boy"Var count=0 var age = 10 var count=0hit(){
        count++
     println("I am"+name+"I did."+count+"Sub doudou")}}Copy the code

**var** in kotlin; **var** in ######; **var** in kotlin; 3. Kotlin does not have to declare variable types to declare variables

In Kotlin, the compiler can automatically deduce what type a variable is from its value, a feature called automatic type inference.

The way we declare a variable without specifying its type is called an implicit declaration

What if you say, I must emphasize the data type in the declaration, ok? The answer is yes!

2. Declare variable types (explicitly)

/** * kotlin */ class Penguin {var name:String = ** * Kotlin */ class Penguin {var name:String = */"Running boy"Var count:Int=0 var count:Int=0 var count:Int=0hit(){
        count++
     println("I am"+name+"I did."+count+"Sub doudou")}}Copy the code

######2, kotlin uses the var keyword (don’t fret, there is another keyword) ######3, kotlin uses the var keyword (don’t fret, there is another keyword) ######3, kotlin uses the var variable name — > var variable name: Variable type = variable value

###### Summary: 1. When declaring a variable explicitly or implicitly, we must assign a value to the variable ######2

Read only variables are declared

Constants in Java, once initialized, cannot be changed

/** * Java person class */ public class person {final String name="Running boy"; // Java constant, final, value cannot change int age; }Copy the code

Read-only variables in Kotlin, like constants in Java, cannot be changed once declared

1. Implicitly declare read-only variables

/** * Kotlin Penguin */ class Penguin {val name = ** * Kotlin Penguin */ class Penguin {val name ="Running boy"Val age= 10 val count=0hit(){count++// this position will give an error because the variable declared by val cannot be changed. Println ()"I am"+name+"I did."+count+"Sub doudou")}}Copy the code

👆 does not declare variable types, only values

2. Display declared read-only variables

/** * kotlin Penguin */ class Penguin {val name:String = ** * Kotlin Penguin */ class Penguin {val name:String ="Running boy"Val age:Int= 10 val count:Int=0hit(){count++// this position will give an error because the variable declared by val cannot be changed. Println ()"I am"+name+"I did."+count+"Sub doudou")}}Copy the code

👆 declares both the variable type and its value

### summary 1. Kotlin is too easy for those with programming skills, especially Those with Java skills. Var declares a variable that can be changed after its value. 3. Val declares a read-only variable that cannot be changed. Different data types still cannot be “mixed” (explained later) 5. Kotlin will automatically infer data type 6 through type inference. Display the specified data type with a colon. 7 Semicolons are not required in kotlin

This is because Kotlin’s automatic type inference function deduces the type of the variable from the value behind it. If we do not assign a value to the variable, Kotlin will not know the type of the variable and will report an error.

So if we use display declaration, don’t variables need to be assigned at declaration time? You can try it and find the error still reported (manual smile 🙂) as follows 👇

/** * kotlin Penguin */ class Penguin {var name:String */ Var count=0 val age= 10 var count=0 val age= 10 var count=0hit(){
        count++
     println("I am"+name+"I did."+count+"Sub doudou")}}Copy the code

If you follow the demo (you do not knock, trouble you do not read my post, PUT me on the blacklist) you will find that I said all right. But there must be a question mark in your head. Kotlin wouldn’t be that stupid, would he? Of course not, but for now, stay foolish.