鍐嶈锛孠otlin 锛佷綘濂? Java 锛?/h1> Posted 寮€鍙戣€呮妧鏈墠绾?/a>
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍐嶈锛孠otlin 锛佷綘濂? Java 锛?/h1>
相关的知识,希望对你有一定的参考价值。
鐐瑰嚮鈥?/span>
寮€鍙戣€呮妧鏈墠绾?/span>
鈥濓紝閫夋嫨鈥滄槦鏍囸煍濃€?/span>
鍦ㄧ湅|鏄熸爣|鐣欒█, 鐪熺埍
浣滆€咃細Bartosz Walacik | 鏉ヨ嚜锛欳SDN | 缂栬緫 锛氬彲鍙?/span>
鍥炲鈥?66鈥濓紝鑾峰彇涓€浠戒笓灞炲ぇ绀煎寘
鍘熸枃锛歨ttps://allegro.tech/2018/05/From-Java-to-Kotlin-and-Back-Again.html
姣棤鐤戦棶锛孠otlin 鍦ㄥ幓骞村緢鍙楁杩庯紝涓氱晫鐢氳嚦鏈変汉璁や负鍏跺皢
鍙栦唬 Java 鐨勯湼涓诲湴浣?/span>
銆傚畠鎻愪緵浜?Null 瀹夊叏鎬э紝浠庤繖涓€鐐规潵璇村畠纭疄姣?Java 鏇村ソ銆傞偅涔堟槸涓嶆槸杩欏氨鎰忓懗鐫€寮€鍙戣€呭簲璇ユ涓嶇姽璞湴鎷ユ姳 Kotlin锛屽惁鍒欏氨钀戒紞浜嗭紵
绛夌瓑锛屾垨璁镐簨鎯呭苟闈炲姝ゃ€?/span>
鍦ㄥ紑濮嬩娇鐢?Kotlin 缂栫▼涔嬪墠锛屾湰鏂囨兂瑕佸垎浜釜鏁呬簨缁欎綘銆傚湪杩欎釜鏁呬簨涓紝浣滆€呮渶鏃╀娇鐢?Kotlin 鏉ョ紪鍐欎竴涓」鐩紝鍚庢潵 Kotlin 鐨勫悇绉嶆€紓妯″紡浠ュ強涓€浜涘叾浠栭殰纰嶈秺鏉ヨ秺璁╀汉鍘岀儲锛屾渶缁堬紝浠栦滑鍐冲畾閲嶅啓杩欎釜椤圭洰銆?/span>
浠ヤ笅涓鸿瘧鏂囷細
涓€鐩翠互鏉ワ紝鎴戝鍩轰簬 JVM 鐨勮瑷€閮介潪甯告儏鏈夌嫭閽熴€傛垜閫氬父浼氱敤 Java 鏉ョ紪鍐欎富绋嬪簭锛屽啀鐢?Groovy 缂栧啓娴嬭瘯浠g爜锛屼袱鑰呴厤鍚堜娇鐢ㄥ緱蹇冨簲鎵嬨€?/span>
2017骞村澶╋紝鍥㈤槦鍙戣捣浜嗕竴涓柊鐨勫井鏈嶅姟椤圭洰锛屽拰寰€甯镐竴鏍凤紝鎴戜滑闇€瑕佸缂栫▼璇█鍜屾妧鏈繘琛岄€夊瀷銆傞儴鍒嗗洟闃熸垚鍛樻槸 Kotlin 鐨勬嫢鎶よ€咃紝鍐嶅姞涓婃垜浠兘鎯冲皾璇曚竴涓嬫柊鐨勪笢瑗匡紝浜庢槸鎴戜滑鍐冲畾鐢?Kotlin 鏉ュ紑鍙戣繖涓」鐩€傜敱浜?Spock 娴嬭瘯妗嗘灦涓嶆敮鎸?Kotlin锛屽洜姝ゆ垜浠喅瀹氬潥鎸佷娇鐢?Groovy 鏉ユ祴璇曘€?/span>
2018骞存槬澶╋紝浣跨敤 Kotlin 寮€鍙戝嚑涓湀涔嬪悗锛屾垜浠€荤粨浜?Kotlin 鐨勪紭缂虹偣锛屾渶缁堢粨璁鸿〃鏄?Kotlin 闄嶄綆浜嗘垜浠殑鐢熶骇鍔涖€?/span>
浜庢槸鎴戜滑浣跨敤 Java 鏉ラ噸鍐欒繖涓井鏈嶅姟椤圭洰銆?/span>
閭d箞 Kotlin 涓昏瀛樺湪鍝簺寮婄锛熶笅闈㈡潵涓€涓€瑙i噴銆?/span>
鍚嶇О閬斀
杩欐槸 Kotlin 鏈€璁╂垜闇囨儕鐨勫湴鏂广€傜湅鐪嬩笅闈㈣繖涓柟娉曪細
fun inc(num : Int) {
val num =
2
if (num >
0) {
val num =
3
}
println (
"num: " + num)
}
褰撲綘璋冪敤 inc(1) 浼氳緭鍑轰粈涔堝憿锛熷湪 Kotlin 涓? 鏂规硶鐨勫弬鏁版棤娉曚慨鏀癸紝鍥犳鍦ㄦ湰渚嬩腑浣犱笉鑳芥敼鍙?num銆傝繖涓璁″緢濂斤紝鍥犱负浣犱笉搴旇鏀瑰彉鏂规硶鐨勮緭鍏ュ弬鏁般€備絾鏄綘鍙互鐢ㄧ浉鍚岀殑鍚嶇О瀹氫箟鍙︿竴涓彉閲忓苟瀵瑰叾杩涜鍒濆鍖栥€?/span>
杩欐牱涓€鏉ワ紝杩欎釜鏂规硶浣滅敤鍩熶腑灏辨湁涓や釜鍚嶄负 num 鐨勫彉閲忋€傚綋鐒讹紝浣犱竴娆″彧鑳借闂叾涓竴涓?num锛屼絾鏄?num 鍊间細琚敼鍙樸€?/span>
鍦?if 璇彞涓啀娣诲姞鍙︿竴涓?num锛屽洜涓轰綔鐢ㄥ煙鐨勫師鍥?num 骞朵笉浼氳淇敼銆?/span>
浜庢槸锛屽湪 Kotlin 涓紝inc(1) 浼氳緭鍑?2銆傚悓鏍锋晥鏋滅殑 Java 浠g爜濡備笅鎵€绀猴紝涓嶈繃鏃犳硶閫氳繃缂栬瘧锛?nbsp;
void inc(int num) {
int num =
2;
//error: variable 'num' is already defined in the scope
if (num >
0) {
int num =
3;
//error: variable 'num' is already defined in the scope
}
System.
out.println (
"num: " + num);
}
鍚嶅瓧閬斀骞朵笉鏄?Kotlin 鍙戞槑鐨勶紝杩欏湪缂栫▼璇█涓緢甯歌銆傚湪 Java 涓垜浠範鎯敤鏂规硶鍙傛暟鏉ユ槧灏勭被瀛楁锛?/span>
public
class Shadow {
int
val;
public Shadow(int
val) {
this.
val =
val;
}
}
鍦?Kotlin 涓悕绉伴伄钄芥湁浜涗弗閲嶏紝杩欐槸 Kotlin 鍥㈤槦鐨勪竴涓璁$己闄枫€?/span>
IDEA 鍥㈤槦璇曞浘閫氳繃鍚戞瘡涓伄钄藉彉閲忔樉绀鸿鍛婁俊鎭潵瑙e喅杩欎釜闂銆備袱涓洟闃熷湪鍚屼竴瀹跺叕鍙稿伐浣滐紝鎴栬浠栦滑鍙互浜掔浉浜ゆ祦骞跺氨閬斀闂杈炬垚鍏辫瘑銆傛垜浠庝釜浜鸿搴﹁禐鎴?IDEA 鐨勫仛娉曞洜涓烘垜鎯充笉鍒版湁鍝簺搴旂敤鍦烘櫙闇€瑕侀伄钄芥柟娉曞弬鏁般€?/span>
绫诲瀷鎺ㄦ柇
鍦↘otlin涓紝褰撲綘澹版槑涓€涓獀ar鎴栨槸val锛屼綘閫氬父浼氳缂栬瘧鍣ㄤ粠鍙宠竟鐨勮〃杈惧紡绫诲瀷涓寽娴嬪彉閲忕被鍨嬨€傛垜浠О涔嬩负灞€閮ㄥ彉閲忕被鍨嬫帹鏂紝杩欏绋嬪簭鍛樻潵璇存槸涓€涓緢澶х殑鏀硅繘銆傚畠鍏佽鎴戜滑鍦ㄤ笉褰卞搷闈欐€佺被鍨嬫鏌ョ殑鎯呭喌涓嬬畝鍖栦唬鐮併€?/span>
渚嬪锛岃繖涓狵otlin浠g爜锛?/span>
var a =
"10"
Kotlin 缂栬瘧鍣ㄤ細灏嗗叾缈昏瘧鎴愶細
var a :
String =
"10"
Java 鍚屾牱鍏峰杩欎釜鐗规€э紝Java 10涓殑绫诲瀷鎺ㄦ柇绀轰緥濡備笅锛?nbsp;
var a =
"10";
瀹炶瘽瀹炶锛孠otlin 鍦ㄨ繖涓€鐐逛笂纭疄鏇磋儨涓€绛广€傚綋鐒讹紝绫诲瀷鎺ㄦ柇杩樺彲搴旂敤鍦ㄥ涓満鏅€?/span>
鍏充簬 Java 10涓殑灞€閮ㄥ彉閲忕被鍨嬫帹鏂紝鐐瑰嚮浠ヤ笅閾炬帴
浜嗚В鏇村锛?/span>
https://medium.com/@afinlay/java-10-sneak-peek-local-variable-type-inference-var-3022016e1a2b
Null 瀹夊叏绫诲瀷
Null 瀹夊叏绫诲瀷鏄?Kotlin 鐨勬潃鎵嬬骇鍔熻兘銆?/span>
杩欎釜鎯虫硶寰堝ソ锛屽湪 Kotlin 涓紝绫诲瀷榛樿涓嶅彲涓虹┖銆傚鏋滀綘闇€瑕佹坊鍔犱竴涓彲涓虹┖鐨勭被鍨嬶紝鍙互鍍忎笅鍒椾唬鐮佽繖鏍凤細
val a:
String? =
null
// ok
val b:
String =
null
// compilation error
鍋囪浣犱娇鐢ㄤ簡鍙负绌虹殑鍙橀噺浣嗘槸骞舵湭杩涜绌哄€兼鏌ワ紝杩欏湪 Kotlin 灏嗘棤娉曢€氳繃缂栬瘧锛屾瘮濡傦細
println (a.length)
// compilation error
println (a?.length)
// fine, prints null
println (a?.length ?:
0)
// fine, prints 0
閭d箞鏄笉鏄鏋滀綘鍚屾椂鎷ユ湁涓嶅彲涓虹┖鍜屽彲涓虹┖鐨勫彉閲忥紝灏卞彲浠ラ伩鍏?Java 涓渶甯歌鐨?NullPointerException 寮傚父鍚楋紵浜嬪疄骞舵病鏈夋兂璞$殑绠€鍗曘€?/span>
褰?Kotlin 浠g爜蹇呴』璋冪敤 Java 浠g爜鏃讹紝浜嬫儏浼氬彉寰楀緢绯熺硶锛屾瘮濡傚簱鏄敤 Java 缂栧啓鐨勶紝鎴戠浉淇¤繖绉嶆儏鍐靛緢甯歌銆備簬鏄涓夌绫诲瀷浜х敓浜嗭紝瀹冭绉颁负骞冲彴绫诲瀷銆侹otlin 鏃犳硶琛ㄧず杩欑濂囨€殑绫诲瀷锛屽畠鍙兘浠?Java 绫诲瀷鎺ㄦ柇鍑烘潵銆傚畠鍙兘浼氳瀵间綘锛屽洜涓哄畠瀵圭┖鍊煎緢瀹芥澗锛屽苟涓斾細绂佺敤 Kotlin 鐨?NULL 瀹夊叏鏈哄埗銆?/span>
鐪嬬湅涓嬮潰杩欎釜 Java 鏂规硶锛?/span>
public
class
Utils {
static String format(String text) {
return text.isEmpty() ?
null : text;
}
}
鍋囧浣犳兂璋冪敤 format(String)銆傚簲璇ヤ娇鐢ㄥ摢绉嶇被鍨嬫潵鑾峰緱杩欎釜 Java 鏂规硶鐨勭粨鏋滃憿锛熶綘鏈変笁涓€夋嫨銆?/span>
绗竴绉嶆柟娉曪細浣犲彲浠ヤ娇鐢?String锛屼唬鐮佺湅璧锋潵寰堝畨鍏紝浣嗘槸浼氭姏鍑?NullPointerException 寮傚父
銆?/span>
fun doSth(text: String) {
val f: String = Utils.format(text)
// compiles but assignment can throw NPE at runtime
println (
"f.len : " + f.length)
}
閭d綘灏遍渶瑕佺敤 Elvis 鏉ヨВ鍐宠繖涓棶棰橈細
fun doSth(text: String) {
val f: String = Utils.format(text) ?:
""
// safe with Elvis
println (
"f.len : " + f.length)
}
绗簩绉嶆柟娉曪細浣犲彲浠ヤ娇鐢?String锛岃兘澶熶繚璇?Null 瀹夊叏鎬с€?/span>
fun doSth(text: String) {
val f: String? = Utils.format(text)
// safe
println (
"f.len : " + f.length)
// compilation error, fine
println (
"f.len : " + f?.length)
// null-safe with ? operator
}
绗笁绉嶆柟娉曪細璁?Kotlin 鍋氬眬閮ㄥ彉閲忕被鍨嬫帹鏂浣曪紵
fun doSth(text: String) {
val f = Utils.format(text)
// f type inferred as String!
println (
"f.len : " + f.length)
// compiles but can throw NPE at runtime
}
棣婁富鎰忥紒杩欎釜 Kotlin 浠g爜鐪嬭捣鏉ュ緢瀹夊叏銆佸彲缂栬瘧锛屼絾鏄畠瀹瑰繊浜嗙┖鍊硷紝灏卞儚鍦?Java 涓竴鏍枫€?/span>
闄ゆ涔嬪锛岃繕鏈夊彟澶栦竴涓柟娉曪紝灏辨槸寮哄埗灏?f 绫诲瀷鎺ㄦ柇涓?String锛?/span>
fun doSth(text: String) {
val f = Utils.format(text)!!
// throws NPE when format() returns null
println (
"f.len : " + f.length)
}
鍦ㄦ垜鐪嬫潵锛孠otlin 鐨勬墍鏈夎繖浜涚被浼?scala 鐨勭被鍨嬬郴缁熻繃浜庡鏉傘€侸ava 浜掓搷浣滄€т技涔庢崯瀹充簡 Kotlin 绫诲瀷鎺ㄦ柇杩欎釜閲嶉噺绾у姛鑳姐€?/span>
绫诲悕绉板瓧闈㈠父閲?/span>
浣跨敤绫讳技 Log4j 鎴栬€?Gson 鐨?Java 搴撴椂锛岀被鏂囧瓧寰堝父瑙併€?/span>
Java 浣跨敤 .class 鍚庣紑缂栧啓绫诲悕锛?nbsp;
Gson gson =
new GsonBuilder().registerTypeAdapter(LocalDate.class,
new LocalDateAdapter()).create();
Groovy 鎶婄被杩涜浜嗚繘涓€姝ョ殑绠€鍖栥€備綘鍙互蹇界暐 .class锛屽畠鏄?Groovy 鎴栬€?Java 绫诲苟涓嶉噸瑕併€?/span>
def gson = new GsonBuilder().registerTypeAdapter(LocalDate, new LocalDateAdapter()).create()
Kotlin 鎶?Kotlin 绫诲拰 Java 绫昏繘琛屼簡鍖哄垎锛屽苟涓哄叾鎻愪緵浜嗚娉曡鑼冿細
val kotlinClass : KClass<LocalDate> = LocalDate::
class
val javaClass : Class<LocalDate> = LocalDate::
class.java
鍥犳鍦?Kotlin 涓紝浣犲繀椤诲啓鎴愬涓嬪舰寮忥細
val gson = GsonBuilder().registerTypeAdapter(LocalDate::
class.java, LocalDateAdapter()).create()
杩欑湅璧锋潵闈炲父涓戦檵銆?/span>
鍙嶅悜绫诲瀷澹版槑
C 绯诲垪鐨勭紪绋嬭瑷€鏈夋爣鍑嗙殑澹版槑绫诲瀷鐨勬柟娉曘€傜畝鑰岃█涔嬶紝棣栧厛鎸囧畾涓€涓被鍨嬶紝鐒跺悗鏄绗﹀悎绫诲瀷鐨勪笢瑗匡紝姣斿鍙橀噺銆佸瓧娈点€佹柟娉曠瓑绛夈€?/span>
Java 涓殑琛ㄧず鏂规硶鏄細
int inc(int i) {
return i +
1;
}
Kotlin 涓垯鏄細
fun inc(i: Int):
Int {
return i +
1
}
杩欑鏂规硶鏈夊嚑涓師鍥犱护浜鸿鍘屻€?/span>
棣栧厛锛屼綘闇€瑕佸湪鍚嶇О鍜岀被鍨嬩箣闂村姞鍏ヨ繖涓浣欑殑鍐掑彿銆傝繖涓澶栬鑹茬殑鐩殑鏄粈涔堬紵涓轰粈涔堝悕绉颁笌鍏剁被鍨嬭鍒嗙锛熸垜涓嶇煡閬撱€傚彲鎮茬殑鏄紝杩欒浣犲湪 Kotlin 鐨勫伐浣滃彉寰楁洿鍔犲洶闅俱€?/span>
绗簩涓棶棰橈紝褰撲綘璇诲彇涓€涓柟娉曞0鏄庢椂锛屼綘棣栧厛鐪嬪埌鐨勬槸鍚嶅瓧鍜岃繑鍥炵被鍨嬶紝鐒跺悗鎵嶆槸鍙傛暟銆?/span>
鍦?Kotlin 涓紝鏂规硶鐨勮繑鍥炵被鍨嬪彲鑳借繙鍦ㄨ灏撅紝鎵€浠ラ渶瑕佹祻瑙堝緢澶氫唬鐮佹墠鑳界湅鍒帮細
private
fun getMetricValue(kafkaTemplate : KafkaTemplate<String, ByteArray>, metricName : String) :
Double {
...
}
鎴栬€咃紝濡傛灉鍙傛暟鏄€愯鏍煎紡鐨勶紝鍒欓渶瑕佹悳绱€傞偅涔堟垜浠渶瑕佸灏戞椂闂存墠鑳芥壘鍒版鏂规硶鐨勮繑鍥炵被鍨嬪憿锛?/span>
@Bean
fun kafkaTemplate(
@Value("${interactions.kafka.bootstrap-servers-dc1}") bootstrapServersDc1: String,
@Value("${interactions.kafka.bootstrap-servers-dc2}") bootstrapServersDc2: String,
cloudMetadata: CloudMetadata,
@Value("${interactions.kafka.batch-size}") batchSize: Int,
@Value("${interactions.kafka.linger-ms}") lingerMs: Int,
metricRegistry : MetricRegistry
): KafkaTemplate<String, ByteArray> {
val bootstrapServer =
if (cloudMetadata.datacenter ==
"dc1") {
bootstrapServersDc1
}
...
}
绗笁涓棶棰樻槸 IDE 涓殑鑷姩鍖栨敮鎸佷笉澶熷ソ銆傛爣鍑嗗仛娉曚粠绫诲瀷鍚嶇О寮€濮嬶紝骞朵笖寰堝鏄撴壘鍒扮被鍨嬨€備竴鏃﹂€夋嫨涓€涓被鍨嬶紝IDE 浼氭彁渚涗竴浜涘叧浜庡彉閲忓悕鐨勫缓璁紝杩欎簺鍙橀噺鍚嶆槸浠庨€夊畾鐨勭被鍨嬫淳鐢熺殑锛屽洜姝や綘鍙互蹇€熻緭鍏ヨ繖鏍风殑鍙橀噺锛?nbsp;
MongoExperimentsRepository repository
Kotlin 灏界鏈?IntelliJ 杩欐牱寮哄ぇ鐨?IDE锛岃緭鍏ュ彉閲忎粛鐒舵槸寰堥毦鐨勩€傚鏋滀綘鏈夊涓瓨鍌ㄥ簱锛屽湪鍒楄〃涓緢闅惧疄鐜版纭殑鑷姩琛ュ叏锛岃繖鎰忓懗鐫€浣犱笉寰椾笉鎵嬪姩杈撳叆瀹屾暣鐨勫彉閲忓悕绉般€?/span>
repository : MongoExperimentsRepository
浼寸敓瀵硅薄
涓€浣?Java 绋嬪簭鍛樻潵鍒?Kotlin 闈㈠墠銆?/span>
鈥滃棬锛孠otlin銆傛垜鏄柊鏉ョ殑锛屾垜鍙互浣跨敤闈欐€佹垚鍛樺悧锛?浠栭棶銆?/span>
鈥滀笉琛屻€傛垜鏄潰鍚戝璞$殑锛岄潤鎬佹垚鍛樹笉鏄潰鍚戝璞$殑銆傗€?Kotlin 鍥炵瓟銆?/span>
鈥滃ソ鍚э紝浣嗘垜闇€瑕?MyClass 鐨?logger锛屾垜璇ユ€庝箞鍔烇紵鈥?nbsp;
鈥滆繖涓病闂锛屼娇鐢ㄤ即鐢熷璞″嵆鍙€傗€?/span>
鈥滈偅鏄粈涔堜笢瑗匡紵鈥?鈥滆繖鏄眬闄愬埌浣犵殑绫荤殑鍗曠嫭瀵硅薄銆傛妸浣犵殑 logger 鏀惧湪浼寸敓瀵硅薄涓€傗€滽otlin瑙i噴璇淬€?/span>
鈥滄垜鎳備簡銆傝繖鏍峰鍚楋紵鈥?/span>
class MyClass {
companion
object {
val logger = LoggerFactory.getLogger(MyClass::
class.java)
}
}
鈥滄纭紒鈥?/span>
鈥滃緢璇︾粏鐨勮娉曪紝鈥濈▼搴忓憳鐪嬭捣鏉ュ緢鐤戞儜锛屸€滀絾鏄病鍏崇郴锛岀幇鍦ㄦ垜鍙互鍍?MyClass.logger 杩欐牱璋冪敤鎴戠殑 logger锛屽氨鍍?Java 涓殑涓€涓潤鎬佹垚鍛橈紵鈥?nbsp;
鈥滃棷......鏄殑锛屼絾瀹冧笉鏄潤鎬佹垚鍛橈紒杩欓噷鍙湁瀵硅薄銆傛妸瀹冪湅浣滄槸宸茬粡瀹炰緥鍖栦负鍗曚緥鐨勫尶鍚嶅唴閮ㄧ被銆備簨瀹炰笂锛岃繖涓被骞朵笉鏄尶鍚嶇殑锛屽畠鐨勫悕瀛楁槸 Companion锛屼絾浣犲彲浠ョ渷鐣ヨ繖涓悕瀛椼€傜湅鍒颁簡鍚楋紵杩欏緢绠€鍗曘€?
鎴戝緢娆h祻瀵硅薄澹版槑鐨勬蹇碘€斺€斿崟渚嬪緢鏈夌敤銆備絾浠庤瑷€涓垹闄ら潤鎬佹垚鍛樻槸涓嶅垏瀹為檯鐨勩€傚湪 Java 涓垜浠娇鐢ㄩ潤鎬?Logger 寰堢粡鍏革紝瀹冨彧鏄竴涓?Logger锛屾墍浠ユ垜浠笉鍏冲績闈㈠悜瀵硅薄鐨勭函搴︺€傚畠鑳藉宸ヤ綔锛屼粠鏉ユ病鏈変换浣曞潖澶勩€?/span>
鍥犱负鏈夋椂鍊欎綘蹇呴』浣跨敤闈欐€併€傛棫鐗堟湰 public static void main() 浠嶇劧鏄惎鍔?Java 搴旂敤绋嬪簭鐨勫敮涓€鏂瑰紡銆?/span>
class AppRunner {
companion
object {
@JvmStatic
fun main(args: Array<String>) {
SpringApplication.run(AppRunner::
class.java, *args)
}
}
}
闆嗗悎瀛楅潰閲?/span>
鍦↗ava涓紝鍒濆鍖栧垪琛ㄩ潪甯哥箒鐞愶細
import java.util.Arrays;
...
List<
String> strings = Arrays.asList(
"Saab",
"Volvo");
鍒濆鍖栧湴鍥鹃潪甯稿啑闀匡紝寰堝浜轰娇鐢?Guava锛?/span>
import com.google.common.collect.ImmutableMap;
...
Map<
String,
String> string = ImmutableMap.of(
"firstName",
"John",
"lastName",
"Doe");
鍦?Java 涓紝鎴戜滑浠嶇劧鍦ㄧ瓑寰呮柊鐨勮娉曟潵琛ㄨ揪闆嗗悎鍜屾槧灏勩€傝娉曞湪璁稿璇█涓潪甯歌嚜鐒跺拰鏂逛究銆?/span>
JavaScript锛?/span>
const
list = [
'Saab',
'Volvo']
const map = {
'firstName':
'John',
'lastName' :
'Doe'}
Python锛?/span>
list = [
'Saab',
'Volvo']
map = {
'firstName':
'John',
'lastName':
'Doe'}
Groovy锛?/span>
def list = [
'Saab',
'Volvo']
def map = [
'firstName':
'John',
'lastName':
'Doe']
绠€鍗曟潵璇达紝闆嗗悎瀛楅潰閲忕殑鏁撮綈璇硶灏辨槸浣犲鐜颁唬缂栫▼璇█鐨勬湡鏈涳紝鐗瑰埆鏄鏋滃畠鏄粠澶村紑濮嬪垱寤虹殑銆侹otlin 鎻愪緵浜嗕竴绯诲垪鍐呯疆鍑芥暟锛屾瘮濡?listOf()銆乵utableListOf()銆乵apOf()銆乭ashMapOf() 绛夌瓑銆?/span>
Kotlin锛?nbsp;
val
list = listOf(
"Saab",
"Volvo")
val
map = mapOf(
"firstName" to
"John",
"lastName" to
"Doe")
鍦ㄥ湴鍥句腑锛岄敭鍜屽€间笌 to 杩愮畻绗﹂厤瀵癸紝杩欏緢濂姐€備絾涓轰粈涔堜竴鐩存病鏈夊緱鍒板箍娉涗娇鐢ㄥ憿锛熶护浜哄け鏈涖€?/span>
Maybe
鍑芥暟寮忚瑷€锛堟瘮濡?Haskell锛夋病鏈夌┖鍊笺€傜浉鍙嶏紝浠栦滑鎻愪緵 Maybe monad锛堝鏋滀綘涓嶇啛鎮塵onad锛岃闃呰 Tomasz Nurkiewicz 鐨勮繖绡囨枃绔狅細
http://www.nurkiewicz.com/2016/06/functor-and-monad-examples-in-plain-java.html
锛夈€?/span>
Maybe 寰堜箙浠ュ墠灏辫 Scala 浠?Option 寮曞叆鍒?JVM 涓栫晫锛岀劧鍚庡湪 Java 8 涓閲囩敤涓?Optional銆傚浠婏紝Optional 鏄湪 API 杈圭晫澶勭悊杩斿洖绫诲瀷涓殑绌哄€肩殑闈炲父娴佽鐨勬柟寮忋€?/span>
Kotlin 涓病鏈?Optional 鐨勭瓑浠风墿锛屾墍浠ヤ綘澶ф搴旇浣跨敤 Kotlin 鐨勫彲绌虹被鍨嬨€傝鎴戜滑鏉ヨ皟鏌ヤ竴涓嬭繖涓棶棰樸€?/span>
閫氬父鎯呭喌涓嬶紝褰撲綘鏈変竴涓?Optional 鐨勬椂鍊欙紝浣犳兂瑕佸簲鐢ㄤ竴绯诲垪鏃犳晥鐨勮浆鎹€?/span>
渚嬪锛屽湪 Java 涓細
public int parseAndInc(String number) {
return Optional.ofNullable(number)
.
map(Integer::parseInt)
.
map(it -> it +
1)
.orElse(
0);
}
鍦?Kotlin 涓紝涓轰簡鏄犲皠浣犲彲浠ヤ娇鐢?let 鍑芥暟锛?/span>
fun parseAndInc(number: String?):
Int {
return number.let { Integer.parseInt(it) }
.let { it -> it +
1 } ?:
0
}
涓婇潰鐨勪唬鐮佹槸閿欒鐨勶紝parseInt() 浼氭姏鍑?NPE 銆俶ap() 浠呭湪鏈夊€兼椂鎵ц銆傚惁鍒欙紝Null 灏变細璺宠繃锛岃繖灏辨槸涓轰粈涔?map() 濡傛鏂逛究銆備笉骞哥殑鏄紝Kotlin 鐨?let 涓嶄細閭f牱宸ヤ綔銆傚畠浠庡乏渚х殑鎵€鏈夊唴瀹逛腑璋冪敤锛屽寘鎷┖鍊笺€?/span>
涓轰簡淇濊瘉杩欎釜浠g爜 Null 瀹夊叏锛屼綘蹇呴』鍦ㄦ瘡涓唬鐮佷箣鍓嶆坊鍔?let锛?nbsp;
fun parseAndInc(number: String?):
Int {
return number?.let { Integer.parseInt(it) }
?.let { it -> it +
1 } ?:
0
}
鐜板湪锛屾瘮杈?Java 鍜?Kotlin 鐗堟湰鐨勫彲璇绘€с€備綘鏇村€惧悜鍝釜锛?/span>
鏁版嵁绫?/span>
鏁版嵁绫绘槸 Kotlin 鍦ㄥ疄鐜?Value Objects 鏃朵娇鐢ㄧ殑鏂规硶锛屼互鍑忓皯 Java 涓笉鍙伩鍏嶇殑鏍锋澘闂銆?/span>
渚嬪锛屽湪 Kotlin 涓紝浣犲彧鍐欎竴涓?Value Object 锛?/span>
data
class User(
val name: String,
val age:
Int)
Kotlin 瀵?equals()銆乭ashCode()銆乼oString() 浠ュ強 copy() 鏈夊緢濂界殑瀹炵幇銆傚湪瀹炵幇绠€鍗曠殑DTO 鏃跺畠闈炲父鏈夌敤銆備絾璇疯浣忥紝鏁版嵁绫诲甫鏈変弗閲嶇殑灞€闄愭€с€備綘鏃犳硶鎵╁睍鏁版嵁绫绘垨鑰呭皢鍏舵娊璞″寲锛屾墍浠ヤ綘鍙兘涓嶄細鍦ㄦ牳蹇冩ā鍨嬩腑浣跨敤瀹冧滑銆?/span>
杩欎釜闄愬埗涓嶆槸 Kotlin 鐨勯敊銆傚湪 equals() 娌℃湁杩濆弽 Liskov 鍘熷垯鐨勬儏鍐典笅锛屾病鏈夊姙娉曚骇鐢熸纭殑鍩轰簬浠峰€肩殑鏁版嵁銆?/span>
杩欎篃鏄负浠€涔?Kotlin 涓嶅厑璁告暟鎹被缁ф壙鐨勫師鍥犮€?/span>
寮€鏀剧被
Kotlin 绫婚粯璁や负 final銆傚鏋滀綘鎯虫墿灞曚竴涓被锛屽繀椤绘坊鍔?open 淇グ绗︺€?/span>
缁ф壙璇硶濡備笅鎵€绀猴細
open
class Base
class Derived : Base()
Kotlin 灏?extends 鍏抽敭瀛楁洿鏀逛负: 杩愮畻绗︼紝璇ヨ繍绠楃鐢ㄤ簬灏嗗彉閲忓悕绉颁笌鍏剁被鍨嬪垎寮€銆傞偅涔堝啀鍥炲埌 C ++璇硶锛熷鎴戞潵璇磋繖寰堟贩涔便€?/span>
杩欓噷鏈変簤璁殑鏄紝榛樿鎯呭喌涓嬬被鏄?final銆備篃璁?Java 绋嬪簭鍛樿繃搴︿娇鐢ㄧ户鎵匡紝涔熻搴旇鍦ㄨ€冭檻鎵╁睍绫讳箣鍓嶈€冭檻涓夋銆備絾鎴戜滑鐢熸椿鍦ㄦ鏋朵笘鐣岋紝Spring 浣跨敤 cglib銆乯assist 搴撲负浣犵殑 bean 鐢熸垚鍔ㄦ€佷唬鐞嗐€侶ibernate 鎵╁睍浣犵殑瀹炰綋浠ュ惎鐢ㄥ欢杩熷姞杞姐€?/span>
濡傛灉浣犱娇鐢?Spring锛屼綘鏈変袱绉嶉€夋嫨銆備綘鍙互鍦ㄦ墍鏈?bean 绫荤殑鍓嶉潰娣诲姞 open锛屾垨鑰呬娇鐢ㄨ繖涓紪璇戝櫒鎻掍欢锛?nbsp;
buildscript {
dependencies {
classpath group:
'org.jetbrains.kotlin', name:
'kotlin-allopen', version:
"$versions.kotlin"
}
}
闄″抄鐨勫涔犳洸绾?/span>
濡傛灉浣犺涓鸿嚜宸辨湁 Java 鍩虹灏卞彲浠ュ揩閫熷涔?Kotlin锛岄偅浣犲氨閿欎簡銆侹otlin 浼氳浣犻櫡鍏ユ繁娓婏紝浜嬪疄涓婏紝Kotlin 鐨勮娉曟洿鎺ヨ繎 Scala銆傝繖鏄竴椤硅祵娉紝浣犲皢涓嶅緱涓嶅繕璁?Java 骞跺垏鎹㈠埌瀹屽叏涓嶅悓鐨勮瑷€銆?/span>
鐩稿弽锛屽涔?Groovy 鏄竴涓剦蹇殑杩囩▼銆侸ava 浠g爜鏄纭殑 Groovy 浠g爜锛屽洜姝や綘鍙互閫氳繃灏嗘枃浠舵墿灞曞悕浠?.java 鏇存敼涓?.groovy銆?/span>
鏈€鍚庣殑鎯虫硶
瀛︿範鏂版妧鏈氨鍍忎竴椤规姇璧勩€傛垜浠姇鍏ユ椂闂达紝鏂版妧鏈鎴戜滑寰楀埌鍥炴姤銆備絾鎴戝苟涓嶆槸璇?Kotlin 鏄竴绉嶇碂绯曠殑璇█锛屽彧鏄?/span>
鍦ㄦ垜浠殑妗堜緥涓紝鎴愭湰杩滆秴鏀剁泭銆?/span>
浠ヤ笂鍐呭缂栬瘧鑷?From Java to Kotlin and Back Again锛?/span>浣滆€?Kotlin ketckup銆?/span>
浠栨槸涓€鍚嶅叿鏈?5骞翠互涓婁笓涓氱粡楠岀殑杞欢宸ョ▼甯堬紝涓撴敞浜嶫VM 銆傚湪 Allegro锛屼粬鏄竴鍚嶅紑鍙戝洟闃熻礋璐d汉锛孞aVers 椤圭洰璐熻矗浜猴紝Spock 鍊″鑰呫€傛澶栵紝浠栬繕鏄?allegro.tech/blog 鐨勪富缂栥€?/span>
鏈枃涓€鍑哄氨寮曞彂浜嗕笟鍐呯殑骞挎硾浜夎锛?span class="mq-680">Kotlin 璇█鎷ユ姢鑰?nbsp;M谩rton Braun 灏辫〃绀轰簡寮虹儓鐨勫弽瀵广€?/span>
M谩rton Braun 鍗佸垎
鍠滄 Kotlin 缂栫▼锛岀洰鍓嶄粬鍦?StackOverflow 涓?Kotlin 鏍囩鐨勬渶楂樼敤鎴峰垪琛ㄤ腑鎺掑悕绗笁锛屽苟涓旀槸涓や釜寮€婧?Kotlin 搴撶殑鍒涘缓鑰咃紝鏈€钁楀悕鐨勬槸 MaterialDrawerKt銆傛澶栦粬杩樻槸 Autosoft 鐨?Android 寮€鍙戜汉鍛橈紝鐩墠姝e湪甯冭揪浣╂柉鎶€鏈粡娴庡ぇ瀛︽敾璇昏绠楁満宸ョ▼纭曞+瀛︿綅銆?/span>
浠ヤ笅灏辨槸浠栭拡瀵逛笂鏂囩殑鍙嶉┏锛?/span>
褰撴垜绗竴娆$湅鍒拌繖绡囨枃绔犳椂锛屾垜灏辨兂鎶婂畠杞彂鍑烘潵鐪嬬湅澶у浼氭€庝箞鎯筹紝鎴戣偗瀹氬畠浼氭槸涓€涓湁浜夎鐨勮瘽棰樸€?/span>
鍚庢潵鎴戣浜嗚繖绡囨枃绔狅紝鏋滅劧璇佹槑浜嗗畠鏄竴绉嶄富瑙傜殑銆佷笉鐪熷疄鐨勩€佺敋鑷虫湁浜涘眳楂樹复涓嬬殑鍋忚銆?/span>
鏈変簺浜哄凡缁忓湪鍘熻创涓嬭繘琛屼簡鍚堢悊鐨勬壒璇勶紝瀵规鎴戜篃鎯宠〃杈句竴涓嬭嚜宸辩殑鐪嬫硶銆?/span>
鍚嶇О閬斀
鈥淚DEA 鍥㈤槦鈥濓紙鎴栬€?Kotlin 鎻掍欢鍥㈤槦锛夊拰鈥淜otlin 鍥㈤槦鈥濊偗瀹氭槸鍚屾牱鐨勪汉锛屾垜浠庝笉璁や负鍐呴儴鍐茬獊浼氭槸涓ソ浜嬨€?/span>
璇█鎻愪緵杩欎釜鍔熻兘缁欎綘锛屼綘闇€瑕佺殑璇濆氨浣跨敤锛屽鏋滆鍘岋紝璋冩暣妫€鏌ヨ缃氨鏄簡銆?/span>
绫诲瀷鎺ㄦ柇
Kotlin 鐨勭被鍨嬫帹鏂棤澶勪笉鍦紝浣滆€呰鐨?Java 10 鍚屾牱鍙互绠€鐩存槸鍦ㄥ紑鐜╃瑧銆?/span>
Kotlin 鐨勬柟寮忚秴瓒婁簡鎺ㄦ柇灞€閮ㄥ彉閲忕被鍨嬫垨杩斿洖琛ㄨ揪寮忎綋鐨勫嚱鏁扮被鍨嬨€傝繖閲屼粙缁嶇殑杩欎袱涓緥瀛愭槸閭d簺鍒氬垰鐪嬭繃鍏充簬 Kotlin 鐨勭涓€娆′粙缁嶆€ц璇濈殑浜轰細鎻愬埌鐨勶紝鑰屼笉鏄偅浜涜姳浜嗗崐骞村涔犺璇█鐨勪汉銆?/span>
渚嬪锛屼綘鎬庝箞鑳戒笉鎻?Kotlin 鎺ㄦ柇娉涘瀷绫诲瀷鍙傛暟鐨勬柟寮忥紵杩欎笉鏄?Kotlin 鐨勪竴娆℃€у姛鑳斤紝瀹冩繁娣辫瀺鍏ヤ簡鏁翠釜璇█銆?/span>
缂栬瘧鏃?Null 瀹夊叏
杩欎釜鎵硅瘎鏄鐨勶紝褰撲綘涓?Java 浠g爜杩涜浜掓搷浣滄椂锛孨ull 瀹夊叏鎬х‘瀹炶鐮村潖浜嗐€傝璇█鑳屽悗鐨勫洟闃熸浘澶氭澹版槑锛屼粬浠渶鍒濊瘯鍥句娇 Java 鍙负绌虹殑姣忕绫诲瀷锛屼絾浠栦滑鍙戠幇瀹冨疄闄呬笂璁╀唬鐮佸彉寰楁洿绯熺硶銆?/span>
Kotlin 涓嶆瘮 Java 鏇村樊锛屼綘鍙渶瑕佹敞鎰忎娇鐢ㄧ粰瀹氬簱鐨勬柟寮忥紝灏卞儚鍦?Java 涓娇鐢ㄥ畠涓€鏍凤紝鍥犱负瀹冨苟娌℃湁涓嶅幓鑰冭檻 Null 瀹夊叏銆傚鏋?Java 搴撳叧蹇?Null
瀹夊叏鎬э紝鍒欏畠浠細鏈夎澶氭敮鎸佹敞閲婂彲渚涙坊鍔犮€?/span>
涔熻鍙互娣诲姞涓€涓紪璇戝櫒鏍囧織锛屼娇姣忕 Java 绫诲瀷閮藉彲浠ヤ负绌猴紝浣嗚繖瀵?Kotlin 鍥㈤槦鏉ヨ涓嶅緱涓嶈姳璐瑰ぇ閲忛澶栬祫婧愩€?/span>
绫诲悕绉板瓧闈㈠父閲?/span>
:: class 涓轰綘鎻愪緵浜嗕竴涓?KClass 瀹炰緥锛屼互渚夸笌 Kotlin 鑷繁鐨勫弽灏?API 涓€璧蜂娇鐢紝鑰?: class.java涓轰綘鎻愪緵浜嗙敤浜?Java 鍙嶅皠鐨勫父瑙?Java 绫诲疄渚嬨€?/span>
鍙嶅悜绫诲瀷澹版槑
涓轰簡娓呮璧疯锛岄鍊掔殑椤哄簭鏄瓨鍦ㄧ殑锛岃繖鏍蜂綘灏卞彲浠ヤ互鍚堢悊鐨勬柟寮忕渷鐣ユ樉寮忕被鍨嬨€?/span>
鍐掑彿鍙槸璇硶锛岃繖鍦ㄧ幇浠h瑷€涓槸鐩稿綋鏅亶鐨勪竴绉嶏紝姣斿 Scala銆丼wift 绛夈€?/span>
鎴戜笉鐭ラ亾浣滆€呭湪浣跨敤浠€涔?IntelliJ锛屼絾鎴戜娇鐢ㄧ殑鍙橀噺鍚嶇О鍜岀被鍨嬮兘鑳藉鑷姩琛ュ叏銆傚浜庡弬鏁帮紝IntelliJ 鐢氳嚦浼氱粰浣犳彁渚涚浉鍚岀被鍨嬬殑鍚嶇О鍜岀被鍨嬬殑寤鸿锛岃繖瀹為檯涓婃瘮 Java 鏇村ソ銆?/span>
浼寸敓瀵硅薄
鍘熸枃涓锛?/span>
鏈夋椂鍊欎綘蹇呴』浣跨敤闈欐€併€傛棫鐗堟湰 public static void main() 浠嶇劧鏄惎鍔?Java 搴旂敤绋嬪簭鐨勫敮涓€鏂瑰紡銆?/span>
class AppRunner {
companion
object {
@JvmStatic
fun main(args: Array<String>) {
SpringApplication.run(AppRunner::
class.java, *args)
}
}
}
瀹為檯涓婏紝杩欎笉鏄惎鍔?Java 搴旂敤绋嬪簭鐨勫敮涓€鏂瑰紡銆備綘鍙互杩欐牱鍋氾細
fun main(args锛欰rray <String>){ SpringApplication.run(AppRunner ::
class.java锛? args)}
鎴栬€呰繖鏍凤細
fun main(args锛欰rray <String>){ runApplication <AppRunner>(* args)}
闆嗗悎瀛楅潰閲?/span>
浣犲彲浠ュ湪娉ㄩ噴涓娇鐢ㄦ暟缁勬枃瀛椼€備絾鏄紝闄ゆ涔嬪锛岃繖浜涢泦鍚堝伐鍘傜殑鍔熻兘闈炲父绠€娲侊紝鑰屼笖瀹冧滑鏄彟涓€绉嶁€滃唴缃€濆埌璇ヨ瑷€鐨勪笢瑗匡紝鑰屽畠浠疄闄呬笂鍙槸搴撳嚱鏁般€?/span>
浣犲彧鏄姳鎬ㄤ娇鐢?杩涜绫诲瀷澹版槑銆傝€屼笖锛屼负浜嗚幏寰楀畠涓嶅繀鏄崟鐙殑璇█缁撴瀯鐨勫ソ澶勶紝瀹冨彧鏄竴涓换浣曚汉閮藉彲浠ュ疄鐜扮殑鍔熻兘銆?/span>
Maybe
濡傛灉浣犲枩娆?Optional 锛屼綘鍙互浣跨敤瀹冦€侹otlin 鍦?JVM 涓婅繍琛屻€?/span>
瀵逛簬浠g爜纭疄杩欐湁浜涢毦鐪嬨€備絾鏄綘涓嶅簲璇ュ湪 Kotlin 浠g爜涓娇鐢?parseInt锛岃€屽簲璇ヨ繖鏍峰仛锛堟垜涓嶇煡閬撲綘浣跨敤璇ヨ瑷€鐨?6 涓湀涓负浣曢敊杩囪繖涓級銆備綘涓轰粈涔堣鏄庣‘鍦板懡鍚嶄竴涓?Lambda 鍙傛暟鍛紵
鏁版嵁绫?/span>
鍘熸枃涓锛?/span>
杩欎釜闄愬埗涓嶆槸 Kotlin 鐨勯敊銆傚湪 equals() 娌℃湁杩濆弽 Liskov 鍘熷垯鐨勬儏鍐典笅锛屾病鏈夊姙娉曚骇鐢熸纭殑鍩轰簬浠峰€肩殑鏁版嵁銆?/span>
杩欏氨鏄负浠€涔?Kotlin 涓嶅厑璁告暟鎹被缁ф壙鐨勫師鍥犮€?/span>
鎴戜笉鐭ラ亾浣犱负浠€涔堟彁鍑鸿繖涓棶棰樸€傚鏋滀綘闇€瑕佹洿澶嶆潅鐨勭被锛屼綘浠嶇劧鍙互鍒涘缓瀹冧滑骞舵墜鍔ㄧ淮鎶ゅ畠浠殑 equals銆乭ashCode 绛夋柟娉曘€傛暟鎹被浠呬粎鏄竴涓畝鍗曠敤渚嬬殑渚挎嵎鏂瑰紡锛屽浜庡緢澶氫汉鏉ヨ杩欏緢甯歌銆?/span>
鍏紑绫?/span>
浣滆€呭啀娆¢剻瑙嗕簡锛屽姝ゆ垜瀹炲湪鏃犺瘽鍙銆?/span>
闄″抄鐨勫涔犳洸绾?/span>
浣滆€呰涓哄涔?Kotlin 寰堥毦锛?浣嗘槸鎴戜釜浜哄苟涓嶈繖涔堣涓恒€?/span>
鏈€鍚庣殑鎯虫硶
浠庝綔鑰呭垪涓剧殑渚嬪瓙涓紝鎴戞劅瑙変粬鍙槸浜嗚В璇█鐨勮〃闈€?/span>
寰堥毦鎯宠薄浠栧姝ゆ湁鎶曞叆寰堝鏃堕棿銆?/span>
鏈€杩戠殑璇█鎺掕锛宩ava渚濈劧鎺掑湪绗竴锛屽湪璇█鎺掕姒滀笂锛孠oltin宸茶鐢╁埌10鍚嶄箣澶栵紒
鍓嶇嚎鎺ㄥ嚭瀛︿範浜ゆ祦缇や竴瀹氳澶囨敞锛?/span>
鐮旂┒/宸ヤ綔鏂瑰悜+鍦扮偣+瀛︽牎/鍏徃+鏄电О锛堝鐩爣妫€娴?涓婃捣+涓婁氦+鍙彲锛?/span>
锛屾牴鎹牸寮忓娉紝鍙洿蹇閫氳繃涓旈個璇疯繘缇?/span>
鎵爜鍔犳垜寰俊杩涚兢锛屽唴鎺ㄥ拰鎶€鏈氦娴侊紝澶т浆浠浂璺濈
END
鍘嗗彶鎺ㄨ崘
鐐逛釜鍦ㄧ湅鍚?/span>
以上是关于鍐嶈锛孠otlin 锛佷綘濂? Java 锛?/h1>
Posted 寮€鍙戣€呮妧鏈墠绾?/a>
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍐嶈锛孠otlin 锛佷綘濂? Java 锛?/h1>
相关的知识,希望对你有一定的参考价值。
鐐瑰嚮鈥?/span>
寮€鍙戣€呮妧鏈墠绾?/span>
鈥濓紝閫夋嫨鈥滄槦鏍囸煍濃€?/span>
浣滆€咃細Bartosz Walacik | 鏉ヨ嚜锛欳SDN | 缂栬緫 锛氬彲鍙?/span>
鍥炲鈥?66鈥濓紝鑾峰彇涓€浠戒笓灞炲ぇ绀煎寘
鍘熸枃锛歨ttps://allegro.tech/2018/05/From-Java-to-Kotlin-and-Back-Again.html
姣棤鐤戦棶锛孠otlin 鍦ㄥ幓骞村緢鍙楁杩庯紝涓氱晫鐢氳嚦鏈変汉璁や负鍏跺皢
鍙栦唬 Java 鐨勯湼涓诲湴浣?/span>
銆傚畠鎻愪緵浜?Null 瀹夊叏鎬э紝浠庤繖涓€鐐规潵璇村畠纭疄姣?Java 鏇村ソ銆傞偅涔堟槸涓嶆槸杩欏氨鎰忓懗鐫€寮€鍙戣€呭簲璇ユ涓嶇姽璞湴鎷ユ姳 Kotlin锛屽惁鍒欏氨钀戒紞浜嗭紵
绛夌瓑锛屾垨璁镐簨鎯呭苟闈炲姝ゃ€?/span>
鍦ㄥ紑濮嬩娇鐢?Kotlin 缂栫▼涔嬪墠锛屾湰鏂囨兂瑕佸垎浜釜鏁呬簨缁欎綘銆傚湪杩欎釜鏁呬簨涓紝浣滆€呮渶鏃╀娇鐢?Kotlin 鏉ョ紪鍐欎竴涓」鐩紝鍚庢潵 Kotlin 鐨勫悇绉嶆€紓妯″紡浠ュ強涓€浜涘叾浠栭殰纰嶈秺鏉ヨ秺璁╀汉鍘岀儲锛屾渶缁堬紝浠栦滑鍐冲畾閲嶅啓杩欎釜椤圭洰銆?/span>
浠ヤ笅涓鸿瘧鏂囷細
涓€鐩翠互鏉ワ紝鎴戝鍩轰簬 JVM 鐨勮瑷€閮介潪甯告儏鏈夌嫭閽熴€傛垜閫氬父浼氱敤 Java 鏉ョ紪鍐欎富绋嬪簭锛屽啀鐢?Groovy 缂栧啓娴嬭瘯浠g爜锛屼袱鑰呴厤鍚堜娇鐢ㄥ緱蹇冨簲鎵嬨€?/span>
2017骞村澶╋紝鍥㈤槦鍙戣捣浜嗕竴涓柊鐨勫井鏈嶅姟椤圭洰锛屽拰寰€甯镐竴鏍凤紝鎴戜滑闇€瑕佸缂栫▼璇█鍜屾妧鏈繘琛岄€夊瀷銆傞儴鍒嗗洟闃熸垚鍛樻槸 Kotlin 鐨勬嫢鎶よ€咃紝鍐嶅姞涓婃垜浠兘鎯冲皾璇曚竴涓嬫柊鐨勪笢瑗匡紝浜庢槸鎴戜滑鍐冲畾鐢?Kotlin 鏉ュ紑鍙戣繖涓」鐩€傜敱浜?Spock 娴嬭瘯妗嗘灦涓嶆敮鎸?Kotlin锛屽洜姝ゆ垜浠喅瀹氬潥鎸佷娇鐢?Groovy 鏉ユ祴璇曘€?/span>
2018骞存槬澶╋紝浣跨敤 Kotlin 寮€鍙戝嚑涓湀涔嬪悗锛屾垜浠€荤粨浜?Kotlin 鐨勪紭缂虹偣锛屾渶缁堢粨璁鸿〃鏄?Kotlin 闄嶄綆浜嗘垜浠殑鐢熶骇鍔涖€?/span>
浜庢槸鎴戜滑浣跨敤 Java 鏉ラ噸鍐欒繖涓井鏈嶅姟椤圭洰銆?/span>
閭d箞 Kotlin 涓昏瀛樺湪鍝簺寮婄锛熶笅闈㈡潵涓€涓€瑙i噴銆?/span>
鍚嶇О閬斀
杩欐槸 Kotlin 鏈€璁╂垜闇囨儕鐨勫湴鏂广€傜湅鐪嬩笅闈㈣繖涓柟娉曪細
褰撲綘璋冪敤 inc(1) 浼氳緭鍑轰粈涔堝憿锛熷湪 Kotlin 涓? 鏂规硶鐨勫弬鏁版棤娉曚慨鏀癸紝鍥犳鍦ㄦ湰渚嬩腑浣犱笉鑳芥敼鍙?num銆傝繖涓璁″緢濂斤紝鍥犱负浣犱笉搴旇鏀瑰彉鏂规硶鐨勮緭鍏ュ弬鏁般€備絾鏄綘鍙互鐢ㄧ浉鍚岀殑鍚嶇О瀹氫箟鍙︿竴涓彉閲忓苟瀵瑰叾杩涜鍒濆鍖栥€?/span>
杩欐牱涓€鏉ワ紝杩欎釜鏂规硶浣滅敤鍩熶腑灏辨湁涓や釜鍚嶄负 num 鐨勫彉閲忋€傚綋鐒讹紝浣犱竴娆″彧鑳借闂叾涓竴涓?num锛屼絾鏄?num 鍊间細琚敼鍙樸€?/span>
鍦?if 璇彞涓啀娣诲姞鍙︿竴涓?num锛屽洜涓轰綔鐢ㄥ煙鐨勫師鍥?num 骞朵笉浼氳淇敼銆?/span>
浜庢槸锛屽湪 Kotlin 涓紝inc(1) 浼氳緭鍑?2銆傚悓鏍锋晥鏋滅殑 Java 浠g爜濡備笅鎵€绀猴紝涓嶈繃鏃犳硶閫氳繃缂栬瘧锛?nbsp;
鍚嶅瓧閬斀骞朵笉鏄?Kotlin 鍙戞槑鐨勶紝杩欏湪缂栫▼璇█涓緢甯歌銆傚湪 Java 涓垜浠範鎯敤鏂规硶鍙傛暟鏉ユ槧灏勭被瀛楁锛?/span>
鍦?Kotlin 涓悕绉伴伄钄芥湁浜涗弗閲嶏紝杩欐槸 Kotlin 鍥㈤槦鐨勪竴涓璁$己闄枫€?/span>
IDEA 鍥㈤槦璇曞浘閫氳繃鍚戞瘡涓伄钄藉彉閲忔樉绀鸿鍛婁俊鎭潵瑙e喅杩欎釜闂銆備袱涓洟闃熷湪鍚屼竴瀹跺叕鍙稿伐浣滐紝鎴栬浠栦滑鍙互浜掔浉浜ゆ祦骞跺氨閬斀闂杈炬垚鍏辫瘑銆傛垜浠庝釜浜鸿搴﹁禐鎴?IDEA 鐨勫仛娉曞洜涓烘垜鎯充笉鍒版湁鍝簺搴旂敤鍦烘櫙闇€瑕侀伄钄芥柟娉曞弬鏁般€?/span>
绫诲瀷鎺ㄦ柇
鍦↘otlin涓紝褰撲綘澹版槑涓€涓獀ar鎴栨槸val锛屼綘閫氬父浼氳缂栬瘧鍣ㄤ粠鍙宠竟鐨勮〃杈惧紡绫诲瀷涓寽娴嬪彉閲忕被鍨嬨€傛垜浠О涔嬩负灞€閮ㄥ彉閲忕被鍨嬫帹鏂紝杩欏绋嬪簭鍛樻潵璇存槸涓€涓緢澶х殑鏀硅繘銆傚畠鍏佽鎴戜滑鍦ㄤ笉褰卞搷闈欐€佺被鍨嬫鏌ョ殑鎯呭喌涓嬬畝鍖栦唬鐮併€?/span>
渚嬪锛岃繖涓狵otlin浠g爜锛?/span>
Kotlin 缂栬瘧鍣ㄤ細灏嗗叾缈昏瘧鎴愶細
Java 鍚屾牱鍏峰杩欎釜鐗规€э紝Java 10涓殑绫诲瀷鎺ㄦ柇绀轰緥濡備笅锛?nbsp;
瀹炶瘽瀹炶锛孠otlin 鍦ㄨ繖涓€鐐逛笂纭疄鏇磋儨涓€绛广€傚綋鐒讹紝绫诲瀷鎺ㄦ柇杩樺彲搴旂敤鍦ㄥ涓満鏅€?/span>
鍏充簬 Java 10涓殑灞€閮ㄥ彉閲忕被鍨嬫帹鏂紝鐐瑰嚮浠ヤ笅閾炬帴
浜嗚В鏇村锛?/span>
Null 瀹夊叏绫诲瀷
Null 瀹夊叏绫诲瀷鏄?Kotlin 鐨勬潃鎵嬬骇鍔熻兘銆?/span>
杩欎釜鎯虫硶寰堝ソ锛屽湪 Kotlin 涓紝绫诲瀷榛樿涓嶅彲涓虹┖銆傚鏋滀綘闇€瑕佹坊鍔犱竴涓彲涓虹┖鐨勭被鍨嬶紝鍙互鍍忎笅鍒椾唬鐮佽繖鏍凤細
鍋囪浣犱娇鐢ㄤ簡鍙负绌虹殑鍙橀噺浣嗘槸骞舵湭杩涜绌哄€兼鏌ワ紝杩欏湪 Kotlin 灏嗘棤娉曢€氳繃缂栬瘧锛屾瘮濡傦細
閭d箞鏄笉鏄鏋滀綘鍚屾椂鎷ユ湁涓嶅彲涓虹┖鍜屽彲涓虹┖鐨勫彉閲忥紝灏卞彲浠ラ伩鍏?Java 涓渶甯歌鐨?NullPointerException 寮傚父鍚楋紵浜嬪疄骞舵病鏈夋兂璞$殑绠€鍗曘€?/span>
褰?Kotlin 浠g爜蹇呴』璋冪敤 Java 浠g爜鏃讹紝浜嬫儏浼氬彉寰楀緢绯熺硶锛屾瘮濡傚簱鏄敤 Java 缂栧啓鐨勶紝鎴戠浉淇¤繖绉嶆儏鍐靛緢甯歌銆備簬鏄涓夌绫诲瀷浜х敓浜嗭紝瀹冭绉颁负骞冲彴绫诲瀷銆侹otlin 鏃犳硶琛ㄧず杩欑濂囨€殑绫诲瀷锛屽畠鍙兘浠?Java 绫诲瀷鎺ㄦ柇鍑烘潵銆傚畠鍙兘浼氳瀵间綘锛屽洜涓哄畠瀵圭┖鍊煎緢瀹芥澗锛屽苟涓斾細绂佺敤 Kotlin 鐨?NULL 瀹夊叏鏈哄埗銆?/span>
鐪嬬湅涓嬮潰杩欎釜 Java 鏂规硶锛?/span>
鍋囧浣犳兂璋冪敤 format(String)銆傚簲璇ヤ娇鐢ㄥ摢绉嶇被鍨嬫潵鑾峰緱杩欎釜 Java 鏂规硶鐨勭粨鏋滃憿锛熶綘鏈変笁涓€夋嫨銆?/span>
绗竴绉嶆柟娉曪細浣犲彲浠ヤ娇鐢?String锛屼唬鐮佺湅璧锋潵寰堝畨鍏紝浣嗘槸浼氭姏鍑?NullPointerException 寮傚父
銆?/span>
閭d綘灏遍渶瑕佺敤 Elvis 鏉ヨВ鍐宠繖涓棶棰橈細
绗簩绉嶆柟娉曪細浣犲彲浠ヤ娇鐢?String锛岃兘澶熶繚璇?Null 瀹夊叏鎬с€?/span>
绗笁绉嶆柟娉曪細璁?Kotlin 鍋氬眬閮ㄥ彉閲忕被鍨嬫帹鏂浣曪紵
棣婁富鎰忥紒杩欎釜 Kotlin 浠g爜鐪嬭捣鏉ュ緢瀹夊叏銆佸彲缂栬瘧锛屼絾鏄畠瀹瑰繊浜嗙┖鍊硷紝灏卞儚鍦?Java 涓竴鏍枫€?/span>
闄ゆ涔嬪锛岃繕鏈夊彟澶栦竴涓柟娉曪紝灏辨槸寮哄埗灏?f 绫诲瀷鎺ㄦ柇涓?String锛?/span>
鍦ㄦ垜鐪嬫潵锛孠otlin 鐨勬墍鏈夎繖浜涚被浼?scala 鐨勭被鍨嬬郴缁熻繃浜庡鏉傘€侸ava 浜掓搷浣滄€т技涔庢崯瀹充簡 Kotlin 绫诲瀷鎺ㄦ柇杩欎釜閲嶉噺绾у姛鑳姐€?/span>
绫诲悕绉板瓧闈㈠父閲?/span>
浣跨敤绫讳技 Log4j 鎴栬€?Gson 鐨?Java 搴撴椂锛岀被鏂囧瓧寰堝父瑙併€?/span>
Java 浣跨敤 .class 鍚庣紑缂栧啓绫诲悕锛?nbsp;
Groovy 鎶婄被杩涜浜嗚繘涓€姝ョ殑绠€鍖栥€備綘鍙互蹇界暐 .class锛屽畠鏄?Groovy 鎴栬€?Java 绫诲苟涓嶉噸瑕併€?/span>
Kotlin 鎶?Kotlin 绫诲拰 Java 绫昏繘琛屼簡鍖哄垎锛屽苟涓哄叾鎻愪緵浜嗚娉曡鑼冿細
鍥犳鍦?Kotlin 涓紝浣犲繀椤诲啓鎴愬涓嬪舰寮忥細
杩欑湅璧锋潵闈炲父涓戦檵銆?/span>
鍙嶅悜绫诲瀷澹版槑
C 绯诲垪鐨勭紪绋嬭瑷€鏈夋爣鍑嗙殑澹版槑绫诲瀷鐨勬柟娉曘€傜畝鑰岃█涔嬶紝棣栧厛鎸囧畾涓€涓被鍨嬶紝鐒跺悗鏄绗﹀悎绫诲瀷鐨勪笢瑗匡紝姣斿鍙橀噺銆佸瓧娈点€佹柟娉曠瓑绛夈€?/span>
Java 涓殑琛ㄧず鏂规硶鏄細
Kotlin 涓垯鏄細
杩欑鏂规硶鏈夊嚑涓師鍥犱护浜鸿鍘屻€?/span>
棣栧厛锛屼綘闇€瑕佸湪鍚嶇О鍜岀被鍨嬩箣闂村姞鍏ヨ繖涓浣欑殑鍐掑彿銆傝繖涓澶栬鑹茬殑鐩殑鏄粈涔堬紵涓轰粈涔堝悕绉颁笌鍏剁被鍨嬭鍒嗙锛熸垜涓嶇煡閬撱€傚彲鎮茬殑鏄紝杩欒浣犲湪 Kotlin 鐨勫伐浣滃彉寰楁洿鍔犲洶闅俱€?/span>
绗簩涓棶棰橈紝褰撲綘璇诲彇涓€涓柟娉曞0鏄庢椂锛屼綘棣栧厛鐪嬪埌鐨勬槸鍚嶅瓧鍜岃繑鍥炵被鍨嬶紝鐒跺悗鎵嶆槸鍙傛暟銆?/span>
鍦?Kotlin 涓紝鏂规硶鐨勮繑鍥炵被鍨嬪彲鑳借繙鍦ㄨ灏撅紝鎵€浠ラ渶瑕佹祻瑙堝緢澶氫唬鐮佹墠鑳界湅鍒帮細
鎴栬€咃紝濡傛灉鍙傛暟鏄€愯鏍煎紡鐨勶紝鍒欓渶瑕佹悳绱€傞偅涔堟垜浠渶瑕佸灏戞椂闂存墠鑳芥壘鍒版鏂规硶鐨勮繑鍥炵被鍨嬪憿锛?/span>
绗笁涓棶棰樻槸 IDE 涓殑鑷姩鍖栨敮鎸佷笉澶熷ソ銆傛爣鍑嗗仛娉曚粠绫诲瀷鍚嶇О寮€濮嬶紝骞朵笖寰堝鏄撴壘鍒扮被鍨嬨€備竴鏃﹂€夋嫨涓€涓被鍨嬶紝IDE 浼氭彁渚涗竴浜涘叧浜庡彉閲忓悕鐨勫缓璁紝杩欎簺鍙橀噺鍚嶆槸浠庨€夊畾鐨勭被鍨嬫淳鐢熺殑锛屽洜姝や綘鍙互蹇€熻緭鍏ヨ繖鏍风殑鍙橀噺锛?nbsp;
Kotlin 灏界鏈?IntelliJ 杩欐牱寮哄ぇ鐨?IDE锛岃緭鍏ュ彉閲忎粛鐒舵槸寰堥毦鐨勩€傚鏋滀綘鏈夊涓瓨鍌ㄥ簱锛屽湪鍒楄〃涓緢闅惧疄鐜版纭殑鑷姩琛ュ叏锛岃繖鎰忓懗鐫€浣犱笉寰椾笉鎵嬪姩杈撳叆瀹屾暣鐨勫彉閲忓悕绉般€?/span>
浼寸敓瀵硅薄
涓€浣?Java 绋嬪簭鍛樻潵鍒?Kotlin 闈㈠墠銆?/span>
鎴戝緢娆h祻瀵硅薄澹版槑鐨勬蹇碘€斺€斿崟渚嬪緢鏈夌敤銆備絾浠庤瑷€涓垹闄ら潤鎬佹垚鍛樻槸涓嶅垏瀹為檯鐨勩€傚湪 Java 涓垜浠娇鐢ㄩ潤鎬?Logger 寰堢粡鍏革紝瀹冨彧鏄竴涓?Logger锛屾墍浠ユ垜浠笉鍏冲績闈㈠悜瀵硅薄鐨勭函搴︺€傚畠鑳藉宸ヤ綔锛屼粠鏉ユ病鏈変换浣曞潖澶勩€?/span>
鍥犱负鏈夋椂鍊欎綘蹇呴』浣跨敤闈欐€併€傛棫鐗堟湰 public static void main() 浠嶇劧鏄惎鍔?Java 搴旂敤绋嬪簭鐨勫敮涓€鏂瑰紡銆?/span>
闆嗗悎瀛楅潰閲?/span>
鍦↗ava涓紝鍒濆鍖栧垪琛ㄩ潪甯哥箒鐞愶細
鍒濆鍖栧湴鍥鹃潪甯稿啑闀匡紝寰堝浜轰娇鐢?Guava锛?/span>
鍦?Java 涓紝鎴戜滑浠嶇劧鍦ㄧ瓑寰呮柊鐨勮娉曟潵琛ㄨ揪闆嗗悎鍜屾槧灏勩€傝娉曞湪璁稿璇█涓潪甯歌嚜鐒跺拰鏂逛究銆?/span>
JavaScript锛?/span>
Python锛?/span>
Groovy锛?/span>
绠€鍗曟潵璇达紝闆嗗悎瀛楅潰閲忕殑鏁撮綈璇硶灏辨槸浣犲鐜颁唬缂栫▼璇█鐨勬湡鏈涳紝鐗瑰埆鏄鏋滃畠鏄粠澶村紑濮嬪垱寤虹殑銆侹otlin 鎻愪緵浜嗕竴绯诲垪鍐呯疆鍑芥暟锛屾瘮濡?listOf()銆乵utableListOf()銆乵apOf()銆乭ashMapOf() 绛夌瓑銆?/span>
Kotlin锛?nbsp;
鍦ㄥ湴鍥句腑锛岄敭鍜屽€间笌 to 杩愮畻绗﹂厤瀵癸紝杩欏緢濂姐€備絾涓轰粈涔堜竴鐩存病鏈夊緱鍒板箍娉涗娇鐢ㄥ憿锛熶护浜哄け鏈涖€?/span>
Maybe
鍑芥暟寮忚瑷€锛堟瘮濡?Haskell锛夋病鏈夌┖鍊笺€傜浉鍙嶏紝浠栦滑鎻愪緵 Maybe monad锛堝鏋滀綘涓嶇啛鎮塵onad锛岃闃呰 Tomasz Nurkiewicz 鐨勮繖绡囨枃绔狅細
http://www.nurkiewicz.com/2016/06/functor-and-monad-examples-in-plain-java.html
锛夈€?/span>
Maybe 寰堜箙浠ュ墠灏辫 Scala 浠?Option 寮曞叆鍒?JVM 涓栫晫锛岀劧鍚庡湪 Java 8 涓閲囩敤涓?Optional銆傚浠婏紝Optional 鏄湪 API 杈圭晫澶勭悊杩斿洖绫诲瀷涓殑绌哄€肩殑闈炲父娴佽鐨勬柟寮忋€?/span>
Kotlin 涓病鏈?Optional 鐨勭瓑浠风墿锛屾墍浠ヤ綘澶ф搴旇浣跨敤 Kotlin 鐨勫彲绌虹被鍨嬨€傝鎴戜滑鏉ヨ皟鏌ヤ竴涓嬭繖涓棶棰樸€?/span>
閫氬父鎯呭喌涓嬶紝褰撲綘鏈変竴涓?Optional 鐨勬椂鍊欙紝浣犳兂瑕佸簲鐢ㄤ竴绯诲垪鏃犳晥鐨勮浆鎹€?/span>
渚嬪锛屽湪 Java 涓細
鍦?Kotlin 涓紝涓轰簡鏄犲皠浣犲彲浠ヤ娇鐢?let 鍑芥暟锛?/span>
涓婇潰鐨勪唬鐮佹槸閿欒鐨勶紝parseInt() 浼氭姏鍑?NPE 銆俶ap() 浠呭湪鏈夊€兼椂鎵ц銆傚惁鍒欙紝Null 灏变細璺宠繃锛岃繖灏辨槸涓轰粈涔?map() 濡傛鏂逛究銆備笉骞哥殑鏄紝Kotlin 鐨?let 涓嶄細閭f牱宸ヤ綔銆傚畠浠庡乏渚х殑鎵€鏈夊唴瀹逛腑璋冪敤锛屽寘鎷┖鍊笺€?/span>
涓轰簡淇濊瘉杩欎釜浠g爜 Null 瀹夊叏锛屼綘蹇呴』鍦ㄦ瘡涓唬鐮佷箣鍓嶆坊鍔?let锛?nbsp;
鐜板湪锛屾瘮杈?Java 鍜?Kotlin 鐗堟湰鐨勫彲璇绘€с€備綘鏇村€惧悜鍝釜锛?/span>
鏁版嵁绫?/span>
鏁版嵁绫绘槸 Kotlin 鍦ㄥ疄鐜?Value Objects 鏃朵娇鐢ㄧ殑鏂规硶锛屼互鍑忓皯 Java 涓笉鍙伩鍏嶇殑鏍锋澘闂銆?/span>
渚嬪锛屽湪 Kotlin 涓紝浣犲彧鍐欎竴涓?Value Object 锛?/span>
Kotlin 瀵?equals()銆乭ashCode()銆乼oString() 浠ュ強 copy() 鏈夊緢濂界殑瀹炵幇銆傚湪瀹炵幇绠€鍗曠殑DTO 鏃跺畠闈炲父鏈夌敤銆備絾璇疯浣忥紝鏁版嵁绫诲甫鏈変弗閲嶇殑灞€闄愭€с€備綘鏃犳硶鎵╁睍鏁版嵁绫绘垨鑰呭皢鍏舵娊璞″寲锛屾墍浠ヤ綘鍙兘涓嶄細鍦ㄦ牳蹇冩ā鍨嬩腑浣跨敤瀹冧滑銆?/span>
杩欎釜闄愬埗涓嶆槸 Kotlin 鐨勯敊銆傚湪 equals() 娌℃湁杩濆弽 Liskov 鍘熷垯鐨勬儏鍐典笅锛屾病鏈夊姙娉曚骇鐢熸纭殑鍩轰簬浠峰€肩殑鏁版嵁銆?/span>
杩欎篃鏄负浠€涔?Kotlin 涓嶅厑璁告暟鎹被缁ф壙鐨勫師鍥犮€?/span>
寮€鏀剧被
Kotlin 绫婚粯璁や负 final銆傚鏋滀綘鎯虫墿灞曚竴涓被锛屽繀椤绘坊鍔?open 淇グ绗︺€?/span>
缁ф壙璇硶濡備笅鎵€绀猴細
Kotlin 灏?extends 鍏抽敭瀛楁洿鏀逛负: 杩愮畻绗︼紝璇ヨ繍绠楃鐢ㄤ簬灏嗗彉閲忓悕绉颁笌鍏剁被鍨嬪垎寮€銆傞偅涔堝啀鍥炲埌 C ++璇硶锛熷鎴戞潵璇磋繖寰堟贩涔便€?/span>
杩欓噷鏈変簤璁殑鏄紝榛樿鎯呭喌涓嬬被鏄?final銆備篃璁?Java 绋嬪簭鍛樿繃搴︿娇鐢ㄧ户鎵匡紝涔熻搴旇鍦ㄨ€冭檻鎵╁睍绫讳箣鍓嶈€冭檻涓夋銆備絾鎴戜滑鐢熸椿鍦ㄦ鏋朵笘鐣岋紝Spring 浣跨敤 cglib銆乯assist 搴撲负浣犵殑 bean 鐢熸垚鍔ㄦ€佷唬鐞嗐€侶ibernate 鎵╁睍浣犵殑瀹炰綋浠ュ惎鐢ㄥ欢杩熷姞杞姐€?/span>
濡傛灉浣犱娇鐢?Spring锛屼綘鏈変袱绉嶉€夋嫨銆備綘鍙互鍦ㄦ墍鏈?bean 绫荤殑鍓嶉潰娣诲姞 open锛屾垨鑰呬娇鐢ㄨ繖涓紪璇戝櫒鎻掍欢锛?nbsp;
闄″抄鐨勫涔犳洸绾?/span>
濡傛灉浣犺涓鸿嚜宸辨湁 Java 鍩虹灏卞彲浠ュ揩閫熷涔?Kotlin锛岄偅浣犲氨閿欎簡銆侹otlin 浼氳浣犻櫡鍏ユ繁娓婏紝浜嬪疄涓婏紝Kotlin 鐨勮娉曟洿鎺ヨ繎 Scala銆傝繖鏄竴椤硅祵娉紝浣犲皢涓嶅緱涓嶅繕璁?Java 骞跺垏鎹㈠埌瀹屽叏涓嶅悓鐨勮瑷€銆?/span>
鐩稿弽锛屽涔?Groovy 鏄竴涓剦蹇殑杩囩▼銆侸ava 浠g爜鏄纭殑 Groovy 浠g爜锛屽洜姝や綘鍙互閫氳繃灏嗘枃浠舵墿灞曞悕浠?.java 鏇存敼涓?.groovy銆?/span>
鏈€鍚庣殑鎯虫硶
瀛︿範鏂版妧鏈氨鍍忎竴椤规姇璧勩€傛垜浠姇鍏ユ椂闂达紝鏂版妧鏈鎴戜滑寰楀埌鍥炴姤銆備絾鎴戝苟涓嶆槸璇?Kotlin 鏄竴绉嶇碂绯曠殑璇█锛屽彧鏄?/span>
鍦ㄦ垜浠殑妗堜緥涓紝鎴愭湰杩滆秴鏀剁泭銆?/span>
鏈枃涓€鍑哄氨寮曞彂浜嗕笟鍐呯殑骞挎硾浜夎锛?span class="mq-680">Kotlin 璇█鎷ユ姢鑰?nbsp;M谩rton Braun 灏辫〃绀轰簡寮虹儓鐨勫弽瀵广€?/span>
M谩rton Braun 鍗佸垎
鍠滄 Kotlin 缂栫▼锛岀洰鍓嶄粬鍦?StackOverflow 涓?Kotlin 鏍囩鐨勬渶楂樼敤鎴峰垪琛ㄤ腑鎺掑悕绗笁锛屽苟涓旀槸涓や釜寮€婧?Kotlin 搴撶殑鍒涘缓鑰咃紝鏈€钁楀悕鐨勬槸 MaterialDrawerKt銆傛澶栦粬杩樻槸 Autosoft 鐨?Android 寮€鍙戜汉鍛橈紝鐩墠姝e湪甯冭揪浣╂柉鎶€鏈粡娴庡ぇ瀛︽敾璇昏绠楁満宸ョ▼纭曞+瀛︿綅銆?/span>
浠ヤ笅灏辨槸浠栭拡瀵逛笂鏂囩殑鍙嶉┏锛?/span>
褰撴垜绗竴娆$湅鍒拌繖绡囨枃绔犳椂锛屾垜灏辨兂鎶婂畠杞彂鍑烘潵鐪嬬湅澶у浼氭€庝箞鎯筹紝鎴戣偗瀹氬畠浼氭槸涓€涓湁浜夎鐨勮瘽棰樸€?/span>
鍚庢潵鎴戣浜嗚繖绡囨枃绔狅紝鏋滅劧璇佹槑浜嗗畠鏄竴绉嶄富瑙傜殑銆佷笉鐪熷疄鐨勩€佺敋鑷虫湁浜涘眳楂樹复涓嬬殑鍋忚銆?/span>
鏈変簺浜哄凡缁忓湪鍘熻创涓嬭繘琛屼簡鍚堢悊鐨勬壒璇勶紝瀵规鎴戜篃鎯宠〃杈句竴涓嬭嚜宸辩殑鐪嬫硶銆?/span>
鍚嶇О閬斀
鈥淚DEA 鍥㈤槦鈥濓紙鎴栬€?Kotlin 鎻掍欢鍥㈤槦锛夊拰鈥淜otlin 鍥㈤槦鈥濊偗瀹氭槸鍚屾牱鐨勪汉锛屾垜浠庝笉璁や负鍐呴儴鍐茬獊浼氭槸涓ソ浜嬨€?/span>
璇█鎻愪緵杩欎釜鍔熻兘缁欎綘锛屼綘闇€瑕佺殑璇濆氨浣跨敤锛屽鏋滆鍘岋紝璋冩暣妫€鏌ヨ缃氨鏄簡銆?/span>
绫诲瀷鎺ㄦ柇
Kotlin 鐨勭被鍨嬫帹鏂棤澶勪笉鍦紝浣滆€呰鐨?Java 10 鍚屾牱鍙互绠€鐩存槸鍦ㄥ紑鐜╃瑧銆?/span>
Kotlin 鐨勬柟寮忚秴瓒婁簡鎺ㄦ柇灞€閮ㄥ彉閲忕被鍨嬫垨杩斿洖琛ㄨ揪寮忎綋鐨勫嚱鏁扮被鍨嬨€傝繖閲屼粙缁嶇殑杩欎袱涓緥瀛愭槸閭d簺鍒氬垰鐪嬭繃鍏充簬 Kotlin 鐨勭涓€娆′粙缁嶆€ц璇濈殑浜轰細鎻愬埌鐨勶紝鑰屼笉鏄偅浜涜姳浜嗗崐骞村涔犺璇█鐨勪汉銆?/span>
渚嬪锛屼綘鎬庝箞鑳戒笉鎻?Kotlin 鎺ㄦ柇娉涘瀷绫诲瀷鍙傛暟鐨勬柟寮忥紵杩欎笉鏄?Kotlin 鐨勪竴娆℃€у姛鑳斤紝瀹冩繁娣辫瀺鍏ヤ簡鏁翠釜璇█銆?/span>
缂栬瘧鏃?Null 瀹夊叏
杩欎釜鎵硅瘎鏄鐨勶紝褰撲綘涓?Java 浠g爜杩涜浜掓搷浣滄椂锛孨ull 瀹夊叏鎬х‘瀹炶鐮村潖浜嗐€傝璇█鑳屽悗鐨勫洟闃熸浘澶氭澹版槑锛屼粬浠渶鍒濊瘯鍥句娇 Java 鍙负绌虹殑姣忕绫诲瀷锛屼絾浠栦滑鍙戠幇瀹冨疄闄呬笂璁╀唬鐮佸彉寰楁洿绯熺硶銆?/span>
Kotlin 涓嶆瘮 Java 鏇村樊锛屼綘鍙渶瑕佹敞鎰忎娇鐢ㄧ粰瀹氬簱鐨勬柟寮忥紝灏卞儚鍦?Java 涓娇鐢ㄥ畠涓€鏍凤紝鍥犱负瀹冨苟娌℃湁涓嶅幓鑰冭檻 Null 瀹夊叏銆傚鏋?Java 搴撳叧蹇?Null
瀹夊叏鎬э紝鍒欏畠浠細鏈夎澶氭敮鎸佹敞閲婂彲渚涙坊鍔犮€?/span>
涔熻鍙互娣诲姞涓€涓紪璇戝櫒鏍囧織锛屼娇姣忕 Java 绫诲瀷閮藉彲浠ヤ负绌猴紝浣嗚繖瀵?Kotlin 鍥㈤槦鏉ヨ涓嶅緱涓嶈姳璐瑰ぇ閲忛澶栬祫婧愩€?/span>
绫诲悕绉板瓧闈㈠父閲?/span>
:: class 涓轰綘鎻愪緵浜嗕竴涓?KClass 瀹炰緥锛屼互渚夸笌 Kotlin 鑷繁鐨勫弽灏?API 涓€璧蜂娇鐢紝鑰?: class.java涓轰綘鎻愪緵浜嗙敤浜?Java 鍙嶅皠鐨勫父瑙?Java 绫诲疄渚嬨€?/span>
鍙嶅悜绫诲瀷澹版槑
涓轰簡娓呮璧疯锛岄鍊掔殑椤哄簭鏄瓨鍦ㄧ殑锛岃繖鏍蜂綘灏卞彲浠ヤ互鍚堢悊鐨勬柟寮忕渷鐣ユ樉寮忕被鍨嬨€?/span>
鍐掑彿鍙槸璇硶锛岃繖鍦ㄧ幇浠h瑷€涓槸鐩稿綋鏅亶鐨勪竴绉嶏紝姣斿 Scala銆丼wift 绛夈€?/span>
鎴戜笉鐭ラ亾浣滆€呭湪浣跨敤浠€涔?IntelliJ锛屼絾鎴戜娇鐢ㄧ殑鍙橀噺鍚嶇О鍜岀被鍨嬮兘鑳藉鑷姩琛ュ叏銆傚浜庡弬鏁帮紝IntelliJ 鐢氳嚦浼氱粰浣犳彁渚涚浉鍚岀被鍨嬬殑鍚嶇О鍜岀被鍨嬬殑寤鸿锛岃繖瀹為檯涓婃瘮 Java 鏇村ソ銆?/span>
浼寸敓瀵硅薄
鍘熸枃涓锛?/span>
瀹為檯涓婏紝杩欎笉鏄惎鍔?Java 搴旂敤绋嬪簭鐨勫敮涓€鏂瑰紡銆備綘鍙互杩欐牱鍋氾細
鎴栬€呰繖鏍凤細
闆嗗悎瀛楅潰閲?/span>
浣犲彲浠ュ湪娉ㄩ噴涓娇鐢ㄦ暟缁勬枃瀛椼€備絾鏄紝闄ゆ涔嬪锛岃繖浜涢泦鍚堝伐鍘傜殑鍔熻兘闈炲父绠€娲侊紝鑰屼笖瀹冧滑鏄彟涓€绉嶁€滃唴缃€濆埌璇ヨ瑷€鐨勪笢瑗匡紝鑰屽畠浠疄闄呬笂鍙槸搴撳嚱鏁般€?/span>
浣犲彧鏄姳鎬ㄤ娇鐢?杩涜绫诲瀷澹版槑銆傝€屼笖锛屼负浜嗚幏寰楀畠涓嶅繀鏄崟鐙殑璇█缁撴瀯鐨勫ソ澶勶紝瀹冨彧鏄竴涓换浣曚汉閮藉彲浠ュ疄鐜扮殑鍔熻兘銆?/span>
Maybe
瀵逛簬浠g爜纭疄杩欐湁浜涢毦鐪嬨€備絾鏄綘涓嶅簲璇ュ湪 Kotlin 浠g爜涓娇鐢?parseInt锛岃€屽簲璇ヨ繖鏍峰仛锛堟垜涓嶇煡閬撲綘浣跨敤璇ヨ瑷€鐨?6 涓湀涓负浣曢敊杩囪繖涓級銆備綘涓轰粈涔堣鏄庣‘鍦板懡鍚嶄竴涓?Lambda 鍙傛暟鍛紵
鏁版嵁绫?/span>
鍘熸枃涓锛?/span>
鎴戜笉鐭ラ亾浣犱负浠€涔堟彁鍑鸿繖涓棶棰樸€傚鏋滀綘闇€瑕佹洿澶嶆潅鐨勭被锛屼綘浠嶇劧鍙互鍒涘缓瀹冧滑骞舵墜鍔ㄧ淮鎶ゅ畠浠殑 equals銆乭ashCode 绛夋柟娉曘€傛暟鎹被浠呬粎鏄竴涓畝鍗曠敤渚嬬殑渚挎嵎鏂瑰紡锛屽浜庡緢澶氫汉鏉ヨ杩欏緢甯歌銆?/span>
鍏紑绫?/span>
浣滆€呭啀娆¢剻瑙嗕簡锛屽姝ゆ垜瀹炲湪鏃犺瘽鍙銆?/span>
闄″抄鐨勫涔犳洸绾?/span>
浣滆€呰涓哄涔?Kotlin 寰堥毦锛?浣嗘槸鎴戜釜浜哄苟涓嶈繖涔堣涓恒€?/span>
鏈€鍚庣殑鎯虫硶
浠庝綔鑰呭垪涓剧殑渚嬪瓙涓紝鎴戞劅瑙変粬鍙槸浜嗚В璇█鐨勮〃闈€?/span>
寰堥毦鎯宠薄浠栧姝ゆ湁鎶曞叆寰堝鏃堕棿銆?/span>
鏈€杩戠殑璇█鎺掕锛宩ava渚濈劧鎺掑湪绗竴锛屽湪璇█鎺掕姒滀笂锛孠oltin宸茶鐢╁埌10鍚嶄箣澶栵紒
鍓嶇嚎鎺ㄥ嚭瀛︿範浜ゆ祦缇や竴瀹氳澶囨敞锛?/span>
鐮旂┒/宸ヤ綔鏂瑰悜+鍦扮偣+瀛︽牎/鍏徃+鏄电О锛堝鐩爣妫€娴?涓婃捣+涓婁氦+鍙彲锛?/span>
锛屾牴鎹牸寮忓娉紝鍙洿蹇閫氳繃涓旈個璇疯繘缇?/span>
鍦ㄧ湅|鏄熸爣|鐣欒█, 鐪熺埍
fun inc(num : Int) {
val num = 2
if (num > 0) {
val num = 3
}
println ( "num: " + num)
}
void inc(int num) {
int num = 2; //error: variable 'num' is already defined in the scope
if (num > 0) {
int num = 3; //error: variable 'num' is already defined in the scope
}
System. out.println ( "num: " + num);
}
public class Shadow {
int val;
public Shadow(int val) {
this. val = val;
}
}
var a = "10"
var a : String = "10"
var a = "10";
https://medium.com/@afinlay/java-10-sneak-peek-local-variable-type-inference-var-3022016e1a2b
val a: String? = null // ok
val b: String = null // compilation error
println (a.length) // compilation error
println (a?.length) // fine, prints null
println (a?.length ?: 0) // fine, prints 0
public class Utils {
static String format(String text) {
return text.isEmpty() ? null : text;
}
}
fun doSth(text: String) {
val f: String = Utils.format(text) // compiles but assignment can throw NPE at runtime
println ( "f.len : " + f.length)
}
fun doSth(text: String) {
val f: String = Utils.format(text) ?: "" // safe with Elvis
println ( "f.len : " + f.length)
}
fun doSth(text: String) {
val f: String? = Utils.format(text) // safe
println ( "f.len : " + f.length) // compilation error, fine
println ( "f.len : " + f?.length) // null-safe with ? operator
}
fun doSth(text: String) {
val f = Utils.format(text) // f type inferred as String!
println ( "f.len : " + f.length) // compiles but can throw NPE at runtime
}
fun doSth(text: String) {
val f = Utils.format(text)!! // throws NPE when format() returns null
println ( "f.len : " + f.length)
}
Gson gson = new GsonBuilder().registerTypeAdapter(LocalDate.class, new LocalDateAdapter()).create();
def gson = new GsonBuilder().registerTypeAdapter(LocalDate, new LocalDateAdapter()).create()
val kotlinClass : KClass<LocalDate> = LocalDate:: class
val javaClass : Class<LocalDate> = LocalDate:: class.java
val gson = GsonBuilder().registerTypeAdapter(LocalDate:: class.java, LocalDateAdapter()).create()
int inc(int i) {
return i + 1;
}
fun inc(i: Int): Int {
return i + 1
}
private fun getMetricValue(kafkaTemplate : KafkaTemplate<String, ByteArray>, metricName : String) : Double {
...
}
@Bean
fun kafkaTemplate(
@Value("${interactions.kafka.bootstrap-servers-dc1}") bootstrapServersDc1: String,
@Value("${interactions.kafka.bootstrap-servers-dc2}") bootstrapServersDc2: String,
cloudMetadata: CloudMetadata,
@Value("${interactions.kafka.batch-size}") batchSize: Int,
@Value("${interactions.kafka.linger-ms}") lingerMs: Int,
metricRegistry : MetricRegistry
): KafkaTemplate<String, ByteArray> {
val bootstrapServer = if (cloudMetadata.datacenter == "dc1") {
bootstrapServersDc1
}
...
}
MongoExperimentsRepository repository
repository : MongoExperimentsRepository
鈥滃棬锛孠otlin銆傛垜鏄柊鏉ョ殑锛屾垜鍙互浣跨敤闈欐€佹垚鍛樺悧锛?浠栭棶銆?/span> 鈥滀笉琛屻€傛垜鏄潰鍚戝璞$殑锛岄潤鎬佹垚鍛樹笉鏄潰鍚戝璞$殑銆傗€?Kotlin 鍥炵瓟銆?/span> 鈥滃ソ鍚э紝浣嗘垜闇€瑕?MyClass 鐨?logger锛屾垜璇ユ€庝箞鍔烇紵鈥?nbsp; 鈥滆繖涓病闂锛屼娇鐢ㄤ即鐢熷璞″嵆鍙€傗€?/span> 鈥滈偅鏄粈涔堜笢瑗匡紵鈥?鈥滆繖鏄眬闄愬埌浣犵殑绫荤殑鍗曠嫭瀵硅薄銆傛妸浣犵殑 logger 鏀惧湪浼寸敓瀵硅薄涓€傗€滽otlin瑙i噴璇淬€?/span> 鈥滄垜鎳備簡銆傝繖鏍峰鍚楋紵鈥?/span>
class MyClass {
companion object {
val logger = LoggerFactory.getLogger(MyClass:: class.java)
}
}
鈥滄纭紒鈥?/span> 鈥滃緢璇︾粏鐨勮娉曪紝鈥濈▼搴忓憳鐪嬭捣鏉ュ緢鐤戞儜锛屸€滀絾鏄病鍏崇郴锛岀幇鍦ㄦ垜鍙互鍍?MyClass.logger 杩欐牱璋冪敤鎴戠殑 logger锛屽氨鍍?Java 涓殑涓€涓潤鎬佹垚鍛橈紵鈥?nbsp; 鈥滃棷......鏄殑锛屼絾瀹冧笉鏄潤鎬佹垚鍛橈紒杩欓噷鍙湁瀵硅薄銆傛妸瀹冪湅浣滄槸宸茬粡瀹炰緥鍖栦负鍗曚緥鐨勫尶鍚嶅唴閮ㄧ被銆備簨瀹炰笂锛岃繖涓被骞朵笉鏄尶鍚嶇殑锛屽畠鐨勫悕瀛楁槸 Companion锛屼絾浣犲彲浠ョ渷鐣ヨ繖涓悕瀛椼€傜湅鍒颁簡鍚楋紵杩欏緢绠€鍗曘€?
class AppRunner {
companion object {
@JvmStatic fun main(args: Array<String>) {
SpringApplication.run(AppRunner:: class.java, *args)
}
}
}
import java.util.Arrays;
...
List< String> strings = Arrays.asList( "Saab", "Volvo");
import com.google.common.collect.ImmutableMap;
...
Map< String, String> string = ImmutableMap.of( "firstName", "John", "lastName", "Doe");
const list = [ 'Saab', 'Volvo']
const map = { 'firstName': 'John', 'lastName' : 'Doe'}
list = [ 'Saab', 'Volvo']
map = { 'firstName': 'John', 'lastName': 'Doe'}
def list = [ 'Saab', 'Volvo']
def map = [ 'firstName': 'John', 'lastName': 'Doe']
val list = listOf( "Saab", "Volvo")
val map = mapOf( "firstName" to "John", "lastName" to "Doe")
public int parseAndInc(String number) {
return Optional.ofNullable(number)
. map(Integer::parseInt)
. map(it -> it + 1)
.orElse( 0);
}
fun parseAndInc(number: String?): Int {
return number.let { Integer.parseInt(it) }
.let { it -> it + 1 } ?: 0
}
fun parseAndInc(number: String?): Int {
return number?.let { Integer.parseInt(it) }
?.let { it -> it + 1 } ?: 0
}
data class User( val name: String, val age: Int)
open class Base
class Derived : Base()
buildscript {
dependencies {
classpath group: 'org.jetbrains.kotlin', name: 'kotlin-allopen', version: "$versions.kotlin"
}
}
浠ヤ笂鍐呭缂栬瘧鑷?From Java to Kotlin and Back Again锛?/span>浣滆€?Kotlin ketckup銆?/span> 浠栨槸涓€鍚嶅叿鏈?5骞翠互涓婁笓涓氱粡楠岀殑杞欢宸ョ▼甯堬紝涓撴敞浜嶫VM 銆傚湪 Allegro锛屼粬鏄竴鍚嶅紑鍙戝洟闃熻礋璐d汉锛孞aVers 椤圭洰璐熻矗浜猴紝Spock 鍊″鑰呫€傛澶栵紝浠栬繕鏄?allegro.tech/blog 鐨勪富缂栥€?/span>
鏈夋椂鍊欎綘蹇呴』浣跨敤闈欐€併€傛棫鐗堟湰 public static void main() 浠嶇劧鏄惎鍔?Java 搴旂敤绋嬪簭鐨勫敮涓€鏂瑰紡銆?/span>
class AppRunner {
companion object {
@JvmStatic fun main(args: Array<String>) {
SpringApplication.run(AppRunner:: class.java, *args)
}
}
}
fun main(args锛欰rray <String>){ SpringApplication.run(AppRunner :: class.java锛? args)}
fun main(args锛欰rray <String>){ runApplication <AppRunner>(* args)}
濡傛灉浣犲枩娆?Optional 锛屼綘鍙互浣跨敤瀹冦€侹otlin 鍦?JVM 涓婅繍琛屻€?/span>
杩欎釜闄愬埗涓嶆槸 Kotlin 鐨勯敊銆傚湪 equals() 娌℃湁杩濆弽 Liskov 鍘熷垯鐨勬儏鍐典笅锛屾病鏈夊姙娉曚骇鐢熸纭殑鍩轰簬浠峰€肩殑鏁版嵁銆?/span> 杩欏氨鏄负浠€涔?Kotlin 涓嶅厑璁告暟鎹被缁ф壙鐨勫師鍥犮€?/span>
鎵爜鍔犳垜寰俊杩涚兢锛屽唴鎺ㄥ拰鎶€鏈氦娴侊紝澶т浆浠浂璺濈
以上是关于鍐嶈锛孠otlin 锛佷綘濂? Java 锛?/h1>