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

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.

主站蜘蛛池模板: 镇巴县| 永康市| 苍南县| 顺昌县| 鄢陵县| 丽水市| 清苑县| 吕梁市| 青铜峡市| 离岛区| 扬中市| 佛学| 枣阳市| 保亭| 海林市| 安多县| 手游| 靖边县| 阳高县| 岗巴县| 印江| 白玉县| 夏津县| 新疆| 墨脱县| 应城市| 故城县| 盖州市| 扶余县| 麦盖提县| 二连浩特市| 平阳县| 外汇| 云安县| 曲阳县| 云阳县| 浮山县| 磴口县| 新昌县| 集贤县| 呼伦贝尔市|