-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.html
5580 lines (3854 loc) · 441 KB
/
result.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="no-js" lang="en">
<html>
<head>
<meta charset="utf-8">
<title>Kotlin</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
</head>
<body>
<article role="main" class="page-content g-9">
<a href="https://github.com/JetBrains/kotlin-web-site/edit/master/docs/reference/basic-syntax.md"
class="page-link-to-github"
target="_blank"
title="Edit this page on GitHub">
<i class="github-icon"></i>
<span class="text">Edit Page</span>
</a>
<h1 id="basic-syntax">Basic Syntax<a href="#basic-syntax" class="anchor"></a></h1>
<h2 id="defining-packages">Defining packages<a href="#defining-packages" class="anchor"></a></h2>
<p>Package specification should be at the top of the source file:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">package</span> <span class="nn">my.demo</span>
<span class="k">import</span> <span class="nn">java.util.*</span>
<span class="c1">// ...</span></code></pre>
<p>It is not required to match directories and packages: source files can be placed arbitrarily in the file system.</p>
<p>See <a href="packages.html">Packages</a>.</p>
<h2 id="defining-functions">Defining functions<a href="#defining-functions" class="anchor"></a></h2>
<p>Function having two <code>Int</code> parameters with <code>Int</code> return type:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">sum</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">Int</span> <span class="p">{</span>
<span class="k">return</span> <span class="n">a</span> <span class="p">+</span> <span class="n">b</span>
<span class="p">}</span></code></pre>
<p>Function with an expression body and inferred return type:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">sum</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">=</span> <span class="n">a</span> <span class="p">+</span> <span class="n">b</span></code></pre>
<p>Function visible from outside of a module should have return type explicitly specified:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">public</span> <span class="k">fun</span> <span class="nf">sum</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">Int</span> <span class="p">=</span> <span class="n">a</span> <span class="p">+</span> <span class="n">b</span></code></pre>
<p>Function returning no meaningful value:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">printSum</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">Unit</span> <span class="p">{</span>
<span class="n">print</span><span class="p">(</span><span class="n">a</span> <span class="p">+</span> <span class="n">b</span><span class="p">)</span>
<span class="p">}</span></code></pre>
<p><code>Unit</code> return type can be omitted:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">public</span> <span class="k">fun</span> <span class="nf">printSum</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">{</span>
<span class="n">print</span><span class="p">(</span><span class="n">a</span> <span class="p">+</span> <span class="n">b</span><span class="p">)</span>
<span class="p">}</span></code></pre>
<p>See <a href="functions.html">Functions</a>.</p>
<h2 id="defining-local-variables">Defining local variables<a href="#defining-local-variables" class="anchor"></a></h2>
<p>Assign-once (read-only) local variable:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">a</span><span class="p">:</span> <span class="n">Int</span> <span class="p">=</span> <span class="m">1</span>
<span class="k">val</span> <span class="py">b</span> <span class="p">=</span> <span class="m">1</span> <span class="c1">// `Int` type is inferred</span>
<span class="k">val</span> <span class="py">c</span><span class="p">:</span> <span class="n">Int</span> <span class="c1">// Type required when no initializer is provided</span>
<span class="n">c</span> <span class="p">=</span> <span class="m">1</span> <span class="c1">// definite assignment</span></code></pre>
<p>Mutable variable:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">var</span> <span class="py">x</span> <span class="p">=</span> <span class="m">5</span> <span class="c1">// `Int` type is inferred</span>
<span class="n">x</span> <span class="p">+=</span> <span class="m">1</span></code></pre>
<p>See also <a href="properties.html">Properties And Fields</a>.</p>
<h2 id="using-string-templates">Using string templates<a href="#using-string-templates" class="anchor"></a></h2>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">main</span><span class="p">(</span><span class="n">args</span><span class="p">:</span> <span class="n">Array</span><span class="p"><</span><span class="n">String</span><span class="p">>)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">args</span><span class="p">.</span><span class="n">size</span><span class="p">()</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span> <span class="k">return</span>
<span class="n">print</span><span class="p">(</span><span class="s">"First argument: ${args[0]}"</span><span class="p">)</span>
<span class="p">}</span></code></pre>
<p>See <a href="basic-types.html#string-templates">String templates</a>.</p>
<h2 id="using-conditional-expressions">Using conditional expressions<a href="#using-conditional-expressions" class="anchor"></a></h2>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">max</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">Int</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="p">></span> <span class="n">b</span><span class="p">)</span>
<span class="k">return</span> <span class="n">a</span>
<span class="k">else</span>
<span class="k">return</span> <span class="n">b</span>
<span class="p">}</span></code></pre>
<p>Using <em class="keyword">if</em> as an expression:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">max</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">=</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="p">></span> <span class="n">b</span><span class="p">)</span> <span class="n">a</span> <span class="k">else</span> <span class="n">b</span></code></pre>
<p>See <a href="control-flow.html#if-expression"><em class="keyword">if</em>-expressions</a>.</p>
<h2 id="using-nullable-values-and-checking-for-null-keyword-">Using nullable values and checking for <em class="keyword">null</em><a href="#using-nullable-values-and-checking-for-null-keyword-" class="anchor"></a></h2>
<p>A reference must be explicitly marked as nullable when <em class="keyword">null</em> value is possible.</p>
<p>Return <em class="keyword">null</em> if <code>str</code> does not hold an integer:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">parseInt</span><span class="p">(</span><span class="n">str</span><span class="p">:</span> <span class="n">String</span><span class="p">):</span> <span class="n">Int</span><span class="p">?</span> <span class="p">{</span>
<span class="c1">// ...</span>
<span class="p">}</span></code></pre>
<p>Use a function returning nullable value:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">main</span><span class="p">(</span><span class="n">args</span><span class="p">:</span> <span class="n">Array</span><span class="p"><</span><span class="n">String</span><span class="p">>)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">args</span><span class="p">.</span><span class="n">size</span><span class="p">()</span> <span class="p"><</span> <span class="m">2</span><span class="p">)</span> <span class="p">{</span>
<span class="n">print</span><span class="p">(</span><span class="s">"Two integers expected"</span><span class="p">)</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="k">val</span> <span class="py">x</span> <span class="p">=</span> <span class="n">parseInt</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="m">0</span><span class="p">])</span>
<span class="k">val</span> <span class="py">y</span> <span class="p">=</span> <span class="n">parseInt</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="m">1</span><span class="p">])</span>
<span class="c1">// Using `x * y` yields error because they may hold nulls.</span>
<span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="p">!=</span> <span class="k">null</span> <span class="p">&&</span> <span class="n">y</span> <span class="p">!=</span> <span class="k">null</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// x and y are automatically cast to non-nullable after null check</span>
<span class="n">print</span><span class="p">(</span><span class="n">x</span> <span class="p">*</span> <span class="n">y</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span></code></pre>
<p>or</p>
<pre><code class="code-block _highlighted lang_kotlin"> <span class="c1">// ...</span>
<span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="p">==</span> <span class="k">null</span><span class="p">)</span> <span class="p">{</span>
<span class="n">print</span><span class="p">(</span><span class="s">"Wrong number format in '${args[0]}'"</span><span class="p">)</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="n">y</span> <span class="p">==</span> <span class="k">null</span><span class="p">)</span> <span class="p">{</span>
<span class="n">print</span><span class="p">(</span><span class="s">"Wrong number format in '${args[1]}'"</span><span class="p">)</span>
<span class="k">return</span>
<span class="p">}</span>
<span class="c1">// x and y are automatically cast to non-nullable after null check</span>
<span class="n">print</span><span class="p">(</span><span class="n">x</span> <span class="p">*</span> <span class="n">y</span><span class="p">)</span></code></pre>
<p>See <a href="null-safety.html">Null-safety</a>.</p>
<h2 id="using-type-checks-and-automatic-casts">Using type checks and automatic casts<a href="#using-type-checks-and-automatic-casts" class="anchor"></a></h2>
<p>The <em class="keyword">is</em> operator checks if an expression is an instance of a type.
If an immutable local variable or property is checked for a specific type, there’s no need to cast it explicitly:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">getStringLength</span><span class="p">(</span><span class="n">obj</span><span class="p">:</span> <span class="n">Any</span><span class="p">):</span> <span class="n">Int</span><span class="p">?</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">obj</span> <span class="k">is</span> <span class="n">String</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// `obj` is automatically cast to `String` in this branch</span>
<span class="k">return</span> <span class="n">obj</span><span class="p">.</span><span class="n">length</span>
<span class="p">}</span>
<span class="c1">// `obj` is still of type `Any` outside of the type-checked branch</span>
<span class="k">return</span> <span class="k">null</span>
<span class="p">}</span></code></pre>
<p>or</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">getStringLength</span><span class="p">(</span><span class="n">obj</span><span class="p">:</span> <span class="n">Any</span><span class="p">):</span> <span class="n">Int</span><span class="p">?</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">obj</span> <span class="p">!</span><span class="k">is</span> <span class="n">String</span><span class="p">)</span>
<span class="k">return</span> <span class="k">null</span>
<span class="c1">// `obj` is automatically cast to `String` in this branch</span>
<span class="k">return</span> <span class="n">obj</span><span class="p">.</span><span class="n">length</span>
<span class="p">}</span></code></pre>
<p>or even</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">getStringLength</span><span class="p">(</span><span class="n">obj</span><span class="p">:</span> <span class="n">Any</span><span class="p">):</span> <span class="n">Int</span><span class="p">?</span> <span class="p">{</span>
<span class="c1">// `obj` is automatically cast to `String` on the right-hand side of `&&`</span>
<span class="k">if</span> <span class="p">(</span><span class="n">obj</span> <span class="k">is</span> <span class="n">String</span> <span class="p">&&</span> <span class="n">obj</span><span class="p">.</span><span class="n">length</span> <span class="p">></span> <span class="m">0</span><span class="p">)</span>
<span class="k">return</span> <span class="n">obj</span><span class="p">.</span><span class="n">length</span>
<span class="k">return</span> <span class="k">null</span>
<span class="p">}</span></code></pre>
<p>See <a href="classes.html">Classes</a> and <a href="typecasts.html">Type casts</a>.</p>
<h2 id="using-a-for-loop">Using a <code>for</code> loop<a href="#using-a-for-loop" class="anchor"></a></h2>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">main</span><span class="p">(</span><span class="n">args</span><span class="p">:</span> <span class="n">Array</span><span class="p"><</span><span class="n">String</span><span class="p">>)</span> <span class="p">{</span>
<span class="k">for</span> <span class="p">(</span><span class="n">arg</span> <span class="k">in</span> <span class="n">args</span><span class="p">)</span>
<span class="n">print</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
<span class="p">}</span></code></pre>
<p>or</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="k">in</span> <span class="n">args</span><span class="p">.</span><span class="n">indices</span><span class="p">)</span>
<span class="n">print</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="n">i</span><span class="p">])</span></code></pre>
<p>See <a href="control-flow.html#for-loops">for loop</a>.</p>
<h2 id="using-a-while-loop">Using a <code>while</code> loop<a href="#using-a-while-loop" class="anchor"></a></h2>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">main</span><span class="p">(</span><span class="n">args</span><span class="p">:</span> <span class="n">Array</span><span class="p"><</span><span class="n">String</span><span class="p">>)</span> <span class="p">{</span>
<span class="k">var</span> <span class="py">i</span> <span class="p">=</span> <span class="m">0</span>
<span class="k">while</span> <span class="p">(</span><span class="n">i</span> <span class="p"><</span> <span class="n">args</span><span class="p">.</span><span class="n">size</span><span class="p">())</span>
<span class="n">print</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="n">i</span><span class="p">++])</span>
<span class="p">}</span></code></pre>
<p>See <a href="control-flow.html#while-loops">while loop</a>.</p>
<h2 id="using-when-expression">Using <code>when</code> expression<a href="#using-when-expression" class="anchor"></a></h2>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">cases</span><span class="p">(</span><span class="n">obj</span><span class="p">:</span> <span class="n">Any</span><span class="p">)</span> <span class="p">{</span>
<span class="k">when</span> <span class="p">(</span><span class="n">obj</span><span class="p">)</span> <span class="p">{</span>
<span class="m">1</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"One"</span><span class="p">)</span>
<span class="s">"Hello"</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"Greeting"</span><span class="p">)</span>
<span class="k">is</span> <span class="n">Long</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"Long"</span><span class="p">)</span>
<span class="p">!</span><span class="k">is</span> <span class="n">String</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"Not a string"</span><span class="p">)</span>
<span class="k">else</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"Unknown"</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span></code></pre>
<p>See <a href="control-flow.html#when-expression">when expression</a>.</p>
<h2 id="using-ranges">Using ranges<a href="#using-ranges" class="anchor"></a></h2>
<p>Check if a number is within a range using <em class="keyword">in</em> operator:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="k">in</span> <span class="m">1.</span><span class="p">.</span><span class="n">y</span><span class="p">-</span><span class="m">1</span><span class="p">)</span>
<span class="n">print</span><span class="p">(</span><span class="s">"OK"</span><span class="p">)</span></code></pre>
<p>Check if a number is out of range:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="p">!</span><span class="k">in</span> <span class="m">0.</span><span class="p">.</span><span class="n">array</span><span class="p">.</span><span class="n">lastIndex</span><span class="p">)</span>
<span class="n">print</span><span class="p">(</span><span class="s">"Out"</span><span class="p">)</span></code></pre>
<p>Iterating over a range:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">for</span> <span class="p">(</span><span class="n">x</span> <span class="k">in</span> <span class="m">1.</span><span class="p">.</span><span class="m">5</span><span class="p">)</span>
<span class="n">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span></code></pre>
<p>See <a href="ranges.html">Ranges</a>.</p>
<h2 id="using-collections">Using collections<a href="#using-collections" class="anchor"></a></h2>
<p>Iterating over a collection:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">for</span> <span class="p">(</span><span class="n">name</span> <span class="k">in</span> <span class="n">names</span><span class="p">)</span>
<span class="n">println</span><span class="p">(</span><span class="n">name</span><span class="p">)</span></code></pre>
<p>Checking if a collection contains an object using <em class="keyword">in</em> operator:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">if</span> <span class="p">(</span><span class="n">text</span> <span class="k">in</span> <span class="n">names</span><span class="p">)</span> <span class="c1">// names.contains(text) is called</span>
<span class="n">print</span><span class="p">(</span><span class="s">"Yes"</span><span class="p">)</span></code></pre>
<p>Using function literals to filter and map collections:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="n">names</span> <span class="n">filter</span> <span class="p">{</span> <span class="n">it</span><span class="p">.</span><span class="n">startsWith</span><span class="p">(</span><span class="s">"A"</span><span class="p">)</span> <span class="p">}</span> <span class="n">sortBy</span> <span class="p">{</span> <span class="n">it</span> <span class="p">}</span> <span class="n">map</span> <span class="p">{</span> <span class="n">it</span><span class="p">.</span><span class="n">toUpperCase</span><span class="p">()</span> <span class="p">}</span> <span class="n">forEach</span> <span class="p">{</span> <span class="n">print</span><span class="p">(</span><span class="n">it</span><span class="p">)</span> <span class="p">}</span></code></pre>
<p>See <a href="lambdas.html">Higher-order functions and Lambdas</a>.</p>
</article>
<article role="main" class="page-content g-9">
<a href="https://github.com/JetBrains/kotlin-web-site/edit/master/docs/reference/idioms.md"
class="page-link-to-github"
target="_blank"
title="Edit this page on GitHub">
<i class="github-icon"></i>
<span class="text">Edit Page</span>
</a>
<h1 id="idioms">Idioms<a href="#idioms" class="anchor"></a></h1>
<p>A collection of random and frequently used idioms in Kotlin. If you have a favorite idiom, contribute it. Do a pull request.</p>
<h3 id="creating-dtos-pojospocos">Creating DTO’s (POJO’s/POCO’s)<a href="#creating-dtos-pojospocos" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="n">data</span> <span class="k">class</span> <span class="nc">Customer</span><span class="p">(</span><span class="k">val</span> <span class="py">name</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="k">val</span> <span class="py">email</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span></code></pre>
<p>provides a <code>Customer</code> class with the following functionality:</p>
<ul>
<li>getters (and setters in case of <em class="keyword">var</em>’s) for all properties</li>
<li><code>equals()</code></li>
<li><code>hashCode()</code></li>
<li><code>toString()</code></li>
<li><code>copy()</code></li>
<li><code>component1()</code>, <code>component2()</code>, …, for all properties (see <a href="data-classes.html">Data classes</a>)</li>
</ul>
<h3 id="declaring-a-final-local-variable">Declaring a final local variable<a href="#declaring-a-final-local-variable" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">a</span> <span class="p">=</span> <span class="n">foo</span><span class="p">()</span></code></pre>
<h3 id="default-values-for-function-parameters">Default values for function parameters<a href="#default-values-for-function-parameters" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">Int</span> <span class="p">=</span> <span class="m">0</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">String</span> <span class="p">=</span> <span class="s">""</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span></code></pre>
<h3 id="filtering-a-list">Filtering a list<a href="#filtering-a-list" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">positives</span> <span class="p">=</span> <span class="n">list</span><span class="p">.</span><span class="n">filter</span> <span class="p">{</span> <span class="n">x</span> <span class="p">-></span> <span class="n">x</span> <span class="p">></span> <span class="m">0</span> <span class="p">}</span></code></pre>
<p>Or alternatively, even shorter:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">positives</span> <span class="p">=</span> <span class="n">list</span><span class="p">.</span><span class="n">filter</span> <span class="p">{</span> <span class="n">it</span> <span class="p">></span> <span class="m">0</span> <span class="p">}</span></code></pre>
<h3 id="string-interpolation">String Interpolation<a href="#string-interpolation" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="n">println</span><span class="p">(</span><span class="s">"Name $name"</span><span class="p">)</span></code></pre>
<h3 id="instance-checks">Instance Checks<a href="#instance-checks" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">when</span> <span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span>
<span class="k">is</span> <span class="n">Foo</span> <span class="p">-></span> <span class="p">...</span>
<span class="k">is</span> <span class="n">Bar</span> <span class="p">-></span> <span class="p">...</span>
<span class="k">else</span> <span class="p">-></span> <span class="p">...</span>
<span class="p">}</span></code></pre>
<h3 id="traversing-a-maplist-of-pairs">Traversing a map/list of pairs<a href="#traversing-a-maplist-of-pairs" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">for</span> <span class="p">((</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="k">in</span> <span class="n">map</span><span class="p">)</span> <span class="p">{</span>
<span class="n">println</span><span class="p">(</span><span class="s">"$k -> $v"</span><span class="p">)</span>
<span class="p">}</span></code></pre>
<p><code>k</code>, <code>v</code> can be called anything.</p>
<h3 id="using-ranges">Using ranges<a href="#using-ranges" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="k">in</span> <span class="m">1.</span><span class="p">.</span><span class="m">100</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span>
<span class="k">for</span> <span class="p">(</span><span class="n">x</span> <span class="k">in</span> <span class="m">2.</span><span class="p">.</span><span class="m">10</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span></code></pre>
<h3 id="read-only-list">Read-only list<a href="#read-only-list" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">list</span> <span class="p">=</span> <span class="n">listOf</span><span class="p">(</span><span class="s">"a"</span><span class="p">,</span> <span class="s">"b"</span><span class="p">,</span> <span class="s">"c"</span><span class="p">)</span></code></pre>
<h3 id="read-only-map">Read-only map<a href="#read-only-map" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">map</span> <span class="p">=</span> <span class="n">mapOf</span><span class="p">(</span><span class="s">"a"</span> <span class="n">to</span> <span class="m">1</span><span class="p">,</span> <span class="s">"b"</span> <span class="n">to</span> <span class="m">2</span><span class="p">,</span> <span class="s">"c"</span> <span class="n">to</span> <span class="m">3</span><span class="p">)</span></code></pre>
<h3 id="accessing-a-map">Accessing a map<a href="#accessing-a-map" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="n">println</span><span class="p">(</span><span class="n">map</span><span class="p">[</span><span class="s">"key"</span><span class="p">])</span>
<span class="n">map</span><span class="p">[</span><span class="s">"key"</span><span class="p">]</span> <span class="p">=</span> <span class="n">value</span></code></pre>
<h3 id="lazy-property">Lazy property<a href="#lazy-property" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">p</span><span class="p">:</span> <span class="n">String</span> <span class="k">by</span> <span class="n">Delegates</span><span class="p">.</span><span class="n">lazy</span> <span class="p">{</span>
<span class="c1">// compute the string</span>
<span class="p">}</span></code></pre>
<h3 id="extension-functions">Extension Functions<a href="#extension-functions" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">String</span><span class="p">.</span><span class="n">spaceToCamelCase</span><span class="p">()</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span>
<span class="s">"Convert this to camelcase"</span><span class="p">.</span><span class="n">spaceToCamelCase</span><span class="p">()</span></code></pre>
<h3 id="creating-a-singleton">Creating a singleton<a href="#creating-a-singleton" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">object</span> <span class="nc">Resource</span> <span class="p">{</span>
<span class="k">val</span> <span class="py">name</span> <span class="p">=</span> <span class="s">"Name"</span>
<span class="p">}</span></code></pre>
<h3 id="if-not-null-shorthand">If not null shorthand<a href="#if-not-null-shorthand" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">files</span> <span class="p">=</span> <span class="n">File</span><span class="p">(</span><span class="s">"Test"</span><span class="p">).</span><span class="n">listFiles</span><span class="p">()</span>
<span class="n">println</span><span class="p">(</span><span class="n">files</span><span class="o">?.</span><span class="n">size</span><span class="p">)</span></code></pre>
<h3 id="if-not-null-and-else-shorthand">If not null and else shorthand<a href="#if-not-null-and-else-shorthand" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">files</span> <span class="p">=</span> <span class="n">File</span><span class="p">(</span><span class="s">"Test"</span><span class="p">).</span><span class="n">listFiles</span><span class="p">()</span>
<span class="n">println</span><span class="p">(</span><span class="n">files</span><span class="o">?.</span><span class="n">size</span> <span class="o">?:</span> <span class="s">"empty"</span><span class="p">)</span></code></pre>
<h3 id="executing-a-statement-if-null">Executing a statement if null<a href="#executing-a-statement-if-null" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">data</span> <span class="p">=</span> <span class="p">...</span>
<span class="k">val</span> <span class="py">email</span> <span class="p">=</span> <span class="n">data</span><span class="p">[</span><span class="s">"email"</span><span class="p">]</span> <span class="o">?:</span> <span class="k">throw</span> <span class="n">IllegalStateException</span><span class="p">(</span><span class="s">"Email is missing!"</span><span class="p">)</span></code></pre>
<h3 id="execute-if-not-null">Execute if not null<a href="#execute-if-not-null" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">data</span> <span class="p">=</span> <span class="p">...</span>
<span class="n">data</span><span class="o">?.</span><span class="n">let</span> <span class="p">{</span>
<span class="p">...</span> <span class="c1">// execute this block if not null</span>
<span class="p">}</span></code></pre>
<h3 id="return-on-when-statement">Return on when statement<a href="#return-on-when-statement" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">transform</span><span class="p">(</span><span class="n">color</span><span class="p">:</span> <span class="n">String</span><span class="p">):</span> <span class="n">Int</span> <span class="p">{</span>
<span class="k">return</span> <span class="k">when</span> <span class="p">(</span><span class="n">color</span><span class="p">)</span> <span class="p">{</span>
<span class="s">"Red"</span> <span class="p">-></span> <span class="m">0</span>
<span class="s">"Green"</span> <span class="p">-></span> <span class="m">1</span>
<span class="s">"Blue"</span> <span class="p">-></span> <span class="m">2</span>
<span class="k">else</span> <span class="p">-></span> <span class="k">throw</span> <span class="n">IllegalArgumentException</span><span class="p">(</span><span class="s">"Invalid color param value"</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span></code></pre>
<h3 id="return-on-try-catch-block">Return on try catch block<a href="#return-on-try-catch-block" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">test</span><span class="p">()</span> <span class="p">{</span>
<span class="k">val</span> <span class="py">result</span> <span class="p">=</span> <span class="k">try</span> <span class="p">{</span>
<span class="n">count</span><span class="p">()</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">e</span><span class="p">:</span> <span class="n">ArithmeticException</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="n">IllegalStateException</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="p">}</span>
<span class="c1">// Working with result</span>
<span class="p">}</span></code></pre>
<h3 id="return-on-if-statement">Return on if statement<a href="#return-on-if-statement" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">foo</span><span class="p">(</span><span class="n">param</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">{</span>
<span class="k">val</span> <span class="py">result</span> <span class="p">=</span> <span class="k">if</span> <span class="p">(</span><span class="n">param</span> <span class="p">==</span> <span class="m">1</span><span class="p">)</span> <span class="p">{</span>
<span class="s">"one"</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">param</span> <span class="p">==</span> <span class="m">2</span><span class="p">)</span> <span class="p">{</span>
<span class="s">"two"</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="s">"three"</span>
<span class="p">}</span>
<span class="p">}</span></code></pre>
<h3 id="single-expression-functions">Single-expression functions<a href="#single-expression-functions" class="anchor"></a></h3>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">theAnswer</span><span class="p">()</span> <span class="p">=</span> <span class="m">42</span></code></pre>
<p>This is equivalent to</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">theAnswer</span><span class="p">():</span> <span class="n">Int</span> <span class="p">{</span>
<span class="k">return</span> <span class="m">42</span>
<span class="p">}</span></code></pre>
<p>This can be effectively combined with other idioms, leading to shorter code. E.g. with the <em class="keyword">when</em>-expression:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">transform</span><span class="p">(</span><span class="n">color</span><span class="p">:</span> <span class="n">String</span><span class="p">):</span> <span class="n">Int</span> <span class="p">=</span> <span class="k">when</span> <span class="p">(</span><span class="n">color</span><span class="p">)</span> <span class="p">{</span>
<span class="s">"Red"</span> <span class="p">-></span> <span class="m">0</span>
<span class="s">"Green"</span> <span class="p">-></span> <span class="m">1</span>
<span class="s">"Blue"</span> <span class="p">-></span> <span class="m">2</span>
<span class="k">else</span> <span class="p">-></span> <span class="k">throw</span> <span class="n">IllegalArgumentException</span><span class="p">(</span><span class="s">"Invalid color param value"</span><span class="p">)</span>
<span class="p">}</span></code></pre>
</article>
<article role="main" class="page-content g-9">
<a href="https://github.com/JetBrains/kotlin-web-site/edit/master/docs/reference/coding-conventions.md"
class="page-link-to-github"
target="_blank"
title="Edit this page on GitHub">
<i class="github-icon"></i>
<span class="text">Edit Page</span>
</a>
<h1 id="coding-conventions">Coding Conventions<a href="#coding-conventions" class="anchor"></a></h1>
<p>This page contains the current coding style for the Kotlin language.</p>
<h2 id="naming-style">Naming Style<a href="#naming-style" class="anchor"></a></h2>
<p>If in doubt default to the Java Coding Conventions such as:</p>
<ul>
<li>use of camelCase for names (and avoid underscore in names)</li>
<li>types start with upper case</li>
<li>methods and properties start with lower case</li>
<li>use 4 space indentation</li>
<li>public functions should have documentation such that it appears in Kotlin Doc</li>
</ul>
<p>Kotlin does not have fields as a primary concept in the language – it only has properties.
Avoid the use of prefixes on properties, such as _ or m_ or other kinds of notation.
If you need access to a backing field of a property, use the $ prefix: <code>$foo</code> to refer to a field behind property <code>foo</code>;
never create a private property and call it <code>_foo</code></p>
<h2 id="colon">Colon<a href="#colon" class="anchor"></a></h2>
<p>There is a space before colon where colon separates type and supertype and there’s no space where colon separates instance and type:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="n">interface</span> <span class="n">Foo</span><span class="p"><</span><span class="k">out</span> <span class="n">T</span> <span class="p">:</span> <span class="n">Any</span><span class="p">></span> <span class="p">:</span> <span class="n">Bar</span> <span class="p">{</span>
<span class="k">fun</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">T</span>
<span class="p">}</span></code></pre>
<h2 id="unit">Unit<a href="#unit" class="anchor"></a></h2>
<p>If a function returns Unit, the return type should be omitted:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">foo</span><span class="p">()</span> <span class="p">{</span> <span class="c1">// ": Unit" is omitted here</span>
<span class="p">}</span></code></pre>
</article>
<article role="main" class="page-content g-9">
<a href="https://github.com/JetBrains/kotlin-web-site/edit/master/docs/reference/basic-types.md"
class="page-link-to-github"
target="_blank"
title="Edit this page on GitHub">
<i class="github-icon"></i>
<span class="text">Edit Page</span>
</a>
<h1 id="basic-types">Basic Types<a href="#basic-types" class="anchor"></a></h1>
<p>In Kotlin, everything is an object in the sense that we can call member functions and properties on any variable. Some types are built-in, because their implementation is optimized, but to the user they look like ordinary classes. In this section we describe most of these types: numbers, characters, booleans and arrays.</p>
<h2 id="numbers">Numbers<a href="#numbers" class="anchor"></a></h2>
<p>Kotlin handles numbers in a way close to Java, but not exactly the same. For example, there are no implicit widening conversions for numbers, and literals are slightly different in some cases.</p>
<p>Kotlin provides the following built-in types representing numbers (this is close to Java):</p>
<table>
<thead>
<tr>
<th>Type</th>
<th>Bitwidth</th>
</tr>
</thead>
<tbody>
<tr>
<td>Double</td>
<td>64</td>
</tr>
<tr>
<td>Float</td>
<td>32</td>
</tr>
<tr>
<td>Long</td>
<td>64</td>
</tr>
<tr>
<td>Int</td>
<td>32</td>
</tr>
<tr>
<td>Short</td>
<td>16</td>
</tr>
<tr>
<td>Byte</td>
<td>8</td>
</tr>
</tbody>
</table>
<p>Note that characters are not numbers in Kotlin.</p>
<h3 id="literal-constants">Literal Constants<a href="#literal-constants" class="anchor"></a></h3>
<p>There are the following kinds of literal constants for integral values:</p>
<ul>
<li>Decimals: <code>123</code>
<ul>
<li>Longs are tagged by a capital <code>L</code>: <code>123L</code></li>
</ul>
</li>
<li>Hexadecimals: <code>0x0F</code></li>
<li>Binaries: <code>0b00001011</code></li>
</ul>
<p>NOTE: Octal literals are not supported.</p>
<p>Kotlin also supports a conventional notation for floating-point numbers:</p>
<ul>
<li>Doubles by default: <code>123.5</code>, <code>123.5e10</code></li>
<li>Floats are tagged by <code>f</code> or <code>F</code>: <code>123.5f</code> </li>
</ul>
<h3 id="representation">Representation<a href="#representation" class="anchor"></a></h3>
<p>On the Java platform, numbers are physically stored as JVM primitive types, unless we need a nullable number reference (e.g. <code>Int?</code>) or generics are involved.
In the latter cases numbers are boxed.</p>
<p>Note that boxing of numbers does not preserve identity:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">a</span><span class="p">:</span> <span class="n">Int</span> <span class="p">=</span> <span class="m">10000</span>
<span class="n">print</span><span class="p">(</span><span class="n">a</span> <span class="n">identityEquals</span> <span class="n">a</span><span class="p">)</span> <span class="c1">// Prints 'true'</span>
<span class="k">val</span> <span class="py">boxedA</span><span class="p">:</span> <span class="n">Int</span><span class="p">?</span> <span class="p">=</span> <span class="n">a</span>
<span class="k">val</span> <span class="py">anotherBoxedA</span><span class="p">:</span> <span class="n">Int</span><span class="p">?</span> <span class="p">=</span> <span class="n">a</span>
<span class="n">print</span><span class="p">(</span><span class="n">boxedA</span> <span class="n">identityEquals</span> <span class="n">anotherBoxedA</span><span class="p">)</span> <span class="c1">// !!!Prints 'false'!!!</span></code></pre>
<p>On the other hand, it preserves equality:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">a</span><span class="p">:</span> <span class="n">Int</span> <span class="p">=</span> <span class="m">10000</span>
<span class="n">print</span><span class="p">(</span><span class="n">a</span> <span class="p">==</span> <span class="n">a</span><span class="p">)</span> <span class="c1">// Prints 'true'</span>
<span class="k">val</span> <span class="py">boxedA</span><span class="p">:</span> <span class="n">Int</span><span class="p">?</span> <span class="p">=</span> <span class="n">a</span>
<span class="k">val</span> <span class="py">anotherBoxedA</span><span class="p">:</span> <span class="n">Int</span><span class="p">?</span> <span class="p">=</span> <span class="n">a</span>
<span class="n">print</span><span class="p">(</span><span class="n">boxedA</span> <span class="p">==</span> <span class="n">anotherBoxedA</span><span class="p">)</span> <span class="c1">// Prints 'true'</span></code></pre>
<h3 id="explicit-conversions">Explicit Conversions<a href="#explicit-conversions" class="anchor"></a></h3>
<p>Due to different representations, smaller types are not subtypes of bigger ones.
If they were, we would have troubles of the following sort</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="c1">// Hypothetical code, does not actually compile:</span>
<span class="k">val</span> <span class="py">a</span><span class="p">:</span> <span class="n">Int</span><span class="p">?</span> <span class="p">=</span> <span class="m">1</span> <span class="c1">// A boxed Int (java.lang.Integer)</span>
<span class="k">val</span> <span class="py">b</span><span class="p">:</span> <span class="n">Long</span><span class="p">?</span> <span class="p">=</span> <span class="n">a</span> <span class="c1">// implicit conversion yields a boxed Long (java.lang.Long)</span>
<span class="n">print</span><span class="p">(</span><span class="n">a</span> <span class="p">==</span> <span class="n">b</span><span class="p">)</span> <span class="c1">// Surprise! This prints "false" as Long's equals() check for other part to be Long as well</span></code></pre>
<p>So not only identity, but even equality would have been lost silently all over the place.</p>
<p>As a consequence, smaller types are NOT implicitly converted to bigger types.
This means that we cannot assign a value of type <code>Byte</code> to an <code>Int</code> variable without an explicit conversion</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">b</span><span class="p">:</span> <span class="n">Byte</span> <span class="p">=</span> <span class="m">1</span> <span class="c1">// OK, literals are checked statically</span>
<span class="k">val</span> <span class="py">i</span><span class="p">:</span> <span class="n">Int</span> <span class="p">=</span> <span class="n">b</span> <span class="c1">// ERROR</span></code></pre>
<p>We can use explicit conversions to widen numbers</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">i</span><span class="p">:</span> <span class="n">Int</span> <span class="p">=</span> <span class="n">b</span><span class="p">.</span><span class="n">toInt</span><span class="p">()</span> <span class="c1">// OK: explicitly widened</span></code></pre>
<p>Every number type supports the following conversions:</p>
<ul>
<li><code>toByte(): Byte</code></li>
<li><code>toShort(): Short</code></li>
<li><code>toInt(): Int</code></li>
<li><code>toLong(): Long</code></li>
<li><code>toFloat(): Float</code></li>
<li><code>toDouble(): Double</code></li>
<li><code>toChar(): Char</code></li>
</ul>
<p>Absence of implicit conversions is rarely noticeable because we can use literals almost freely cause the type is inferred from the context, and arithmetical operations are overloaded for appropriate conversions, for example</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">l</span> <span class="p">=</span> <span class="m">1.</span><span class="n">toLong</span><span class="p">()</span> <span class="p">+</span> <span class="m">3</span> <span class="c1">// Long + Int => Long</span></code></pre>
<h3 id="operations">Operations<a href="#operations" class="anchor"></a></h3>
<p>Kotlin supports the standard set of arithmetical operations over numbers, which are declared as members of appropriate classes (but the compiler optimizes the calls down to the corresponding instructions).
See <a href="operator-overloading.html">Operator overloading</a>.</p>
<p>As of bitwise operations, there’re no special characters for them, but just named functions that can be called in infix form, for example:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">x</span> <span class="p">=</span> <span class="p">(</span><span class="m">1</span> <span class="n">shl</span> <span class="m">2</span><span class="p">)</span> <span class="n">and</span> <span class="m">0</span><span class="n">x000FF000</span></code></pre>
<p>Here is the complete list of bitwise operations (available for <code>Int</code> and <code>Long</code> only):</p>
<ul>
<li><code>shl(bits)</code> – signed shift left (Java’s <code><<</code>)</li>
<li><code>shr(bits)</code> – signed shift right (Java’s <code>>></code>)</li>
<li><code>ushr(bits)</code> – unsigned shift right (Java’s <code>>>></code>)</li>
<li><code>and(bits)</code> – bitwise and</li>
<li><code>or(bits)</code> – bitwise or</li>
<li><code>xor(bits)</code> – bitwise xor</li>
<li><code>inv()</code> – bitwise inversion</li>
</ul>
<h2 id="characters">Characters<a href="#characters" class="anchor"></a></h2>
<p>Characters are represented by the type <code>Char</code>. They can not be treated directly as numbers</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">check</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">Char</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">c</span> <span class="p">==</span> <span class="m">1</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// ERROR: incompatible types</span>
<span class="c1">// ...</span>
<span class="p">}</span>
<span class="p">}</span></code></pre>
<p>Character literals go in single quotes: <code>'1'</code>, <code>'\n'</code>, <code>'\uFF00'</code>.
We can explicitly convert a character to an <code>Int</code> number</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">fun</span> <span class="nf">decimalDigitValue</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">Char</span><span class="p">):</span> <span class="n">Int</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="n">c</span> <span class="p">!</span><span class="k">in</span> <span class="sc">'0'</span><span class="p">..</span><span class="sc">'9'</span><span class="p">)</span>
<span class="k">throw</span> <span class="n">IllegalArgumentException</span><span class="p">(</span><span class="s">"Out of range"</span><span class="p">)</span>
<span class="k">return</span> <span class="n">c</span><span class="p">.</span><span class="n">toInt</span><span class="p">()</span> <span class="p">-</span> <span class="sc">'0'</span><span class="p">.</span><span class="n">toInt</span><span class="p">()</span> <span class="c1">// Explicit conversions to numbers</span>
<span class="p">}</span></code></pre>
<p>Like numbers, characters are boxed when a nullable reference is needed. Identity is not preserved by the boxing operation.</p>
<h2 id="booleans">Booleans<a href="#booleans" class="anchor"></a></h2>
<p>The type <code>Boolean</code> represents booleans, and has two values: <em class="keyword">true</em> and <em class="keyword">false</em>.</p>
<p>Booleans are boxed if a nullable reference is needed.</p>
<p>Built-in operations on booleans include</p>
<ul>
<li><code>||</code> – lazy disjunction</li>
<li><code>&&</code> – lazy conjunction</li>
</ul>
<h2 id="arrays">Arrays<a href="#arrays" class="anchor"></a></h2>
<p>Arrays in Kotlin are represented by the <code>Array</code> class, that has <code>get</code> and <code>set</code> functions (that turn into <code>[]</code> by operator overloading conventions), and <code>size</code>, along with a few other useful member functions:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">class</span> <span class="nc">Array</span><span class="p"><</span><span class="n">T</span><span class="p">></span> <span class="k">private</span> <span class="p">()</span> <span class="p">{</span>
<span class="k">fun</span> <span class="nf">size</span><span class="p">():</span> <span class="n">Int</span>
<span class="k">fun</span> <span class="nf">get</span><span class="p">(</span><span class="n">index</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">T</span>
<span class="k">fun</span> <span class="nf">set</span><span class="p">(</span><span class="n">index</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="n">T</span><span class="p">):</span> <span class="n">Unit</span>
<span class="k">fun</span> <span class="nf">iterator</span><span class="p">():</span> <span class="n">Iterator</span><span class="p"><</span><span class="n">T</span><span class="p">></span>
<span class="c1">// ...</span>
<span class="p">}</span></code></pre>
<p>To create an array, we can use a library function <code>arrayOf()</code> and pass the item values to it, so that <code>arrayOf(1, 2, 3)</code> creates an array [1, 2, 3].
Alternatively, the <code>arrayOfNulls()</code> library function can be used to create an array of a given size filled with null elements.</p>
<p>Another option is to use a factory function that takes the array size and the function that can return the initial value
of each array element given its index:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="c1">// Creates an Array<String> with values ["0", "1", "4", "9", "16"]</span>
<span class="k">val</span> <span class="py">asc</span> <span class="p">=</span> <span class="n">Array</span><span class="p">(</span><span class="m">5</span><span class="p">,</span> <span class="p">{</span><span class="n">i</span> <span class="p">-></span> <span class="p">(</span><span class="n">i</span> <span class="p">*</span> <span class="n">i</span><span class="p">).</span><span class="n">toString</span><span class="p">()})</span></code></pre>
<p>As we said above, the <code>[]</code> operation stands for calls to member functions <code>get()</code> and <code>set()</code>.</p>
<p>Note: unlike Java, arrays in Kotlin are invariant. This means that Kotlin does not let us assign an <code>Array<String></code>
to an <code>Array<Any></code>, which prevents a possible runtime failure (but you can use <code>Array<out Any></code>,
see <a href="generics.html#type-projections">Type Projections</a>).</p>
<p>Kotlin also has specialized classes to represent arrays of primitive types without boxing overhead: ByteArray,
ShortArray, IntArray and so on. These classes have no inheritance relation to the <code>Array</code> class, but they
have the same set of methods and properties. Each of them also has a corresponding factory function:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">x</span><span class="p">:</span> <span class="n">IntArray</span> <span class="p">=</span> <span class="n">intArray</span><span class="p">(</span><span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="m">3</span><span class="p">)</span>
<span class="n">x</span><span class="p">[</span><span class="m">0</span><span class="p">]</span> <span class="p">=</span> <span class="n">x</span><span class="p">[</span><span class="m">1</span><span class="p">]</span> <span class="p">+</span> <span class="n">x</span><span class="p">[</span><span class="m">2</span><span class="p">]</span></code></pre>
<h2 id="strings">Strings<a href="#strings" class="anchor"></a></h2>
<p>Strings are represented by the type <code>String</code>. Strings are immutable.
Elements of a string are characters that can be accessed by the indexing operation: <code>s[i]</code>.
A string can be iterated over with a <em class="keyword">for</em>-loop:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">for</span> <span class="p">(</span><span class="n">c</span> <span class="k">in</span> <span class="n">str</span><span class="p">)</span> <span class="p">{</span>
<span class="n">println</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="p">}</span></code></pre>
<h3 id="string-literals">String Literals<a href="#string-literals" class="anchor"></a></h3>
<p>Kotlin has two types of string literals: escaped strings that may have escaped characters in them and raw strings that can contain newlines and arbitrary text. An escaped string is very much like a Java string:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">s</span> <span class="p">=</span> <span class="s">"Hello, world!\n"</span></code></pre>
<p>Escaping is done in the conventional way, with a backslash.</p>
<p>A raw string is delimited by a triple quote (<code>"""</code>), contains no escaping and can contain newlines and any other characters:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">text</span> <span class="p">=</span> <span class="s">"""</span>
<span class="k">for</span> <span class="p">(</span><span class="n">c</span> <span class="k">in</span> <span class="s">"foo"</span><span class="p">)</span>
<span class="n">print</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="s">"""</span></code></pre>
<h3 id="templates">Templates<a href="#templates" class="anchor"></a></h3>
<p>Strings may contain template expressions, i.e. pieces of code that are evaluated and whose results are concatenated into the string. A template expression starts with a dollar sign ($) and consists of either a simple name:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">i</span> <span class="p">=</span> <span class="m">10</span>
<span class="k">val</span> <span class="py">s</span> <span class="p">=</span> <span class="s">"i = $i"</span> <span class="c1">// evaluates to "i = 10"</span></code></pre>
<p>or an arbitrary expression in curly braces:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">s</span> <span class="p">=</span> <span class="s">"abc"</span>
<span class="k">val</span> <span class="py">str</span> <span class="p">=</span> <span class="s">"$s.length is ${s.length}"</span> <span class="c1">// evaluates to "abc.length is 3"</span></code></pre>
</article>
<article role="main" class="page-content g-9">
<a href="https://github.com/JetBrains/kotlin-web-site/edit/master/docs/reference/packages.md"
class="page-link-to-github"
target="_blank"
title="Edit this page on GitHub">
<i class="github-icon"></i>
<span class="text">Edit Page</span>
</a>
<h1 id="packages">Packages<a href="#packages" class="anchor"></a></h1>
<p>A source file may start with a package declaration:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">package</span> <span class="nn">foo.bar</span>
<span class="k">fun</span> <span class="nf">baz</span><span class="p">()</span> <span class="p">{}</span>
<span class="k">class</span> <span class="nc">Goo</span> <span class="p">{}</span>
<span class="c1">// ...</span></code></pre>
<p>All the contents (such as classes and functions) of the source file are contained by the package declared.
So, in the example above, the full name of <code>baz()</code> is <code>foo.bar.baz</code>, and the full name of <code>Goo</code> is <code>foo.bar.Goo</code>. </p>
<p>If the package is not specified, the contents of such a file belong to “default” package that has no name.</p>
<h2 id="imports">Imports<a href="#imports" class="anchor"></a></h2>
<p>Apart from the default imports declared by the module, each file may contain its own import directives.
Syntax for imports is described in the <a href="grammar.html#imports">grammar</a>.</p>
<p>We can import either a single name, e.g.</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">import</span> <span class="nn">foo.Bar</span> <span class="c1">// Bar is now accessible without qualification</span></code></pre>
<p>or all the accessible contents of a scope (package, class, object etc):</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">import</span> <span class="nn">foo.*</span> <span class="c1">// everything in 'foo' becomes accessible</span></code></pre>
<p>If there is a name clash, we can disambiguate by using <em class="keyword">as</em> keyword to locally rename the clashing entity:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">import</span> <span class="nn">foo.Bar</span> <span class="c1">// Bar is accessible</span>
<span class="k">import</span> <span class="nn">bar.Bar</span> <span class="k">as</span> <span class="n">bBar</span> <span class="c1">// bBar stands for 'bar.Bar'</span></code></pre>
<h2 id="visibility-and-package-nesting">Visibility and Package Nesting<a href="#visibility-and-package-nesting" class="anchor"></a></h2>
<p>If a top-level declaration is marked <em class="keyword">private</em>, it is private to the package it’s declared in (see <a href="visibility-modifiers.html">Visibility Modifiers</a>).
Since packages really nest in Kotlin, i.e. package <code>foo.bar</code> is considered a member of <code>foo</code>, if something is <em class="keyword">private</em> in a package,
it is visible to all its subpackages.</p>
<p>Note that members of outer packages are <strong>not</strong> imported by default, i.e. in a file in package <code>foo.bar</code> we can’t access
members of <code>foo</code> without importing them.</p>
</article>
<article role="main" class="page-content g-9">
<a href="https://github.com/JetBrains/kotlin-web-site/edit/master/docs/reference/control-flow.md"
class="page-link-to-github"
target="_blank"
title="Edit this page on GitHub">
<i class="github-icon"></i>
<span class="text">Edit Page</span>
</a>
<h1 id="control-flow">Control Flow<a href="#control-flow" class="anchor"></a></h1>
<h2 id="if-expression">If Expression<a href="#if-expression" class="anchor"></a></h2>
<p>In Kotlin, <em class="keyword">if</em> is an expression, i.e. it returns a value.
Therefore there is no ternary operator (condition ? then : else), because ordinary <em class="keyword">if</em> works fine in this role.</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="c1">// Traditional usage </span>
<span class="k">var</span> <span class="py">max</span> <span class="p">=</span> <span class="n">a</span>
<span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="p"><</span> <span class="n">b</span><span class="p">)</span>
<span class="n">max</span> <span class="p">=</span> <span class="n">b</span>
<span class="c1">// With else </span>
<span class="k">var</span> <span class="py">max</span><span class="p">:</span> <span class="n">Int</span>
<span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="p">></span> <span class="n">b</span><span class="p">)</span>
<span class="n">max</span> <span class="p">=</span> <span class="n">a</span>
<span class="k">else</span>
<span class="n">max</span> <span class="p">=</span> <span class="n">b</span>
<span class="c1">// As expression </span>
<span class="k">val</span> <span class="py">max</span> <span class="p">=</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="p">></span> <span class="n">b</span><span class="p">)</span> <span class="n">a</span> <span class="k">else</span> <span class="n">b</span></code></pre>
<p><em class="keyword">if</em> branches can be blocks, and the last expression is the value of a block:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">max</span> <span class="p">=</span> <span class="k">if</span> <span class="p">(</span><span class="n">a</span> <span class="p">></span> <span class="n">b</span><span class="p">)</span> <span class="p">{</span>
<span class="n">print</span><span class="p">(</span><span class="s">"Choose a"</span><span class="p">)</span>
<span class="n">a</span>
<span class="p">}</span>
<span class="k">else</span> <span class="p">{</span>
<span class="n">print</span><span class="p">(</span><span class="s">"Choose b"</span><span class="p">)</span>
<span class="n">b</span>
<span class="p">}</span></code></pre>
<p>When <em class="keyword">if</em> has only one branch, or one of its branches results in <code>Unit</code>, its type is <code>Unit</code>.</p>
<p>See the <a href="grammar.html#if">grammar for <em class="keyword">if</em></a>.</p>
<h2 id="when-expression">When Expression<a href="#when-expression" class="anchor"></a></h2>
<p><em class="keyword">when</em> replaces the switch operator of C-like languages. In the simplest form it looks like this</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">when</span> <span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span>
<span class="m">1</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"x == 1"</span><span class="p">)</span>
<span class="m">2</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"x == 2"</span><span class="p">)</span>
<span class="k">else</span> <span class="p">-></span> <span class="p">{</span> <span class="c1">// Note the block</span>
<span class="n">print</span><span class="p">(</span><span class="s">"x is neither 1 nor 2"</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span></code></pre>
<p><em class="keyword">when</em> matches its argument against all branches consequently until some branch condition is satisfied.
<em class="keyword">when</em> can be used either as an expression or as a statement. If it is used as an expression, the value
of the satisfied branch becomes the value of the overall expression. If it is used as a statement, the values of
individual branches are ignored. (Just like with <em class="keyword">if</em>, each branch can be a block, and its value
is the value of the last expression in the block.)</p>
<p>The <em class="keyword">else</em> branch is evaluated if none of the other branch conditions are satisfied.
If <em class="keyword">when</em> is used as an expression, the <em class="keyword">else</em> branch is mandatory,
unless the compiler can prove that all possible cases are covered with branch conditions.</p>
<p>If many cases should be handled in the same way, the branch conditions may be combined with a comma:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">when</span> <span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span>
<span class="m">0</span><span class="p">,</span> <span class="m">1</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"x == 0 or x == 1"</span><span class="p">)</span>
<span class="k">else</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"otherwise"</span><span class="p">)</span>
<span class="p">}</span></code></pre>
<p>We can use arbitrary expressions (not only constants) as branch conditions</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">when</span> <span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span>
<span class="n">parseInt</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"s encodes x"</span><span class="p">)</span>
<span class="k">else</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"s does not encode x"</span><span class="p">)</span>
<span class="p">}</span></code></pre>
<p>We can also check a value for being <em class="keyword">in</em> or <em class="keyword">!in</em> a <a href="ranges.html">range</a> or a collection:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">when</span> <span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span>
<span class="k">in</span> <span class="m">1.</span><span class="p">.</span><span class="m">10</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"x is in the range"</span><span class="p">)</span>
<span class="k">in</span> <span class="n">validNumbers</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"x is valid"</span><span class="p">)</span>
<span class="p">!</span><span class="k">in</span> <span class="m">10.</span><span class="p">.</span><span class="m">20</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"x is outside the range"</span><span class="p">)</span>
<span class="k">else</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"none of the above"</span><span class="p">)</span>
<span class="p">}</span></code></pre>
<p>Another possibility is to check that a value <em class="keyword">is</em> or <em class="keyword">!is</em> of a particular type. Note that,
due to <a href="typecasts.html#smart-casts">smart casts</a>, you can access the methods and properties of the type without
any extra checks.</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">val</span> <span class="py">hasPrefix</span> <span class="p">=</span> <span class="k">when</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span>
<span class="k">is</span> <span class="n">String</span> <span class="p">-></span> <span class="n">x</span><span class="p">.</span><span class="n">startsWith</span><span class="p">(</span><span class="s">"prefix"</span><span class="p">)</span>
<span class="k">else</span> <span class="p">-></span> <span class="k">false</span>
<span class="p">}</span></code></pre>
<p><em class="keyword">when</em> can also be used as a replacement for an <em class="keyword">if</em>-<em class="keyword">else</em> <em class="keyword">if</em> chain.
If no argument is supplied, the branch conditions are simply boolean expressions, and a branch is executed when its condition is true:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">when</span> <span class="p">{</span>
<span class="n">x</span><span class="p">.</span><span class="n">isOdd</span><span class="p">()</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"x is odd"</span><span class="p">)</span>
<span class="n">x</span><span class="p">.</span><span class="n">isEven</span><span class="p">()</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"x is even"</span><span class="p">)</span>
<span class="k">else</span> <span class="p">-></span> <span class="n">print</span><span class="p">(</span><span class="s">"x is funny"</span><span class="p">)</span>
<span class="p">}</span></code></pre>
<p>See the <a href="grammar.html#when">grammar for <em class="keyword">when</em></a>.</p>
<h2 id="for-loops">For Loops<a href="#for-loops" class="anchor"></a></h2>
<p><em class="keyword">for</em> loop iterates through anything that provides an iterator. The syntax is as follows:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">for</span> <span class="p">(</span><span class="n">item</span> <span class="k">in</span> <span class="n">collection</span><span class="p">)</span>
<span class="n">print</span><span class="p">(</span><span class="n">item</span><span class="p">)</span></code></pre>
<p>The body can be a block.</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">for</span> <span class="p">(</span><span class="n">item</span><span class="p">:</span> <span class="n">Int</span> <span class="k">in</span> <span class="n">ints</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// ...</span>
<span class="p">}</span></code></pre>
<p>As mentioned before, <em class="keyword">for</em> iterates through anything that provides an iterator, i.e.</p>
<ul>
<li>has an instance- or extension-function <code>iterator()</code>, whose return type
<ul>
<li>has an instance- or extension-function <code>next()</code>, and</li>
<li>has an instance- or extension-function <code>hasNext()</code> that returns <code>Boolean</code>.</li>
</ul>
</li>
</ul>
<p>If you want to iterate through an array or a list with an index, you can do it this way:</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="k">in</span> <span class="n">array</span><span class="p">.</span><span class="n">indices</span><span class="p">)</span>
<span class="n">print</span><span class="p">(</span><span class="n">array</span><span class="p">[</span><span class="n">i</span><span class="p">])</span></code></pre>
<p>Note that this “iteration through a range” is compiled down to optimal implementation with no extra objects created.</p>
<p>See the <a href="grammar.html#for">grammar for <em class="keyword">for</em></a>.</p>
<h2 id="while-loops">While Loops<a href="#while-loops" class="anchor"></a></h2>
<p><em class="keyword">while</em> and <em class="keyword">do</em>..<em class="keyword">while</em> work as usual</p>
<pre><code class="code-block _highlighted lang_kotlin"><span class="k">while</span> <span class="p">(</span><span class="n">x</span> <span class="p">></span> <span class="m">0</span><span class="p">)</span> <span class="p">{</span>
<span class="n">x</span><span class="p">--</span>
<span class="p">}</span>
<span class="k">do</span> <span class="p">{</span>
<span class="k">val</span> <span class="py">y</span> <span class="p">=</span> <span class="n">retrieveData</span><span class="p">()</span>
<span class="p">}</span> <span class="k">while</span> <span class="p">(</span><span class="n">y</span> <span class="p">!=</span> <span class="k">null</span><span class="p">)</span> <span class="c1">// y is visible here!</span></code></pre>
<p>See the <a href="grammar.html#while">grammar for <em class="keyword">while</em></a>.</p>
<h2 id="break-and-continue-in-loops">Break and continue in loops<a href="#break-and-continue-in-loops" class="anchor"></a></h2>
<p>Kotlin supports traditional <em class="keyword">break</em> and <em class="keyword">continue</em> operators in loops. See <a href="returns.html">Returns and jumps</a>.</p>
</article>
<article role="main" class="page-content g-9">