|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Kotlin Language features\n", |
| 8 | + "\n", |
| 9 | + "## Basic features" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 3, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [ |
| 17 | + { |
| 18 | + "name": "stdout", |
| 19 | + "output_type": "stream", |
| 20 | + "text": [ |
| 21 | + "The sum is 3\n", |
| 22 | + "The globalValue is -> I am a global value\n", |
| 23 | + "small\n", |
| 24 | + "small\n", |
| 25 | + "Français\n", |
| 26 | + "1\n", |
| 27 | + "2\n", |
| 28 | + "3\n", |
| 29 | + "6\n", |
| 30 | + "4\n", |
| 31 | + "2\n", |
| 32 | + "0\n", |
| 33 | + "Hello\n", |
| 34 | + "world\n" |
| 35 | + ] |
| 36 | + } |
| 37 | + ], |
| 38 | + "source": [ |
| 39 | + "// Functions can be declared outside of classes\n", |
| 40 | + "fun sum(x:Int, y:Int) : Int {\n", |
| 41 | + " return x + y\n", |
| 42 | + "} // function definition\n", |
| 43 | + "\n", |
| 44 | + "\n", |
| 45 | + "val globalValue = \"I am a global value\"\n", |
| 46 | + "\n", |
| 47 | + "\n", |
| 48 | + "var a: Int = 0 // variable\n", |
| 49 | + "a = 23\n", |
| 50 | + "\n", |
| 51 | + "val b: String = \"Hello\" // immutable variable\n", |
| 52 | + "//b = \"World\" //error\n", |
| 53 | + "\n", |
| 54 | + "var message = \"Hello\" // Type is inferred\n", |
| 55 | + "\n", |
| 56 | + "val total = sum(2, 1) // call sum function \n", |
| 57 | + "println(\"The sum is ${total}\") // interpolation\n", |
| 58 | + "println(\"The globalValue is -> $globalValue\")\n", |
| 59 | + "\n", |
| 60 | + "val testResult1 = if (total > 3) { \n", |
| 61 | + " \"Big\" \n", |
| 62 | + "} else {\n", |
| 63 | + " \"small\"\n", |
| 64 | + "} \n", |
| 65 | + "println(testResult1)\n", |
| 66 | + "\n", |
| 67 | + "val testResult2 = if (total > 3) \"Big\" else \"small\"\n", |
| 68 | + "println(testResult2)\n", |
| 69 | + "\n", |
| 70 | + "val greeting = \"Bonjour\"\n", |
| 71 | + "val language = when(greeting){\n", |
| 72 | + " \"Bonjour\",\"salut\" -> \"Français\"\n", |
| 73 | + " \"Good morning\" -> \"English\"\n", |
| 74 | + " else -> \"Unknown\"\n", |
| 75 | + "}\n", |
| 76 | + "println(language)\n", |
| 77 | + "\n", |
| 78 | + "for (i in 1..3) println(i) // single line\n", |
| 79 | + "for (i in 6 downTo 0 step 2) println(i)\n", |
| 80 | + "for (msg in arrayOf(\"Hello\", \"world\")) {\n", |
| 81 | + " println(msg)\n", |
| 82 | + "}\n", |
| 83 | + "var x = 2\n", |
| 84 | + "while (x > 0) {\n", |
| 85 | + " x--\n", |
| 86 | + "}" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "metadata": {}, |
| 92 | + "source": [ |
| 93 | + "## Null safety" |
| 94 | + ] |
| 95 | + } |
| 96 | + ], |
| 97 | + "metadata": { |
| 98 | + "kernelspec": { |
| 99 | + "display_name": "Kotlin", |
| 100 | + "language": "kotlin", |
| 101 | + "name": "kotlin" |
| 102 | + }, |
| 103 | + "language_info": { |
| 104 | + "codemirror_mode": "text/x-kotlin", |
| 105 | + "file_extension": ".kt", |
| 106 | + "mimetype": "text/x-kotlin", |
| 107 | + "name": "kotlin", |
| 108 | + "nbconvert_exporter": "", |
| 109 | + "pygments_lexer": "kotlin", |
| 110 | + "version": "1.9.23" |
| 111 | + } |
| 112 | + }, |
| 113 | + "nbformat": 4, |
| 114 | + "nbformat_minor": 2 |
| 115 | +} |
0 commit comments