- 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.
推薦閱讀
- Oracle WebLogic Server 12c:First Look
- Drupal 8 Blueprints
- ASP.NET Core Essentials
- C/C++算法從菜鳥到達(dá)人
- 數(shù)據(jù)庫系統(tǒng)原理及MySQL應(yīng)用教程
- UI智能化與前端智能化:工程技術(shù)、實(shí)現(xiàn)方法與編程思想
- Mastering Swift 2
- Spring Boot進(jìn)階:原理、實(shí)戰(zhàn)與面試題分析
- Test-Driven Machine Learning
- SQL 經(jīng)典實(shí)例
- GameMaker Essentials
- Python入門很輕松(微課超值版)
- C++ Fundamentals
- FFmpeg開發(fā)實(shí)戰(zhàn):從零基礎(chǔ)到短視頻上線
- 深入解析Java編譯器:源碼剖析與實(shí)例詳解