- Hands-On Design Patterns with Kotlin
- Alexey Soshin
- 114字
- 2021-06-25 20:49:23
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.
推薦閱讀
- Extending Jenkins
- 一步一步學Spring Boot 2:微服務項目實戰
- Mobile Application Development:JavaScript Frameworks
- Java面向對象思想與程序設計
- Java完全自學教程
- Android Development with Kotlin
- JavaScript 網頁編程從入門到精通 (清華社"視頻大講堂"大系·網絡開發視頻大講堂)
- Learning ASP.NET Core 2.0
- BeagleBone Media Center
- C#程序設計教程
- SEO實戰密碼
- UML 基礎與 Rose 建模案例(第3版)
- C# 8.0核心技術指南(原書第8版)
- Learning OpenStack Networking(Neutron)
- Learning Unreal Engine Android Game Development