官术网_书友最值得收藏!

Type inference

Let's define a simple string in Java:

String s = "Hello World";

We defined that s is of type String. But why? Isn't it obvious at this point?

Kotlin provides us with type inference:

val s = "Hello World"

Now, the compiler will decide what type of variable should be used. Unlike interpreted languages (such as JavaScript, Groovy, or Ruby), the type of variable is defined only once. This will not work:

var s = "I'm a string"
s = 1 // s is a String

You may wonder why we've used one var and one val to define the variables. We'll explain it shortly.

主站蜘蛛池模板: 奉节县| 广安市| 石河子市| 铁力市| 石阡县| 天峻县| 墨竹工卡县| 南岸区| 蕉岭县| 理塘县| 壤塘县| 汪清县| 抚松县| 思南县| 新建县| 桑日县| 唐河县| 桦川县| 双流县| 米泉市| 荣成市| 南昌市| 嵊州市| 唐河县| 靖安县| 阿克苏市| 嫩江县| 将乐县| 翁源县| 苍山县| 富宁县| 南投县| 济宁市| 红安县| 科尔| 军事| 当涂县| 清徐县| 昆山市| 贡山| 广汉市|