├── .gitignore ├── .travis.yml ├── CNAME ├── LICENSE.txt ├── README.md ├── android-chrome-192x192.png ├── android-chrome-512x512.png ├── apple-touch-icon.png ├── browserconfig.xml ├── cirru ├── classes.cirru ├── collections.cirru ├── concurrency.cirru ├── delegated-properties.cirru ├── dsl.cirru ├── error-prone.cirru ├── footer.cirru ├── functions.cirru ├── generated │ └── .gitignore ├── header.cirru ├── index.cirru ├── io.cirru ├── scoping-functions.cirru └── spring-framework.cirru ├── classes.html ├── code ├── java │ ├── basic │ │ ├── apply.java │ │ ├── arrays.java │ │ ├── bits-operations.java │ │ ├── cast.java │ │ ├── default-parameters.java │ │ ├── elvis-operator.java │ │ ├── hello-world.java │ │ ├── null-i.java │ │ ├── null-ii.java │ │ ├── operations.java │ │ ├── print.java │ │ ├── strings-i.java │ │ ├── strings-ii.java │ │ ├── switch.java │ │ ├── ternary-operator.java │ │ ├── use.java │ │ ├── variables-i.java │ │ └── variables-ii.java │ ├── classes │ │ ├── classes-00.java │ │ ├── classes-01.java │ │ ├── classes-010.java │ │ ├── classes-011.java │ │ ├── classes-012.java │ │ ├── classes-02.java │ │ ├── classes-03.java │ │ ├── classes-04.java │ │ ├── classes-05.java │ │ ├── classes-06.java │ │ ├── classes-07.java │ │ ├── classes-08.java │ │ └── classes-09.java │ ├── collections │ │ ├── 01_ListOf.java │ │ ├── 02_Filter.java │ │ ├── 03_GroupBy.java │ │ ├── 04_PartitionBy.java │ │ ├── 05_SortBy.java │ │ ├── 06_Fold.java │ │ ├── 07_AssociateBy.java │ │ ├── 08_Zip.java │ │ ├── 09_InfixFunctions.java │ │ ├── 10_CollectingAndThen.java │ │ ├── 11_For.java │ │ ├── 12_Repeat.java │ │ └── 13_Comparators.java │ ├── concurrency │ │ ├── 01.java │ │ ├── 02.java │ │ └── 03.java │ ├── delegated-properties │ │ ├── lazy.java │ │ ├── map.java │ │ └── observable.java │ ├── dsl │ │ ├── 01.java │ │ ├── 02.java │ │ ├── 03.java │ │ ├── 04.java │ │ └── 05.java │ ├── error-prone │ │ └── 01-equals.java │ ├── functions │ │ ├── functions-02.java │ │ ├── functions-03.java │ │ ├── functions-04.java │ │ ├── functions-05.java │ │ ├── functions-07.java │ │ ├── functions-08.java │ │ ├── functions-09.java │ │ └── functions-10.java │ ├── io │ │ └── read-file-to-string.java │ ├── scoping-functions │ │ ├── also.java │ │ ├── apply.java │ │ ├── let-apply.java │ │ ├── let.java │ │ ├── run.java │ │ ├── takeIf.java │ │ └── with.java │ └── spring-framework │ │ └── spring-framework-00.java └── kotlin │ ├── basic │ ├── apply.kt │ ├── arrays.kt │ ├── bits-operations.kt │ ├── cast.kt │ ├── default-parameters.kt │ ├── elvis-operator.kt │ ├── hello-world.kt │ ├── null-i.kt │ ├── null-ii.kt │ ├── operations.kt │ ├── print.kt │ ├── read-file.kt │ ├── strings-i.kt │ ├── strings-ii.kt │ ├── ternary-operator.kt │ ├── use.kt │ ├── variables-i.kt │ ├── variables-ii.kt │ └── when.kt │ ├── classes │ ├── classes-00.kt │ ├── classes-01.kt │ ├── classes-010.kt │ ├── classes-011.kt │ ├── classes-012.kt │ ├── classes-02.kt │ ├── classes-03.kt │ ├── classes-04.kt │ ├── classes-05.kt │ ├── classes-06.kt │ ├── classes-07.kt │ ├── classes-08.kt │ └── classes-09.kt │ ├── collections │ ├── 01_ListOf.kt │ ├── 02_Filter.kt │ ├── 03_GroupBy.kt │ ├── 04_PartitionBy.kt │ ├── 05_SortBy.kt │ ├── 06_Fold.kt │ ├── 07_AssociateBy.kt │ ├── 08_Zip.kt │ ├── 09_InfixFunctions.kt │ ├── 10_CollectingAndThen.kt │ ├── 11_For.kt │ ├── 12_Repeat.kt │ └── 13_Comparators.kt │ ├── concurrency │ ├── 01.kt │ ├── 02.kt │ └── 03.kt │ ├── delegated-properties │ ├── lazy.kt │ ├── map.kt │ └── observable.kt │ ├── dsl │ ├── 01.kt │ ├── 02.kt │ ├── 03.kt │ ├── 04.kt │ └── 05.kt │ ├── error-prone │ └── 01-equals.kt │ ├── functions │ ├── functions-02.kt │ ├── functions-03.kt │ ├── functions-04.kt │ ├── functions-05.kt │ ├── functions-07.kt │ ├── functions-08.kt │ ├── functions-09.kt │ └── functions-10.kt │ ├── io │ └── read-file-to-string.kt │ ├── scoping-functions │ ├── also.kt │ ├── apply.kt │ ├── let-apply.kt │ ├── let.kt │ ├── run.kt │ ├── takeIf.kt │ └── with.kt │ └── spring-framework │ └── spring-framework-00.kt ├── collections.html ├── concurrency.html ├── css ├── bootstrap.min.css ├── highlightjs-night-owl.css └── style.css ├── delegated-properties.html ├── dsl.html ├── end.html ├── error-prone.html ├── favicon-16x16.png ├── favicon-32x32.png ├── favicon.ico ├── functions.html ├── index.html ├── io.html ├── js ├── bootstrap.min.js └── highlight.9.16.2.js ├── make.coffee ├── mstile-150x150.png ├── package-lock.json ├── package.json ├── safari-pinned-tab.svg ├── scoping-functions.html ├── site.webmanifest ├── spring-framework.html ├── start-body.html └── start.html /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | build 3 | .DS_Store 4 | *~ 5 | \#*# 6 | .idea -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: trusty 2 | language: node_js 3 | node_js: 4 | - stable 5 | skip_cleanup: true 6 | stages: 7 | - build 8 | - name: deploy 9 | if: branch = master AND type = push 10 | jobs: 11 | include: 12 | - stage: build 13 | before_install: 14 | - npm install -g npm@latest 15 | - npm install -g node-gyp 16 | - npm install -g coffeescript 17 | install: 18 | - npm install 19 | script: 20 | - ./make.coffee dev 21 | - stage: deploy 22 | script: 23 | - git checkout master 24 | - ./make.coffee dev 25 | - git add . 26 | - > 27 | if [ ! -z "$(git status --untracked-files=no --porcelain)" ]; then 28 | git commit -m "HTML updated"; 29 | git push https://${GITHUB_TOKEN}@github.com/driver733/kotlin-vs-java.git; 30 | else 31 | echo "No changes in generated HTML files"; 32 | fi 33 | -------------------------------------------------------------------------------- /CNAME: -------------------------------------------------------------------------------- 1 | www.kotlinvsjava.com -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Fabio Santana 4 | Copyright (c) 2019 Mikhail Yakushin 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kotlin vs Java 2 | 3 | [![Build Status](https://travis-ci.com/driver733/kotlin-vs-java.svg)](https://travis-ci.com/driver733/kotlin-vs-java) 4 | 5 | [![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=driver733/kotlin-vs-java)](https://dependabot.com) 6 | 7 | [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/driver733/kotlin-vs-java/blob/master/LICENSE.txt) 8 | 9 | Web page: [https://www.kotlinvsjava.com](https://www.kotlinvsjava.com) 10 | 11 | Based on: [fabiomsr/from-java-to-kotlin](https://github.com/fabiomsr/from-java-to-kotlin) 12 | 13 | ## Develop 14 | 15 | 1. Install npm 16 | 2. Install coffeescript 17 | 18 | `npm install -g coffeescript` 19 | 3. Install node-gyp 20 | 21 | `npm install -g node-gyp` 22 | 4. Install dependencies 23 | 24 | `npm install` 25 | 5. Generate HTML 26 | 27 | `./make.coffee dev` 28 | 29 | ## HTML generation 30 | 31 | First, the cirru templates (in `./cirru`) are combined with the header 32 | and footer (`./cirru/header.cirru` + `./cirru/{FILE}.cirru` + `./cirru/footer.cirru`). 33 | Next, the generated cirru templates from the first step (in `./cirru/generated`) 34 | are converted into HTML (in `./`). 35 | 36 | ## CI/CD 37 | 38 | TravisCI automatically regenerates HTML files on each merge commit made to the master branch. 39 | Therefore, HTML files must not be committed manually (e.g. in pull requests). 40 | 41 | ## Adding new code snippets 42 | 43 | The code snippets reside in the `code/java` and `code/kotlin` folders. 44 | They are referenced in cirru (`./cirru`) templates this way: 45 | ``` 46 | .lang Java 47 | pre.code $ code (@insert ../../code/java/dsl/04.java) $ :class java 48 | ``` 49 | 50 | ## How to contribute 51 | 52 | Fork repository, make changes, send a pull request. I will review 53 | your changes and apply them to the `master` branch shortly, provided 54 | they don't violate the quality standards. Before 55 | sending your pull request please check that the HTML is generated correctly: 56 | 57 | ``` 58 | ./make.coffee dev 59 | ``` 60 | 61 | ## Got questions? 62 | 63 | If you have questions or general suggestions, don't hesitate to submit 64 | a new [Github issue](https://github.com/driver733/kotlin-vs-java/issues/new). 65 | -------------------------------------------------------------------------------- /android-chrome-192x192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/android-chrome-192x192.png -------------------------------------------------------------------------------- /android-chrome-512x512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/android-chrome-512x512.png -------------------------------------------------------------------------------- /apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/apple-touch-icon.png -------------------------------------------------------------------------------- /browserconfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | #da532c 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /cirru/classes.cirru: -------------------------------------------------------------------------------- 1 | .section 2 | .list-group-flush 3 | div (:class "list-group-item border-0") 4 | .title "Constructor Call" 5 | .container-fluid 6 | .row 7 | .col 8 | .lang Java 9 | pre.code $ code (@insert ../../code/java/classes/classes-00.java) $ :class java 10 | .col 11 | .lang Kotlin 12 | pre.code $ code (@insert ../../code/kotlin/classes/classes-00.kt) $ :class kotlin 13 | div (:class "list-group-item border-0") 14 | .title "Class" 15 | .container-fluid 16 | .row 17 | .col 18 | .lang Java 19 | pre.code $ code (@insert ../../code/java/classes/classes-01.java) $ :class java 20 | .col 21 | .lang Kotlin 22 | pre.code $ code (@insert ../../code/kotlin/classes/classes-01.kt) $ :class kotlin 23 | div (:class "list-group-item border-0") 24 | .title "Open Class" 25 | .container-fluid 26 | .row 27 | .col 28 | .lang Java 29 | pre.code $ code (@insert ../../code/java/classes/classes-02.java) $ :class java 30 | .col 31 | .lang Kotlin 32 | pre.code $ code (@insert ../../code/kotlin/classes/classes-02.kt) $ :class kotlin 33 | div (:class "list-group-item border-0") 34 | .title "Final Attributes" 35 | .container-fluid 36 | .row 37 | .col 38 | .lang Java 39 | pre.code $ code (@insert ../../code/java/classes/classes-03.java) $ :class java 40 | .col 41 | .lang Kotlin 42 | pre.code $ code (@insert ../../code/kotlin/classes/classes-03.kt) $ :class kotlin 43 | div (:class "list-group-item border-0") 44 | .title "Primary Constructor" 45 | .container-fluid 46 | .row 47 | .col 48 | .lang Java 49 | pre.code $ code (@insert ../../code/java/classes/classes-04.java) $ :class java 50 | .col 51 | .lang Kotlin 52 | pre.code $ code (@insert ../../code/kotlin/classes/classes-04.kt) $ :class kotlin 53 | div (:class "list-group-item border-0") 54 | .title "Optional Arguments in Constructors" 55 | .container-fluid 56 | .row 57 | .col 58 | .lang Java 59 | pre.code $ code (@insert ../../code/java/classes/classes-05.java) $ :class java 60 | .col 61 | .lang Kotlin 62 | pre.code $ code (@insert ../../code/kotlin/classes/classes-05.kt) $ :class kotlin 63 | div (:class "list-group-item border-0") 64 | .title "Properties" 65 | .container-fluid 66 | .row 67 | .col 68 | .lang Java 69 | pre.code $ code (@insert ../../code/java/classes/classes-06.java) $ :class java 70 | .col 71 | .lang Kotlin 72 | pre.code $ code (@insert ../../code/kotlin/classes/classes-06.kt) $ :class kotlin 73 | 74 | .section 75 | .list-group-flush 76 | div (:class "list-group-item border-0") 77 | .title "Abstract Class" 78 | .container-fluid 79 | .row 80 | .col 81 | .lang Java 82 | pre.code $ code (@insert ../../code/java/classes/classes-07.java) $ :class java 83 | .col 84 | .lang Kotlin 85 | pre.code $ code (@insert ../../code/kotlin/classes/classes-07.kt) $ :class kotlin 86 | div (:class "list-group-item border-0") 87 | .title "Singleton" 88 | .container-fluid 89 | .row 90 | .col 91 | .lang Java 92 | pre.code $ code (@insert ../../code/java/classes/classes-08.java) $ :class java 93 | .col 94 | .lang Kotlin 95 | pre.code $ code (@insert ../../code/kotlin/classes/classes-08.kt) $ :class kotlin 96 | div (:class "list-group-item border-0") 97 | .title "Extensions" 98 | .container-fluid 99 | .row 100 | .col 101 | .lang Java 102 | pre.code $ code (@insert ../../code/java/classes/classes-09.java) $ :class java 103 | .col 104 | .lang Kotlin 105 | pre.code $ code (@insert ../../code/kotlin/classes/classes-09.kt) $ :class kotlin 106 | div (:class "list-group-item border-0") 107 | .title "Inner Class" 108 | .container-fluid 109 | .row 110 | .col 111 | .lang Java 112 | pre.code $ code (@insert ../../code/java/classes/classes-010.java) $ :class java 113 | .col 114 | .lang Kotlin 115 | pre.code $ code (@insert ../../code/kotlin/classes/classes-010.kt) $ :class kotlin 116 | div (:class "list-group-item border-0") 117 | .title "Nested Class" 118 | .container-fluid 119 | .row 120 | .col 121 | .lang Java 122 | pre.code $ code (@insert ../../code/java/classes/classes-011.java) $ :class java 123 | .col 124 | .lang Kotlin 125 | pre.code $ code (@insert ../../code/kotlin/classes/classes-011.kt) $ :class kotlin 126 | div (:class "list-group-item border-0") 127 | .title "Interface" 128 | .container-fluid 129 | .row 130 | .col 131 | .lang Java 132 | pre.code $ code (@insert ../../code/java/classes/classes-012.java) $ :class java 133 | .col 134 | .lang Kotlin 135 | pre.code $ code (@insert ../../code/kotlin/classes/classes-012.kt) $ :class kotlin 136 | -------------------------------------------------------------------------------- /cirru/collections.cirru: -------------------------------------------------------------------------------- 1 | .section 2 | .list-group-flush 3 | div (:class "list-group-item border-0") 4 | .title "ListOf" 5 | .container-fluid 6 | .row 7 | .col 8 | .lang Java 9 | pre.code $ code (@insert ../../code/java/collections/01_ListOf.java) $ :class java 10 | .col 11 | .lang Kotlin 12 | pre.code $ code (@insert ../../code/kotlin/collections/01_ListOf.kt) $ :class kotlin 13 | div (:class "list-group-item border-0") 14 | .title "Filter" 15 | .container-fluid 16 | .row 17 | .col 18 | .lang Java 19 | pre.code $ code (@insert ../../code/java/collections/02_Filter.java) $ :class java 20 | .col 21 | .lang Kotlin 22 | pre.code $ code (@insert ../../code/kotlin/collections/02_Filter.kt) $ :class kotlin 23 | div (:class "list-group-item border-0") 24 | .title "GroupBy" 25 | .container-fluid 26 | .row 27 | .col 28 | .lang Java 29 | pre.code $ code (@insert ../../code/java/collections/03_GroupBy.java) $ :class java 30 | .col 31 | .lang Kotlin 32 | pre.code $ code (@insert ../../code/kotlin/collections/03_GroupBy.kt) $ :class kotlin 33 | div (:class "list-group-item border-0") 34 | .title "PartitionBy" 35 | .container-fluid 36 | .row 37 | .col 38 | .lang Java 39 | pre.code $ code (@insert ../../code/java/collections/04_PartitionBy.java) $ :class java 40 | .col 41 | .lang Kotlin 42 | pre.code $ code (@insert ../../code/kotlin/collections/04_PartitionBy.kt) $ :class kotlin 43 | div (:class "list-group-item border-0") 44 | .title "SortBy" 45 | .container-fluid 46 | .row 47 | .col 48 | .lang Java 49 | pre.code $ code (@insert ../../code/java/collections/05_SortBy.java) $ :class java 50 | .col 51 | .lang Kotlin 52 | pre.code $ code (@insert ../../code/kotlin/collections/05_SortBy.kt) $ :class kotlin 53 | div (:class "list-group-item border-0") 54 | .title "Fold" 55 | .container-fluid 56 | .row 57 | .col 58 | .lang Java 59 | pre.code $ code (@insert ../../code/java/collections/06_Fold.java) $ :class java 60 | .col 61 | .lang Kotlin 62 | pre.code $ code (@insert ../../code/kotlin/collections/06_Fold.kt) $ :class kotlin 63 | div (:class "list-group-item border-0") 64 | .title "AssociateBy" 65 | .container-fluid 66 | .row 67 | .col 68 | .lang Java 69 | pre.code $ code (@insert ../../code/java/collections/07_AssociateBy.java) $ :class java 70 | .col 71 | .lang Kotlin 72 | pre.code $ code (@insert ../../code/kotlin/collections/07_AssociateBy.kt) $ :class kotlin 73 | div (:class "list-group-item border-0") 74 | .title "Zip" 75 | .container-fluid 76 | .row 77 | .col 78 | .lang Java 79 | pre.code $ code (@insert ../../code/java/collections/08_Zip.java) $ :class java 80 | .col 81 | .lang Kotlin 82 | pre.code $ code (@insert ../../code/kotlin/collections/08_Zip.kt) $ :class kotlin 83 | div (:class "list-group-item border-0") 84 | .title "Infix functions" 85 | .container-fluid 86 | .row 87 | .col 88 | .lang Java 89 | pre.code $ code (@insert ../../code/java/collections/09_InfixFunctions.java) $ :class java 90 | .col 91 | .lang Kotlin 92 | pre.code $ code (@insert ../../code/kotlin/collections/09_InfixFunctions.kt) $ :class kotlin 93 | div (:class "list-group-item border-0") 94 | .title "CollectingAndThen" 95 | .container-fluid 96 | .row 97 | .col 98 | .lang Java 99 | pre.code $ code (@insert ../../code/java/collections/10_CollectingAndThen.java) $ :class java 100 | .col 101 | .lang Kotlin 102 | pre.code $ code (@insert ../../code/kotlin/collections/10_CollectingAndThen.kt) $ :class kotlin 103 | div (:class "list-group-item border-0") 104 | .title "For" 105 | .container-fluid 106 | .row 107 | .col 108 | .lang Java 109 | pre.code $ code (@insert ../../code/java/collections/11_For.java) $ :class java 110 | .col 111 | .lang Kotlin 112 | pre.code $ code (@insert ../../code/kotlin/collections/11_For.kt) $ :class kotlin 113 | div (:class "list-group-item border-0") 114 | .title "Repeat" 115 | .container-fluid 116 | .row 117 | .col 118 | .lang Java 119 | pre.code $ code (@insert ../../code/java/collections/12_Repeat.java) $ :class java 120 | .col 121 | .lang Kotlin 122 | pre.code $ code (@insert ../../code/kotlin/collections/12_Repeat.kt) $ :class kotlin 123 | div (:class "list-group-item border-0") 124 | .title "Comparators" 125 | .container-fluid 126 | .row 127 | .col 128 | .lang Java 129 | pre.code $ code (@insert ../../code/java/collections/13_Comparators.java) $ :class java 130 | .col 131 | .lang Kotlin 132 | pre.code $ code (@insert ../../code/kotlin/collections/13_Comparators.kt) $ :class kotlin 133 | 134 | -------------------------------------------------------------------------------- /cirru/concurrency.cirru: -------------------------------------------------------------------------------- 1 | .section 2 | .list-group-flush 3 | div (:class "list-group-item border-0") 4 | .title "I" 5 | .container-fluid 6 | .row 7 | .col 8 | .lang Java 9 | pre.code $ code (@insert ../../code/java/concurrency/01.java) $ :class java 10 | .col 11 | .lang Kotlin 12 | pre.code $ code (@insert ../../code/kotlin/concurrency/01.kt) $ :class kotlin 13 | div (:class "list-group-item border-0") 14 | .title "II" 15 | .container-fluid 16 | .row 17 | .col 18 | .lang Java 19 | pre.code $ code (@insert ../../code/java/concurrency/02.java) $ :class java 20 | .col 21 | .lang Kotlin 22 | pre.code $ code (@insert ../../code/kotlin/concurrency/02.kt) $ :class kotlin 23 | div (:class "list-group-item border-0") 24 | .title "III" 25 | .container-fluid 26 | .row 27 | .col 28 | .lang Java 29 | pre.code $ code (@insert ../../code/java/concurrency/03.java) $ :class java 30 | .col 31 | .lang Kotlin 32 | pre.code $ code (@insert ../../code/kotlin/concurrency/03.kt) $ :class kotlin 33 | -------------------------------------------------------------------------------- /cirru/delegated-properties.cirru: -------------------------------------------------------------------------------- 1 | .section 2 | .list-group-flush 3 | div (:class "list-group-item border-0") 4 | .title "Lazy" 5 | .container-fluid 6 | .row 7 | .col 8 | .lang Java 9 | pre.code $ code (@insert ../../code/java/delegated-properties/lazy.java) $ :class java 10 | .col 11 | .lang Kotlin 12 | pre.code $ code (@insert ../../code/kotlin/delegated-properties/lazy.kt) $ :class kotlin 13 | div (:class "list-group-item border-0") 14 | .title "Map" 15 | .container-fluid 16 | .row 17 | .col 18 | .lang Java 19 | pre.code $ code (@insert ../../code/java/delegated-properties/map.java) $ :class java 20 | .col 21 | .lang Kotlin 22 | pre.code $ code (@insert ../../code/kotlin/delegated-properties/map.kt) $ :class kotlin 23 | div (:class "list-group-item border-0") 24 | .title "Observable" 25 | .container-fluid 26 | .row 27 | .col 28 | .lang Java 29 | pre.code $ code (@insert ../../code/java/delegated-properties/observable.java) $ :class java 30 | .col 31 | .lang Kotlin 32 | pre.code $ code (@insert ../../code/kotlin/delegated-properties/observable.kt) $ :class kotlin 33 | -------------------------------------------------------------------------------- /cirru/dsl.cirru: -------------------------------------------------------------------------------- 1 | .section 2 | .list-group-flush 3 | div (:class "list-group-item border-0") 4 | .title "I" 5 | .container-fluid 6 | .row 7 | .col 8 | .lang Java 9 | pre.code $ code (@insert ../../code/java/dsl/01.java) $ :class java 10 | .col 11 | .lang Kotlin 12 | pre.code $ code (@insert ../../code/kotlin/dsl/01.kt) $ :class kotlin 13 | div (:class "list-group-item border-0") 14 | .title "II" 15 | .container-fluid 16 | .row 17 | .col 18 | .lang Java 19 | pre.code $ code (@insert ../../code/java/dsl/02.java) $ :class java 20 | .col 21 | .lang Kotlin 22 | pre.code $ code (@insert ../../code/kotlin/dsl/02.kt) $ :class kotlin 23 | div (:class "list-group-item border-0") 24 | .title "III" 25 | .container-fluid 26 | .row 27 | .col 28 | .lang Java 29 | pre.code $ code (@insert ../../code/java/dsl/03.java) $ :class java 30 | .col 31 | .lang Kotlin 32 | pre.code $ code (@insert ../../code/kotlin/dsl/03.kt) $ :class kotlin 33 | div (:class "list-group-item border-0") 34 | .title "IV" 35 | .container-fluid 36 | .row 37 | .col 38 | .lang Java 39 | pre.code $ code (@insert ../../code/java/dsl/04.java) $ :class java 40 | .col 41 | .lang Kotlin 42 | pre.code $ code (@insert ../../code/kotlin/dsl/04.kt) $ :class kotlin 43 | div (:class "list-group-item border-0") 44 | .title "V" 45 | .container-fluid 46 | .row 47 | .col 48 | .lang Java 49 | pre.code $ code (@insert ../../code/java/dsl/05.java) $ :class java 50 | .col 51 | .lang Kotlin 52 | pre.code $ code (@insert ../../code/kotlin/dsl/05.kt) $ :class kotlin 53 | -------------------------------------------------------------------------------- /cirru/error-prone.cirru: -------------------------------------------------------------------------------- 1 | .section 2 | .list-group-flush 3 | div (:class "list-group-item border-0") 4 | .title "Equals" 5 | .container-fluid 6 | .row 7 | .col 8 | .lang Java 9 | pre.code $ code (@insert ../../code/java/error-prone/01-equals.java) $ :class java 10 | .col 11 | .lang Kotlin 12 | pre.code $ code (@insert ../../code/kotlin/error-prone/01-equals.kt) $ :class kotlin 13 | 14 | -------------------------------------------------------------------------------- /cirru/footer.cirru: -------------------------------------------------------------------------------- 1 | footer 2 | div 3 | :class 4 | = "container text-center" 5 | div (:class "row justify-content-center pt-4") 6 | .col 7 | span 8 | = "Java 13" 9 | div (:class "row justify-content-center") 10 | .col 11 | span 12 | = "Kotlin 1.3.60" 13 | div (:class "row justify-content-center pt-4 pb-2") 14 | .col 15 | span 16 | = "Created by: " 17 | a (:href https://www.driver733.com) 18 | = @driver733 19 | div (:class "row justify-content-center") 20 | .col 21 | a (:href "https://github.com/driver733/kotlin-vs-java") 22 | img (:src "//img.shields.io/github/stars/driver733/kotlin-vs-java.svg?style=social") (:alt "GitHub stars") 23 | div (:class "row justify-content-center pt-4 pb-4") 24 | .col 25 | span 26 | = "Based on: " 27 | a (:href https://github.com/fabiomsr/from-java-to-kotlin) 28 | = fabiomsr/from-java-to-kotlin 29 | @insert ../../end.html 30 | -------------------------------------------------------------------------------- /cirru/functions.cirru: -------------------------------------------------------------------------------- 1 | .section 2 | .list-group-flush 3 | div (:class "list-group-item border-0") 4 | .title "Default and non-null parameters" 5 | .container-fluid 6 | .row 7 | .col 8 | .lang Java 9 | pre.code $ code (@insert ../../code/java/functions/functions-02.java) $ :class java 10 | .col 11 | .lang Kotlin 12 | pre.code $ code (@insert ../../code/kotlin/functions/functions-02.kt) $ :class kotlin 13 | div (:class "list-group-item border-0") 14 | .title "Single-statement functions" 15 | .container-fluid 16 | .row 17 | .col 18 | .lang Java 19 | pre.code $ code (@insert ../../code/java/functions/functions-04.java) $ :class java 20 | .col 21 | .lang Kotlin 22 | pre.code $ code (@insert ../../code/kotlin/functions/functions-04.kt) $ :class kotlin 23 | 24 | .section 25 | .list-group-flush 26 | div (:class "list-group-item border-0") 27 | .title "Vararg" 28 | .container-fluid 29 | .row 30 | .col 31 | .lang Java 32 | pre.code $ code (@insert ../../code/java/functions/functions-05.java) $ :class java 33 | .col 34 | .lang Kotlin 35 | pre.code $ code (@insert ../../code/kotlin/functions/functions-05.kt) $ :class kotlin 36 | div (:class "list-group-item border-0") 37 | .title "Public functions" 38 | .container-fluid 39 | .row 40 | .col 41 | .lang Java 42 | pre.code $ code (@insert ../../code/java/functions/functions-07.java) $ :class java 43 | .col 44 | .lang Kotlin 45 | pre.code $ code (@insert ../../code/kotlin/functions/functions-07.kt) $ :class kotlin 46 | div (:class "list-group-item border-0") 47 | .title "Default parameters" 48 | .container-fluid 49 | .row 50 | .col 51 | .lang Java 52 | pre.code $ code (@insert ../../code/java/functions/functions-08.java) $ :class java 53 | .col 54 | .lang Kotlin 55 | pre.code $ code (@insert ../../code/kotlin/functions/functions-08.kt) $ :class kotlin 56 | div (:class "list-group-item border-0") 57 | .title "Generic methods" 58 | .container-fluid 59 | .row 60 | .col 61 | .lang Java 62 | pre.code $ code (@insert ../../code/java/functions/functions-09.java) $ :class java 63 | .col 64 | .lang Kotlin 65 | pre.code $ code (@insert ../../code/kotlin/functions/functions-09.kt) $ :class kotlin 66 | div (:class "list-group-item border-0") 67 | .title "Data classes" 68 | .container-fluid 69 | .row 70 | .col 71 | .lang Java 72 | pre.code $ code (@insert ../../code/java/functions/functions-10.java) $ :class java 73 | .col 74 | .lang Kotlin 75 | pre.code $ code (@insert ../../code/kotlin/functions/functions-10.kt) $ :class kotlin 76 | -------------------------------------------------------------------------------- /cirru/generated/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore -------------------------------------------------------------------------------- /cirru/header.cirru: -------------------------------------------------------------------------------- 1 | @insert ../../start.html 2 | script (:async) (:src "https://www.googletagmanager.com/gtag/js?id=UA-127781126-2") 3 | script = "window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-127781126-2');" 4 | script (:src "https://code.jquery.com/jquery-3.3.1.slim.min.js") (:integrity "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo") (:crossorigin "anonymous") 5 | script (:src "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js") (:integrity "sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1") (:crossorigin "anonymous") 6 | script (:src "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js") (:integrity "sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM") (:crossorigin "anonymous") 7 | script (:src js/highlight.9.16.2.js) 8 | script (:src js/bootstrap.min.js) 9 | script "hljs.initHighlightingOnLoad();" 10 | link (:rel "stylesheet") (:href "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css") (:integrity "sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T") (:crossorigin "anonymous") 11 | link (:rel stylesheet) (:href css/style.css) 12 | link (:rel stylesheet) (:href css/highlightjs-night-owl.css) 13 | link (:rel apple-touch-icon) (:sizes 180x180) (:href /apple-touch-icon.png) 14 | link (:rel "icon") (:type image/png) (:sizes 32x32) (:href /favicon-32x32.png) 15 | link (:rel "icon") (:type image/png) (:sizes 16x16) (:href /favicon-16x16.png) 16 | link (:rel "manifest") (:href /site.webmanifest) 17 | link (:rel "mask-icon") (:href /safari-pinned-tab.svg) (:color #5bbad5) 18 | title "Kotlin vs Java" 19 | meta (:charset utf-8) 20 | meta (:name viewport) (:content "width=device-width") (:initial-scale 1) 21 | meta (:name msapplication-TileColor) (:content #da532c) 22 | meta (:name theme-color) (:content #ffffff) 23 | 24 | @insert ../../start-body.html 25 | 26 | div (:class "container-fluid text-center header-title") 27 | div (:class "row text-center") 28 | .col 29 | p 30 | = "Kotlin vs Java" 31 | 32 | nav (:class "navbar navbar-dark navbar-expand-lg") 33 | button (:class navbar-toggler) (:type button) (:data-toggle collapse) (:data-target #navbarNav) (:aria-controls navbarNav) (:aria-expanded false) (:aria-label "Toggle navigation") 34 | span (:class navbar-toggler-icon) 35 | div (:class "collapse navbar-collapse") (:id "navbarNav") 36 | ul (:class "navbar-nav") 37 | @if (@ index) 38 | li (:class "nav-item px-2 active") 39 | a (:class nav-link) (:href /index.html) 40 | = Basics 41 | span (:class sr-only) 42 | = (current) 43 | li (:class "nav-item px-2") 44 | a (:class nav-link) (:href /index.html) 45 | = Basics 46 | @if (@ classes) 47 | li (:class "nav-item px-2 active") 48 | a (:class nav-link) (:href /classes.html) 49 | = Classes 50 | span (:class sr-only) 51 | = (current) 52 | li (:class "nav-item px-2") 53 | a (:class nav-link) (:href /classes.html) 54 | = Classes 55 | @if (@ collections) 56 | li (:class "nav-item px-2 active") 57 | a (:class nav-link) (:href /collections.html) 58 | = Collections 59 | span (:class sr-only) 60 | = (current) 61 | li (:class "nav-item px-2") 62 | a (:class nav-link) (:href /collections.html) 63 | = Collections 64 | @if (@ concurrency) 65 | li (:class "nav-item px-2 active") 66 | a (:class nav-link) (:href /concurrency.html) 67 | = Concurrency 68 | span (:class sr-only) 69 | = (current) 70 | li (:class "nav-item px-2") 71 | a (:class nav-link) (:href /concurrency.html) 72 | = Concurrency 73 | @if (@ delegated_properties) 74 | li (:class "nav-item px-2 active") 75 | a (:class nav-link) (:href /delegated-properties.html) 76 | = Delegated Properties 77 | span (:class sr-only) 78 | = (current) 79 | li (:class "nav-item px-2") 80 | a (:class nav-link) (:href /delegated-properties.html) 81 | = Delegated Properties 82 | @if (@ dsl) 83 | li (:class "nav-item px-2 active") 84 | a (:class nav-link) (:href /dsl.html) 85 | = DSL 86 | span (:class sr-only) 87 | = (current) 88 | li (:class "nav-item px-2") 89 | a (:class nav-link) (:href /dsl.html) 90 | = DSL 91 | @if (@ functions) 92 | li (:class "nav-item px-2 active") 93 | a (:class nav-link) (:href /functions.html) 94 | = Functions 95 | span (:class sr-only) 96 | = (current) 97 | li (:class "nav-item px-2") 98 | a (:class nav-link) (:href /functions.html) 99 | = Functions 100 | @if (@ io) 101 | li (:class "nav-item px-2 active") 102 | a (:class nav-link) (:href /io.html) 103 | = "I/O" 104 | span (:class sr-only) 105 | = (current) 106 | li (:class "nav-item px-2") 107 | a (:class nav-link) (:href /io.html) 108 | = "I/O" 109 | @if (@ scoping_functions) 110 | li (:class "nav-item px-2 active") 111 | a (:class nav-link) (:href /scoping-functions.html) 112 | = Scoping Functions 113 | span (:class sr-only) 114 | = (current) 115 | li (:class "nav-item px-2") 116 | a (:class nav-link) (:href /scoping-functions.html) 117 | = Scoping Functions 118 | @if (@ spring_framework) 119 | li (:class "nav-item px-2 active") 120 | a (:class nav-link) (:href /spring-framework.html) 121 | = Spring Framework 122 | span (:class sr-only) 123 | = (current) 124 | li (:class "nav-item px-2") 125 | a (:class nav-link) (:href /spring-framework.html) 126 | = Spring Framework 127 | @if (@ error_prone) 128 | li (:class "nav-item px-2 active") 129 | a (:class nav-link) (:href /error-prone.html) 130 | = Error prone 131 | span (:class sr-only) 132 | = (current) 133 | li (:class "nav-item px-2") 134 | a (:class nav-link) (:href /error-prone.html) 135 | = Error prone 136 | 137 | a 138 | :href 139 | = https://github.com/driver733/kotlin-vs-java 140 | :class 141 | = github-corner 142 | :aria-label 143 | = View source on Github 144 | svg (:width 80) (:height 80) (:viewBox 0 0 250 250) (:style fill:#70B7FD; color:#fff; position: absolute; top: 0; border: 0; right: 0;) (:aria-hidden true) 145 | path (:d "M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z") 146 | path (:d "M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2") (:fill currentColor) (:style transform-origin: 130px 106px;) (:class octo-arm) 147 | path (:d "M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z") (:fill currentColor) (:class octo-body) 148 | style 149 | = ".github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}" 150 | -------------------------------------------------------------------------------- /cirru/index.cirru: -------------------------------------------------------------------------------- 1 | .section 2 | .list-group-flush 3 | div (:class "list-group-item border-0") 4 | .title "Hello world" 5 | .container-fluid 6 | .row 7 | .col 8 | .lang Java 9 | pre.code $ code (@insert ../../code/java/basic/hello-world.java) $ :class java 10 | .col 11 | .lang Kotlin 12 | pre.code $ code (@insert ../../code/kotlin/basic/hello-world.kt) $ :class kotlin 13 | div (:class "list-group-item border-0") 14 | .title "Variables I" 15 | .container-fluid 16 | .row 17 | .col 18 | .lang Java 19 | pre.code $ code (@insert ../../code/java/basic/variables-i.java) $ :class java 20 | .col 21 | .lang Kotlin 22 | pre.code $ code (@insert ../../code/kotlin/basic/variables-i.kt) $ :class kotlin 23 | div (:class "list-group-item border-0") 24 | .title "Variables II" 25 | .container-fluid 26 | .row 27 | .col 28 | .lang Java 29 | pre.code $ code (@insert ../../code/java/basic/variables-ii.java) $ :class java 30 | .col 31 | .lang Kotlin 32 | pre.code $ code (@insert ../../code/kotlin/basic/variables-ii.kt) $ :class kotlin 33 | div (:class "list-group-item border-0") 34 | .title "Null I" 35 | .container-fluid 36 | .row 37 | .col 38 | .lang Java 39 | pre.code $ code (@insert ../../code/java/basic/null-i.java) $ :class java 40 | .col 41 | .lang Kotlin 42 | pre.code $ code (@insert ../../code/kotlin/basic/null-i.kt) $ :class kotlin 43 | div (:class "list-group-item border-0") 44 | .title "Null II" 45 | .container-fluid 46 | .row 47 | .col 48 | .lang Java 49 | pre.code $ code (@insert ../../code/java/basic/null-ii.java) $ :class java 50 | .col 51 | .lang Kotlin 52 | pre.code $ code (@insert ../../code/kotlin/basic/null-ii.kt) $ :class kotlin 53 | div (:class "list-group-item border-0") 54 | .title "Elvis operator" 55 | .container-fluid 56 | .row 57 | .col 58 | .lang Java 59 | pre.code $ code (@insert ../../code/java/basic/elvis-operator.java) $ :class java 60 | .col 61 | .lang Kotlin 62 | pre.code $ code (@insert ../../code/kotlin/basic/elvis-operator.kt) $ :class kotlin 63 | div (:class "list-group-item border-0") 64 | .title "Strings I" 65 | .container-fluid 66 | .row 67 | .col 68 | .lang Java 69 | pre.code $ code (@insert ../../code/java/basic/strings-i.java) $ :class java 70 | .col 71 | .lang Kotlin 72 | pre.code $ code (@insert ../../code/kotlin/basic/strings-i.kt) $ :class kotlin 73 | div (:class "list-group-item border-0") 74 | .title "Strings II" 75 | .container-fluid 76 | .row 77 | .col 78 | .lang Java 79 | pre.code $ code (@insert ../../code/java/basic/strings-ii.java) $ :class java 80 | .col 81 | .lang Kotlin 82 | pre.code $ code (@insert ../../code/kotlin/basic/strings-ii.kt) $ :class kotlin 83 | div (:class "list-group-item border-0") 84 | .title "Ternary operator" 85 | .container-fluid 86 | .row 87 | .col 88 | .lang Java 89 | pre.code $ code (@insert ../../code/java/basic/ternary-operator.java) $ :class java 90 | .col 91 | .lang Kotlin 92 | pre.code $ code (@insert ../../code/kotlin/basic/ternary-operator.kt) $ :class kotlin 93 | div (:class "list-group-item border-0") 94 | .title "Arrays" 95 | .container-fluid 96 | .row 97 | .col 98 | .lang Java 99 | pre.code $ code (@insert ../../code/java/basic/arrays.java) $ :class java 100 | .col 101 | .lang Kotlin 102 | pre.code $ code (@insert ../../code/kotlin/basic/arrays.kt) $ :class kotlin 103 | div (:class "list-group-item border-0") 104 | .title "Use" 105 | .container-fluid 106 | .row 107 | .col 108 | .lang Java 109 | pre.code $ code (@insert ../../code/java/basic/use.java) $ :class java 110 | .col 111 | .lang Kotlin 112 | pre.code $ code (@insert ../../code/kotlin/basic/use.kt) $ :class kotlin 113 | -------------------------------------------------------------------------------- /cirru/io.cirru: -------------------------------------------------------------------------------- 1 | .section 2 | .list-group-flush 3 | div (:class "list-group-item border-0") 4 | .title "Read file contents into a string" 5 | .container-fluid 6 | .row 7 | .col 8 | .lang Java 9 | pre.code $ code (@insert ../../code/java/io/read-file-to-string.java) $ :class java 10 | .col 11 | .lang Kotlin 12 | pre.code $ code (@insert ../../code/kotlin/io/read-file-to-string.kt) $ :class kotlin 13 | -------------------------------------------------------------------------------- /cirru/scoping-functions.cirru: -------------------------------------------------------------------------------- 1 | .section 2 | .list-group-flush 3 | div (:class "list-group-item border-0") 4 | .title "Also" 5 | .container-fluid 6 | .row 7 | .col 8 | .lang Java 9 | pre.code $ code (@insert ../../code/java/scoping-functions/also.java) $ :class java 10 | .col 11 | .lang Kotlin 12 | pre.code $ code (@insert ../../code/kotlin/scoping-functions/also.kt) $ :class kotlin 13 | div (:class "list-group-item border-0") 14 | .title "Apply" 15 | .container-fluid 16 | .row 17 | .col 18 | .lang Java 19 | pre.code $ code (@insert ../../code/java/scoping-functions/apply.java) $ :class java 20 | .col 21 | .lang Kotlin 22 | pre.code $ code (@insert ../../code/kotlin/scoping-functions/apply.kt) $ :class kotlin 23 | div (:class "list-group-item border-0") 24 | .title "Let" 25 | .container-fluid 26 | .row 27 | .col 28 | .lang Java 29 | pre.code $ code (@insert ../../code/java/scoping-functions/let.java) $ :class java 30 | .col 31 | .lang Kotlin 32 | pre.code $ code (@insert ../../code/kotlin/scoping-functions/let.kt) $ :class kotlin 33 | div (:class "list-group-item border-0") 34 | .title "Let and apply" 35 | .container-fluid 36 | .row 37 | .col 38 | .lang Java 39 | pre.code $ code (@insert ../../code/java/scoping-functions/let-apply.java) $ :class java 40 | .col 41 | .lang Kotlin 42 | pre.code $ code (@insert ../../code/kotlin/scoping-functions/let-apply.kt) $ :class kotlin 43 | div (:class "list-group-item border-0") 44 | .title "Run" 45 | .container-fluid 46 | .row 47 | .col 48 | .lang Java 49 | pre.code $ code (@insert ../../code/java/scoping-functions/run.java) $ :class java 50 | .col 51 | .lang Kotlin 52 | pre.code $ code (@insert ../../code/kotlin/scoping-functions/run.kt) $ :class kotlin 53 | div (:class "list-group-item border-0") 54 | .title "TakeIf" 55 | .container-fluid 56 | .row 57 | .col 58 | .lang Java 59 | pre.code $ code (@insert ../../code/java/scoping-functions/takeIf.java) $ :class java 60 | .col 61 | .lang Kotlin 62 | pre.code $ code (@insert ../../code/kotlin/scoping-functions/takeIf.kt) $ :class kotlin 63 | div (:class "list-group-item border-0") 64 | .title "With" 65 | .container-fluid 66 | .row 67 | .col 68 | .lang Java 69 | pre.code $ code (@insert ../../code/java/scoping-functions/with.java) $ :class java 70 | .col 71 | .lang Kotlin 72 | pre.code $ code (@insert ../../code/kotlin/scoping-functions/with.kt) $ :class kotlin 73 | -------------------------------------------------------------------------------- /cirru/spring-framework.cirru: -------------------------------------------------------------------------------- 1 | .section 2 | .list-group-flush 3 | div (:class "list-group-item border-0") 4 | .title "Immutable Configuration properties" 5 | .container-fluid 6 | .row 7 | .col 8 | .lang Java 9 | pre.code $ code (@insert ../../code/java/spring-framework/spring-framework-00.java) $ :class java 10 | .col 11 | .lang Kotlin 12 | pre.code $ code (@insert ../../code/kotlin/spring-framework/spring-framework-00.kt) $ :class kotlin 13 | -------------------------------------------------------------------------------- /classes.html: -------------------------------------------------------------------------------- 1 | 2 | Kotlin vs Java

Kotlin vs Java

Constructor Call
Java
final var file = new File("file.txt");
  3 | 
Kotlin
val file = File("file.txt")
  4 | 
Class
Java
public final class User {
  5 | }
  6 | 
Kotlin
class User
  7 | 
Open Class
Java
public class User {
  8 | }
  9 | 
Kotlin
open class User
 10 | 
Final Attributes
Java
final class User {
 11 |      private final String name;
 12 | 
 13 |      public User(String name) {
 14 |          this.name = name;
 15 |      }
 16 | 
 17 |      public String getName() {
 18 |          return name;
 19 |      }
 20 |  }
 21 | 
Kotlin
class User(val name: String)
 22 | 
Primary Constructor
Java
final class User {
 23 |      private String name;
 24 | 
 25 |      public User(String name) {
 26 |          this.name = name;
 27 |      }
 28 | 
 29 |      public String getName() {
 30 |          return name;
 31 |      }
 32 | 
 33 |      public void setName(String name) {
 34 |          this.name = name;
 35 |      }
 36 |  }
 37 | 
Kotlin
class User(var name: String)
 38 | 
Optional Arguments in Constructors
Java
final class User {
 39 |      private String name;
 40 |      private String lastName;
 41 | 
 42 |      public User(String name) {
 43 |          this(name, "");
 44 |      }
 45 | 
 46 |      public User(String name, String lastName) {
 47 |          this.name = name;
 48 |          this.lastName = lastName;
 49 |      }
 50 | 
 51 |      // And Getters & Setters
 52 |  }
 53 | 
Kotlin
class User(var name: String, var lastName: String = "")
 54 | 
Properties
Java
public class Document {
 55 | 
 56 |     private String id = "00x";
 57 | 
 58 |     public String getId() {
 59 |          return id;
 60 |      }
 61 | 
 62 |     public void setId(String id) {
 63 |          if (id != null && !id.isEmpty()) {
 64 |              this.id = id;
 65 |          }
 66 |      }
 67 | 
 68 | }
 69 | 
Kotlin
class Document{
 70 |     var id : String = "00x"
 71 |         set(value) {
 72 |             if(value.isNotEmpty())  field = value
 73 |         }
 74 | }
 75 | 
Abstract Class
Java
public abstract class Document{
 76 |    public abstract int calculateSize();
 77 | }
 78 | 
 79 | public class Photo extends Document{
 80 |     @Override
 81 |     public int calculateSize() {
 82 | 
 83 |     }
 84 | }
 85 | 
Kotlin
abstract class Document {
 86 |     abstract fun calculateSize(): Int
 87 | }
 88 | 
 89 | class Photo : Document() {
 90 |     override fun calculateSize(): Int {
 91 | 
 92 |     }
 93 | }
 94 | 
Singleton
Java
public class Document {
 95 |    private static final Document INSTANCE = new Document();
 96 | 
 97 |    public static Document getInstance(){
 98 |        return INSTANCE;
 99 |    }
100 | 
101 |  }
102 |  
103 | 
Kotlin
object Document {
104 | 
105 | }
106 | 
Extensions
Java
public class ByteArrayUtils {
107 |       public static String toHexString(byte[] data) {
108 | 
109 |       }
110 |   }
111 | 
112 |   final var dummyData = new byte[10];
113 |   final var hexValue = ByteArrayUtils.toHexString(dummyData);
114 | 
Kotlin
fun ByteArray.toHex() : String {
115 | 
116 | }
117 | 
118 | val dummyData = byteArrayOf()
119 | val hexValue = dummyData.toHex()
120 | 
Inner Class
Java
public class Documment {
121 | 
122 |     class InnerClass {
123 | 
124 |     }
125 | 
126 | }
127 | 
Kotlin
class Document {
128 |     inner class InnerClass
129 | }
130 | 
Nested Class
Java
public class Documment {
131 | 
132 |     public static class InnerClass {
133 | 
134 |     }
135 | 
136 | }
137 | 
Kotlin
class Document {
138 | 
139 |     class InnerClass
140 | 
141 | }
142 | 
Interface
Java
public interface Printable {
143 |     void print();
144 | }
145 | 
146 | public class Document implements Printable {
147 |     @Override
148 |     public void print() {
149 | 
150 |     }
151 | }
152 | 
Kotlin
interface Printable {
153 |     fun print()
154 | }
155 | 
156 | class Document : Printable {
157 |     override fun print() {
158 | 
159 |     }
160 | }
161 | 
162 | -------------------------------------------------------------------------------- /code/java/basic/apply.java: -------------------------------------------------------------------------------- 1 | File file = new File("file.txt"); 2 | 3 | file.setExecutable(true); 4 | file.setReadable(true); 5 | file.setWritable(true); 6 | -------------------------------------------------------------------------------- /code/java/basic/arrays.java: -------------------------------------------------------------------------------- 1 | var size = 1; 2 | 3 | var numbers = new int[size]; 4 | numbers[0] = 1; 5 | 6 | var bigNumbers = new long[size]; 7 | 8 | var strings = new String[size]; 9 | -------------------------------------------------------------------------------- /code/java/basic/bits-operations.java: -------------------------------------------------------------------------------- 1 | final int andResult = a & b; 2 | final int orResult = a | b; 3 | final int xorResult = a ^ b; 4 | final int rightShift = a >> 2; 5 | final int leftShift = a << 2; 6 | -------------------------------------------------------------------------------- /code/java/basic/cast.java: -------------------------------------------------------------------------------- 1 | if (a instanceof String) { 2 | final var result = ((String) a).substring(1); 3 | } 4 | -------------------------------------------------------------------------------- /code/java/basic/default-parameters.java: -------------------------------------------------------------------------------- 1 | public void greeting(String name, String greet) { 2 | System.out.println(greet + " " + greet + "!"); 3 | } 4 | 5 | public void greeting(String name) { 6 | greeting(name, "hello"); 7 | } -------------------------------------------------------------------------------- /code/java/basic/elvis-operator.java: -------------------------------------------------------------------------------- 1 | final var result = Optional.ofNullable(nullableVariable) 2 | .flatMap(v -> Optional.ofNullable(v.someNullableMethodCall())) 3 | .orElseGet(() -> fallbackIfNullMethodCall()) 4 | -------------------------------------------------------------------------------- /code/java/basic/hello-world.java: -------------------------------------------------------------------------------- 1 | public static void main(final String[] args) { 2 | System.out.println("Hello world!"); 3 | } -------------------------------------------------------------------------------- /code/java/basic/null-i.java: -------------------------------------------------------------------------------- 1 | final String name = null; 2 | 3 | String lastName; 4 | lastName = null; 5 | -------------------------------------------------------------------------------- /code/java/basic/null-ii.java: -------------------------------------------------------------------------------- 1 | final Integer length = 2 | bob != null 3 | && bob.department != null 4 | && bob.department.text != null 5 | ? bob.department.text.length() 6 | : null; 7 | -------------------------------------------------------------------------------- /code/java/basic/operations.java: -------------------------------------------------------------------------------- 1 | if (x instanceof Integer) { } 2 | final var text = (String) other; 3 | if (x >= 0 && x <= 10 ) { } 4 | -------------------------------------------------------------------------------- /code/java/basic/print.java: -------------------------------------------------------------------------------- 1 | System.out.print("Hello, World!"); 2 | System.out.println("Hello, World!"); 3 | -------------------------------------------------------------------------------- /code/java/basic/strings-i.java: -------------------------------------------------------------------------------- 1 | final var name = "John"; 2 | final var lastName = "Smith"; 3 | final var text = "My name is: " + name + " " + lastName; 4 | final var otherText = "My name is: " + name.substring(2); 5 | -------------------------------------------------------------------------------- /code/java/basic/strings-ii.java: -------------------------------------------------------------------------------- 1 | final var text = "First Line\n" + 2 | "Second Line\n" + 3 | "Third Line"; 4 | -------------------------------------------------------------------------------- /code/java/basic/switch.java: -------------------------------------------------------------------------------- 1 | final int x = // value; 2 | final String xResult; 3 | 4 | switch (x){ 5 | case 0: 6 | case 11: 7 | xResult = "0 or 11"; 8 | break; 9 | case 1: 10 | case 2: 11 | //... 12 | case 10: 13 | xResult = "from 1 to 10"; 14 | break; 15 | default: 16 | if(x < 12 && x > 14) { 17 | xResult = "not from 12 to 14"; 18 | break; 19 | } 20 | 21 | if(isOdd(x)) { 22 | xResult = "is odd"; 23 | break; 24 | } 25 | 26 | xResult = "otherwise"; 27 | } 28 | 29 | 30 | 31 | final int y = // value; 32 | final String yResult; 33 | 34 | if (isNegative(y)) { 35 | yResult = "is Negative"; 36 | } else if(isZero(y)){ 37 | yResult = "is Zero"; 38 | } else if(isOdd(y)){ 39 | yResult = "is Odd"; 40 | } else { 41 | yResult = "otherwise"; 42 | } 43 | -------------------------------------------------------------------------------- /code/java/basic/ternary-operator.java: -------------------------------------------------------------------------------- 1 | final var text = x > 5 ? "x > 5" : "x <= 5"; 2 | -------------------------------------------------------------------------------- /code/java/basic/use.java: -------------------------------------------------------------------------------- 1 | var file = new File("contents.txt"); 2 | try { 3 | var fileReader = new FileReader(file); 4 | try (var bufferedReader = new BufferedReader(fileReader)) { 5 | // the bufferedReader is closed automatically 6 | } 7 | } catch (IOException ex) { 8 | // handle the exception 9 | } -------------------------------------------------------------------------------- /code/java/basic/variables-i.java: -------------------------------------------------------------------------------- 1 | var x; // compile-time error 2 | final var xx; // compile-time error 3 | final var y = 1; 4 | -------------------------------------------------------------------------------- /code/java/basic/variables-ii.java: -------------------------------------------------------------------------------- 1 | int w; 2 | int z = 2; 3 | z = 3; 4 | w = 1; 5 | -------------------------------------------------------------------------------- /code/java/classes/classes-00.java: -------------------------------------------------------------------------------- 1 | final var file = new File("file.txt"); 2 | -------------------------------------------------------------------------------- /code/java/classes/classes-01.java: -------------------------------------------------------------------------------- 1 | public final class User { 2 | } 3 | -------------------------------------------------------------------------------- /code/java/classes/classes-010.java: -------------------------------------------------------------------------------- 1 | public class Documment { 2 | 3 | class InnerClass { 4 | 5 | } 6 | 7 | } 8 | -------------------------------------------------------------------------------- /code/java/classes/classes-011.java: -------------------------------------------------------------------------------- 1 | public class Documment { 2 | 3 | public static class InnerClass { 4 | 5 | } 6 | 7 | } 8 | -------------------------------------------------------------------------------- /code/java/classes/classes-012.java: -------------------------------------------------------------------------------- 1 | public interface Printable { 2 | void print(); 3 | } 4 | 5 | public class Document implements Printable { 6 | @Override 7 | public void print() { 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /code/java/classes/classes-02.java: -------------------------------------------------------------------------------- 1 | public class User { 2 | } 3 | -------------------------------------------------------------------------------- /code/java/classes/classes-03.java: -------------------------------------------------------------------------------- 1 | final class User { 2 | private final String name; 3 | 4 | public User(String name) { 5 | this.name = name; 6 | } 7 | 8 | public String getName() { 9 | return name; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /code/java/classes/classes-04.java: -------------------------------------------------------------------------------- 1 | final class User { 2 | private String name; 3 | 4 | public User(String name) { 5 | this.name = name; 6 | } 7 | 8 | public String getName() { 9 | return name; 10 | } 11 | 12 | public void setName(String name) { 13 | this.name = name; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /code/java/classes/classes-05.java: -------------------------------------------------------------------------------- 1 | final class User { 2 | private String name; 3 | private String lastName; 4 | 5 | public User(String name) { 6 | this(name, ""); 7 | } 8 | 9 | public User(String name, String lastName) { 10 | this.name = name; 11 | this.lastName = lastName; 12 | } 13 | 14 | // And Getters & Setters 15 | } 16 | -------------------------------------------------------------------------------- /code/java/classes/classes-06.java: -------------------------------------------------------------------------------- 1 | public class Document { 2 | 3 | private String id = "00x"; 4 | 5 | public String getId() { 6 | return id; 7 | } 8 | 9 | public void setId(String id) { 10 | if (id != null && !id.isEmpty()) { 11 | this.id = id; 12 | } 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /code/java/classes/classes-07.java: -------------------------------------------------------------------------------- 1 | public abstract class Document{ 2 | public abstract int calculateSize(); 3 | } 4 | 5 | public class Photo extends Document{ 6 | @Override 7 | public int calculateSize() { 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /code/java/classes/classes-08.java: -------------------------------------------------------------------------------- 1 | public class Document { 2 | private static final Document INSTANCE = new Document(); 3 | 4 | public static Document getInstance(){ 5 | return INSTANCE; 6 | } 7 | 8 | } 9 | 10 | -------------------------------------------------------------------------------- /code/java/classes/classes-09.java: -------------------------------------------------------------------------------- 1 | public class ByteArrayUtils { 2 | public static String toHexString(byte[] data) { 3 | 4 | } 5 | } 6 | 7 | final var dummyData = new byte[10]; 8 | final var hexValue = ByteArrayUtils.toHexString(dummyData); 9 | -------------------------------------------------------------------------------- /code/java/collections/01_ListOf.java: -------------------------------------------------------------------------------- 1 | final var map = Map.of( 2 | 1, "One", 3 | 2, "Two", 4 | 3, "Three" 5 | ); 6 | -------------------------------------------------------------------------------- /code/java/collections/02_Filter.java: -------------------------------------------------------------------------------- 1 | var filtered = numbers 2 | .stream() 3 | .filter(num -> num > 5) 4 | .collect(Collectors.toList()) -------------------------------------------------------------------------------- /code/java/collections/03_GroupBy.java: -------------------------------------------------------------------------------- 1 | final var groups = items.stream() 2 | .collect( 3 | Collectors.groupingBy( 4 | item -> (item % 2) == 0 ? "even" : "odd" 5 | ) 6 | ); 7 | -------------------------------------------------------------------------------- /code/java/collections/04_PartitionBy.java: -------------------------------------------------------------------------------- 1 | final var partitioned = numbers.stream() 2 | .collect( 3 | Collectors.partitioningBy(num -> num % 2 == 0) 4 | ); 5 | 6 | final var evens = partitioned.get(true); 7 | final var odds = partitioned.get(false); 8 | -------------------------------------------------------------------------------- /code/java/collections/05_SortBy.java: -------------------------------------------------------------------------------- 1 | final var users = getUsers(); 2 | users.sort(Comparator.comparing(user -> user.lastname)); 3 | -------------------------------------------------------------------------------- /code/java/collections/06_Fold.java: -------------------------------------------------------------------------------- 1 | private <T> Double calculateTotals(final Grid<T> grid) { 2 | return grid.getRows() 3 | .stream() 4 | .flatMap(e -> e.getColumns().stream()) 5 | .collect(Collectors.groupingBy(Column::getTitle)) 6 | .entrySet() 7 | .stream() 8 | .flatMap(e -> e.getValue().stream()) 9 | .map(Column::getValue) 10 | .reduce( 11 | 0.0, 12 | (accumulator, value) -> 13 | value instanceof Number 14 | ? accumulator + ((Number) (value)).doubleValue() 15 | : accumulator, 16 | Double::sum 17 | ); 18 | 19 | @Data // Lombok 20 | class Grid<T> { 21 | private final List rows; 22 | } 23 | 24 | @Data // Lombok 25 | class Row<T> { 26 | private final List columns; 27 | } 28 | 29 | @Data // Lombok 30 | class Column<T> { 31 | private final String title; 32 | private final T value; 33 | } -------------------------------------------------------------------------------- /code/java/collections/07_AssociateBy.java: -------------------------------------------------------------------------------- 1 | final var map = currencies.stream() 2 | .collect( 3 | Collectors.toMap( 4 | Currency::getCode, 5 | Function.identity() 6 | ) 7 | ); -------------------------------------------------------------------------------- /code/java/collections/08_Zip.java: -------------------------------------------------------------------------------- 1 | final var numbers = List.of(1, 2, 3); 2 | final var names = List.of("Jack", "Michael", "Bob"); 3 | IntStream 4 | .range(0, Math.min(names.size(), ages.size())) 5 | .mapToObj(i -> names.get(i) + ":" + ages.get(i)) 6 | .collect(Collectors.toList()); -------------------------------------------------------------------------------- /code/java/collections/09_InfixFunctions.java: -------------------------------------------------------------------------------- 1 | var list = List.of("a", "b", "c") 2 | list.add("d") 3 | list.remove("d") -------------------------------------------------------------------------------- /code/java/collections/10_CollectingAndThen.java: -------------------------------------------------------------------------------- 1 | final var topPaidEmployee = Map.of( 2 | "Alex", 250, 3 | "Boss", 999, 4 | "Chris", 400 5 | ).entrySet() 6 | .stream() 7 | .filter(employee -> !employee.getKey().equals("Boss")) 8 | .collect( 9 | Collectors.collectingAndThen( 10 | Collectors.maxBy(Comparator.comparing(Map.Entry::getValue)), 11 | employee -> { 12 | if (employee.isPresent()) { 13 | return employee.get().getKey(); 14 | } 15 | throw new IllegalStateException("No employees"); 16 | } 17 | ) 18 | ); 19 | -------------------------------------------------------------------------------- /code/java/collections/11_For.java: -------------------------------------------------------------------------------- 1 | for (int i = 1; i < 11; i++) { } 2 | 3 | for (int i = 1; i < 11; i += 2) { } 4 | 5 | for (String item : collection) { } 6 | 7 | for (var entry: map.entrySet()) { } 8 | -------------------------------------------------------------------------------- /code/java/collections/12_Repeat.java: -------------------------------------------------------------------------------- 1 | for (int i = 0; i < 3; i++) { 2 | System.out.println("Hello") 3 | } -------------------------------------------------------------------------------- /code/java/collections/13_Comparators.java: -------------------------------------------------------------------------------- 1 | var persons = List.of( 2 | new Person("Bob C.", 5), new Person("Alex G.", 10), new Person("Alex G.", 12) 3 | ); 4 | persons.sort( 5 | Comparator 6 | .comparing(Person::getScore, Comparator.reverseOrder()) 7 | .thenComparing((Person::getName), Comparator.naturalOrder()) 8 | ); 9 | // [(Alex G., 12), (Alex G., 10), (Bob C., 5)] 10 | 11 | @Data // Lombok 12 | class Person { 13 | private final String name; 14 | private final Integer score; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /code/java/concurrency/01.java: -------------------------------------------------------------------------------- 1 | public CompletableFuture<SomeOtherResult> loadAndCombine(String name1, String name2){ 2 | loadImageAsync(name1) 3 | .thenAcceptBoth(loadImageAsync(name2)) ((one, two) -> { 4 | combineImages(one, two) 5 | )} 6 | } 7 | -------------------------------------------------------------------------------- /code/java/concurrency/02.java: -------------------------------------------------------------------------------- 1 | public <T> CompletableFuture<T> inTransaction(DatabaseFunction<T> fun) { 2 | return CompletableFuture 3 | .runAsync(() -> this.sendQuery("BEGIN")) 4 | .thenApply(ignored -> fun.execute(this)) 5 | .thenApply(s -> { 6 | this.sendQuery("COMMIT"); 7 | return s; 8 | }).exceptionally(throwable -> { 9 | this.sendQuery("ROLLBACK"); 10 | return null; 11 | }); 12 | } 13 | -------------------------------------------------------------------------------- /code/java/concurrency/03.java: -------------------------------------------------------------------------------- 1 | final var callables = Stream.of("Service A", "Service B", "Service C") 2 | .map(DummyService::new) 3 | .map(service -> (Callable<ContentDuration>) service::getContent) 4 | .collect(Collectors.toList()); 5 | 6 | final var executor = Executors.newWorkStealingPool(); 7 | final var results = executor.invokeAll(callables).stream() 8 | .map( 9 | future -> { 10 | try { 11 | return future.get(); 12 | } catch (InterruptedException | ExecutionException e) { 13 | throw new RuntimeException(e); 14 | } 15 | } 16 | ).collect(Collectors.toList()); 17 | -------------------------------------------------------------------------------- /code/java/delegated-properties/lazy.java: -------------------------------------------------------------------------------- 1 | private String value; 2 | 3 | public synchronized String getValue() { 4 | if (value == null) { 5 | value = "Something"; 6 | } 7 | return value; 8 | } -------------------------------------------------------------------------------- /code/java/delegated-properties/map.java: -------------------------------------------------------------------------------- 1 | final ObjectMapper mapper = new ObjectMapper(); // jackson's objectmapper 2 | final MyPojo pojo = mapper.convertValue(map, MyPojo.class); 3 | 4 | // https://stackoverflow.com/a/16430704/2441104 -------------------------------------------------------------------------------- /code/java/delegated-properties/observable.java: -------------------------------------------------------------------------------- 1 | public class PCLNewsAgency { 2 | private String news; 3 | 4 | private PropertyChangeSupport support; 5 | 6 | public PCLNewsAgency() { 7 | support = new PropertyChangeSupport(this); 8 | } 9 | 10 | public void addPropertyChangeListener(PropertyChangeListener pcl) { 11 | support.addPropertyChangeListener(pcl); 12 | } 13 | 14 | public void removePropertyChangeListener(PropertyChangeListener pcl) { 15 | support.removePropertyChangeListener(pcl); 16 | } 17 | 18 | public void setNews(String value) { 19 | support.firePropertyChange("news", this.news, value); 20 | this.news = value; 21 | } 22 | } -------------------------------------------------------------------------------- /code/java/dsl/01.java: -------------------------------------------------------------------------------- 1 | final var order = Sandwich() 2 | .type("toastie") 3 | .bread("white") 4 | .fillings(List.of("Cheese", "Ham")) 5 | .dressings(List.of("Pepper", "Worcestershire sauce")) 6 | .side("Crisps") 7 | .makeMeASandwich() 8 | 9 | System.out.println(ooSandwich.receipt()) 10 | 11 | // https://www.kotlindays.com/2019/12/02/days-of-kotlin-a-dsl-for-everyone/index.html -------------------------------------------------------------------------------- /code/java/dsl/02.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/code/java/dsl/02.java -------------------------------------------------------------------------------- /code/java/dsl/03.java: -------------------------------------------------------------------------------- 1 | final var act = this; 2 | final var layout = new LinearLayout(act); 3 | layout.setOrientation(LinearLayout.VERTICAL); 4 | final var name = new EditText(act); 5 | final var button = new Button(act); 6 | button.setText("Say Hello"); 7 | button.setOnClickListener( 8 | () -> { 9 | Toast.makeText(act, "Hello, " + name.text + "!", Toast.LENGTH_SHORT) 10 | .show() 11 | } 12 | layout.addView(name); 13 | layout.addView(button); -------------------------------------------------------------------------------- /code/java/dsl/04.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/code/java/dsl/04.java -------------------------------------------------------------------------------- /code/java/dsl/05.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/code/java/dsl/05.java -------------------------------------------------------------------------------- /code/java/error-prone/01-equals.java: -------------------------------------------------------------------------------- 1 | var first = new String("abc"); 2 | var second = new String("abc"); 3 | 4 | var eq = first.equals(second) // true 5 | var refEq = first == second // false 6 | -------------------------------------------------------------------------------- /code/java/functions/functions-02.java: -------------------------------------------------------------------------------- 1 | public void hello(String name) { 2 | if (name == null) { 3 | name = "World"; 4 | } 5 | 6 | System.out.print("Hello, " + name + "!"); 7 | } 8 | -------------------------------------------------------------------------------- /code/java/functions/functions-03.java: -------------------------------------------------------------------------------- 1 | public boolean hasItems() { 2 | return true; 3 | } 4 | -------------------------------------------------------------------------------- /code/java/functions/functions-04.java: -------------------------------------------------------------------------------- 1 | public double cube(double x) { 2 | return x * x * x; 3 | } 4 | -------------------------------------------------------------------------------- /code/java/functions/functions-05.java: -------------------------------------------------------------------------------- 1 | public int sum(int... numbers) { } 2 | -------------------------------------------------------------------------------- /code/java/functions/functions-07.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(final String[] args){ 3 | openFile("file.txt", true); 4 | } 5 | } 6 | 7 | class FileUtils { 8 | public static File openFile(String filename, boolean readOnly) { } 9 | } 10 | -------------------------------------------------------------------------------- /code/java/functions/functions-08.java: -------------------------------------------------------------------------------- 1 | public static void main(String[]args){ 2 | createFile("file.txt"); 3 | 4 | createFile("file.txt", true); 5 | 6 | createFile("file.txt", true, false); 7 | 8 | createExecutableFile("file.txt"); 9 | } 10 | 11 | public static File createFile(String filename) { } 12 | 13 | public static File createFile(String filename, boolean appendDate) { } 14 | 15 | public static File createFile(String filename, boolean appendDate, 16 | boolean executable) { } 17 | 18 | public static File createExecutableFile(String filename) { } 19 | -------------------------------------------------------------------------------- /code/java/functions/functions-09.java: -------------------------------------------------------------------------------- 1 | public void init() { 2 | var moduleInferred = createList("net"); 3 | } 4 | 5 | public <T> List<T> createList(T item) { } 6 | -------------------------------------------------------------------------------- /code/java/functions/functions-10.java: -------------------------------------------------------------------------------- 1 | public static void main(String[]args) { 2 | val book = createBook(); 3 | System.out.println(book); 4 | System.out.println("Title: " + book.title); 5 | } 6 | 7 | public static Book createBook(){ 8 | return new Book("title_01", "author_01"); 9 | } 10 | 11 | @Data // Lombok 12 | public class Book { 13 | private final String title; 14 | private final String author; 15 | } 16 | -------------------------------------------------------------------------------- /code/java/io/read-file-to-string.java: -------------------------------------------------------------------------------- 1 | String content = null; 2 | try { 3 | content = Files.readString(filePath); 4 | } catch (IOException e) { 5 | e.printStackTrace(); 6 | } 7 | -------------------------------------------------------------------------------- /code/java/scoping-functions/also.java: -------------------------------------------------------------------------------- 1 | final var original = "abc" 2 | 3 | System.out.println(original); 4 | System.out.println(new StringBuilder(input).reverse().toString()); 5 | System.out.println(original.length()); -------------------------------------------------------------------------------- /code/java/scoping-functions/apply.java: -------------------------------------------------------------------------------- 1 | public MyFragment createInstance(Bundle args) { 2 | final var fragment = new MyFragment(); 3 | fragment.setArguments(args); 4 | return fragment; 5 | } 6 | -------------------------------------------------------------------------------- /code/java/scoping-functions/let-apply.java: -------------------------------------------------------------------------------- 1 | public File makeDir(String path) { 2 | final var result = new File(path); 3 | result.mkdirs(); 4 | return result; 5 | } -------------------------------------------------------------------------------- /code/java/scoping-functions/let.java: -------------------------------------------------------------------------------- 1 | final var original = "abc" 2 | // Map the value and send to the next chain 3 | 4 | Optional.of(original) 5 | .map( 6 | str -> { 7 | System.out.println("The original String is " + str); // "abc" 8 | val result = new StringBuilder(input).reverse().toString(); 9 | return result; 10 | } 11 | ).map( 12 | reversed -> { 13 | System.out.println("The reverse String is " + reversed); // "cba" 14 | val result = reversed.length(); 15 | return result; 16 | } 17 | ).map( 18 | length -> { 19 | System.out.println("The length of the String is " + length); // 3 20 | return length; 21 | } 22 | ) 23 | -------------------------------------------------------------------------------- /code/java/scoping-functions/run.java: -------------------------------------------------------------------------------- 1 | if (webview.settings != null) { 2 | final var settings = webview.settings; 3 | settings.javaScriptEnabled = true; 4 | settings.databaseEnabled = true; 5 | } -------------------------------------------------------------------------------- /code/java/scoping-functions/takeIf.java: -------------------------------------------------------------------------------- 1 | public void displaySubstringPosition(final String input, final String sub) { 2 | final var index = input.indexOf(sub); 3 | if (index >= 0) { 4 | System.out.println("The substring " + sub + "is found in " + input + "."); 5 | System.out.println("Its start position is " + index + "."); 6 | } 7 | } -------------------------------------------------------------------------------- /code/java/scoping-functions/with.java: -------------------------------------------------------------------------------- 1 | val numbers = List.of("one", "two", "three"); 2 | with(numbers) { 3 | final var firstItem = numbers.get(0); 4 | final var lastItem = numbers.get(numbers.size() - 1); 5 | System.out.println("First item: " + firstItem + ", last item: " + lastItem); 6 | } -------------------------------------------------------------------------------- /code/java/spring-framework/spring-framework-00.java: -------------------------------------------------------------------------------- 1 | @ImmutableConfigurationProperties(prefix = "dependency.updates") 2 | @Validated 3 | @RequiredArgsConstructor // Lombok 4 | @Data // Lombok 5 | public class AppProperties { 6 | private final List<Location> location; 7 | private final Git git; 8 | private final Integer limit; 9 | @NotBlank 10 | private final String cron; 11 | @NotBlank 12 | private final String priority; 13 | @NotBlank 14 | private final String dirPath; 15 | } 16 | 17 | @Validated 18 | @Data // Lombok 19 | public class Location { 20 | @NotBlank 21 | private final String project; 22 | @NotBlank 23 | private final String repository; 24 | } 25 | 26 | @Validated 27 | @Data // Lombok 28 | public class Git { 29 | @NotBlank 30 | private final String domain; 31 | private final Credentials credentials; 32 | } 33 | 34 | @Validated 35 | @Data // Lombok 36 | public class Credentials { 37 | @NotBlank 38 | private final String username; 39 | @NotBlank 40 | private final String password; 41 | } -------------------------------------------------------------------------------- /code/kotlin/basic/apply.kt: -------------------------------------------------------------------------------- 1 | val file = File("file.txt").apply { 2 | setExecutable(true) 3 | setReadable(true) 4 | setWritable(true) 5 | } 6 | -------------------------------------------------------------------------------- /code/kotlin/basic/arrays.kt: -------------------------------------------------------------------------------- 1 | val size = 1 2 | 3 | val numbers = IntArray(size) 4 | numbers[0] = 1 5 | 6 | val bigNumbers = LongArray(size) 7 | 8 | val strings = Array(size) { String() } 9 | -------------------------------------------------------------------------------- /code/kotlin/basic/bits-operations.kt: -------------------------------------------------------------------------------- 1 | val andResult = a and b 2 | val orResult = a or b 3 | val xorResult = a xor b 4 | val rightShift = a shr 2 5 | val leftShift = a shl 2 6 | -------------------------------------------------------------------------------- /code/kotlin/basic/cast.kt: -------------------------------------------------------------------------------- 1 | if (a is String) { 2 | val result = a.substring(1) 3 | } 4 | -------------------------------------------------------------------------------- /code/kotlin/basic/default-parameters.kt: -------------------------------------------------------------------------------- 1 | fun greeting(name: String, greet: String = "Hello") { 2 | println("$greet $name!") 3 | } -------------------------------------------------------------------------------- /code/kotlin/basic/elvis-operator.kt: -------------------------------------------------------------------------------- 1 | val result = nullableVariable?.someNullableMethodCall() ?: fallbackIfNullMethodCall() -------------------------------------------------------------------------------- /code/kotlin/basic/hello-world.kt: -------------------------------------------------------------------------------- 1 | fun main() { 2 | println("Hello world!") 3 | } 4 | 5 | fun main(args: Array<String>) { 6 | println("Hello World!") 7 | } -------------------------------------------------------------------------------- /code/kotlin/basic/null-i.kt: -------------------------------------------------------------------------------- 1 | val name: String? = null 2 | 3 | var lastName: String? 4 | lastName = null 5 | 6 | var firstName: String 7 | firstName = null // Compilation error!! 8 | -------------------------------------------------------------------------------- /code/kotlin/basic/null-ii.kt: -------------------------------------------------------------------------------- 1 | val length = bob?.department?.text?.length 2 | -------------------------------------------------------------------------------- /code/kotlin/basic/operations.kt: -------------------------------------------------------------------------------- 1 | if (x is Int) { } 2 | 3 | val text = other as String 4 | 5 | if (x in 0..10) { } 6 | -------------------------------------------------------------------------------- /code/kotlin/basic/print.kt: -------------------------------------------------------------------------------- 1 | print("Hello, World!") 2 | println("Hello, World!") 3 | -------------------------------------------------------------------------------- /code/kotlin/basic/read-file.kt: -------------------------------------------------------------------------------- 1 | val text = File("something.txt").readText() -------------------------------------------------------------------------------- /code/kotlin/basic/strings-i.kt: -------------------------------------------------------------------------------- 1 | val name = "John" 2 | val lastName = "Smith" 3 | val text = "My name is: $name $lastName" 4 | val otherText = "My name is: ${name.substring(2)}" 5 | -------------------------------------------------------------------------------- /code/kotlin/basic/strings-ii.kt: -------------------------------------------------------------------------------- 1 | val text = """ 2 | |First Line 3 | |Second Line 4 | |Third Line 5 | """.trimMargin() 6 | -------------------------------------------------------------------------------- /code/kotlin/basic/ternary-operator.kt: -------------------------------------------------------------------------------- 1 | val text = if (x > 5) "x > 5" else "x <= 5" 2 | -------------------------------------------------------------------------------- /code/kotlin/basic/use.kt: -------------------------------------------------------------------------------- 1 | File("contents.txt") 2 | .bufferedReader() 3 | .use { 4 | // the bufferedReader is closed automatically 5 | } -------------------------------------------------------------------------------- /code/kotlin/basic/variables-i.kt: -------------------------------------------------------------------------------- 1 | val x: Int // compile-time error 2 | val y // compile-time error 3 | val z = 2 4 | -------------------------------------------------------------------------------- /code/kotlin/basic/variables-ii.kt: -------------------------------------------------------------------------------- 1 | var q // compile-time error 2 | var w: Int // compile-time error 3 | var z = 2 4 | -------------------------------------------------------------------------------- /code/kotlin/basic/when.kt: -------------------------------------------------------------------------------- 1 | val x = // value 2 | val xResult = when (x) { 3 | 0, 11 -> "0 or 11" 4 | in 1..10 -> "from 1 to 10" 5 | !in 12..14 -> "not from 12 to 14" 6 | else -> if (isOdd(x)) { "is odd" } else { "otherwise" } 7 | } 8 | 9 | val y = // value 10 | val yResult = when { 11 | isNegative(y) -> "is Negative" 12 | isZero(y) -> "is Zero" 13 | isOdd(y) -> "is odd" 14 | else -> "otherwise" 15 | } 16 | -------------------------------------------------------------------------------- /code/kotlin/classes/classes-00.kt: -------------------------------------------------------------------------------- 1 | val file = File("file.txt") 2 | -------------------------------------------------------------------------------- /code/kotlin/classes/classes-01.kt: -------------------------------------------------------------------------------- 1 | class User 2 | -------------------------------------------------------------------------------- /code/kotlin/classes/classes-010.kt: -------------------------------------------------------------------------------- 1 | class Document { 2 | inner class InnerClass 3 | } 4 | -------------------------------------------------------------------------------- /code/kotlin/classes/classes-011.kt: -------------------------------------------------------------------------------- 1 | class Document { 2 | 3 | class InnerClass 4 | 5 | } 6 | -------------------------------------------------------------------------------- /code/kotlin/classes/classes-012.kt: -------------------------------------------------------------------------------- 1 | interface Printable { 2 | fun print() 3 | } 4 | 5 | class Document : Printable { 6 | override fun print() { 7 | 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /code/kotlin/classes/classes-02.kt: -------------------------------------------------------------------------------- 1 | open class User 2 | -------------------------------------------------------------------------------- /code/kotlin/classes/classes-03.kt: -------------------------------------------------------------------------------- 1 | class User(val name: String) 2 | -------------------------------------------------------------------------------- /code/kotlin/classes/classes-04.kt: -------------------------------------------------------------------------------- 1 | class User(var name: String) 2 | -------------------------------------------------------------------------------- /code/kotlin/classes/classes-05.kt: -------------------------------------------------------------------------------- 1 | class User(var name: String, var lastName: String = "") 2 | -------------------------------------------------------------------------------- /code/kotlin/classes/classes-06.kt: -------------------------------------------------------------------------------- 1 | class Document{ 2 | var id : String = "00x" 3 | set(value) { 4 | if(value.isNotEmpty()) field = value 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /code/kotlin/classes/classes-07.kt: -------------------------------------------------------------------------------- 1 | abstract class Document { 2 | abstract fun calculateSize(): Int 3 | } 4 | 5 | class Photo : Document() { 6 | override fun calculateSize(): Int { 7 | 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /code/kotlin/classes/classes-08.kt: -------------------------------------------------------------------------------- 1 | object Document { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /code/kotlin/classes/classes-09.kt: -------------------------------------------------------------------------------- 1 | fun ByteArray.toHex() : String { 2 | 3 | } 4 | 5 | val dummyData = byteArrayOf() 6 | val hexValue = dummyData.toHex() 7 | -------------------------------------------------------------------------------- /code/kotlin/collections/01_ListOf.kt: -------------------------------------------------------------------------------- 1 | val numbers = listOf(1, 2, 3) 2 | 3 | val map = mapOf(1 to "One", 4 | 2 to "Two", 5 | 3 to "Three") 6 | -------------------------------------------------------------------------------- /code/kotlin/collections/02_Filter.kt: -------------------------------------------------------------------------------- 1 | val filtered = numbers.filter { it > 5 } 2 | -------------------------------------------------------------------------------- /code/kotlin/collections/03_GroupBy.kt: -------------------------------------------------------------------------------- 1 | val groups = numbers.groupBy { 2 | if (it % 2 == 0) "even" else "odd" 3 | } 4 | -------------------------------------------------------------------------------- /code/kotlin/collections/04_PartitionBy.kt: -------------------------------------------------------------------------------- 1 | val (evens, odds) = numbers.partition { it % 2 == 0 } 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /code/kotlin/collections/05_SortBy.kt: -------------------------------------------------------------------------------- 1 | val users = getUsers() 2 | users.sortBy { it.lastname } 3 | -------------------------------------------------------------------------------- /code/kotlin/collections/06_Fold.kt: -------------------------------------------------------------------------------- 1 | fun <T> calculateTotals(grid: Grid<T>) = 2 | grid.rows 3 | .flatMap(Row<T>::columns) 4 | .groupingBy(Column<T>::title) 5 | .fold(0.0) { 6 | accumulator, 7 | (_, value) -> 8 | accumulator + 9 | when (value) { 10 | is Number -> value.toDouble() 11 | else -> 0.0 12 | } 13 | } 14 | 15 | data class Grid<T>(val rows: List) 16 | data class Row<T>(val columns: List) 17 | data class Column<T>(val title: String, val value: T) 18 | 19 | // https://kotlinexpertise.com/kotlin-productivity/ -------------------------------------------------------------------------------- /code/kotlin/collections/07_AssociateBy.kt: -------------------------------------------------------------------------------- 1 | val map = currencies.associateBy { it.code } -------------------------------------------------------------------------------- /code/kotlin/collections/08_Zip.kt: -------------------------------------------------------------------------------- 1 | val pairs = listOf(1, 2, 3).zip(listOf("one", "two", "three")) // List> -------------------------------------------------------------------------------- /code/kotlin/collections/09_InfixFunctions.kt: -------------------------------------------------------------------------------- 1 | val list = mutableListOf("a", "b", "c") 2 | list += "d" 3 | list -= "d" -------------------------------------------------------------------------------- /code/kotlin/collections/10_CollectingAndThen.kt: -------------------------------------------------------------------------------- 1 | val topPaidEmployee = listOf("Alex" to 250, "Boss" to 999, "Chris" to 400) 2 | .filter { it.first != "Boss" } 3 | .maxBy { it.second } 4 | ?.first 5 | ?: throw IllegalStateException("No employees") -------------------------------------------------------------------------------- /code/kotlin/collections/11_For.kt: -------------------------------------------------------------------------------- 1 | for (i in 1 until 11) { } 2 | 3 | for (i in 1..10 step 2) {} 4 | 5 | for (item in collection) {} 6 | for ((index, item) in collection.withIndex()) {} 7 | 8 | for ((key, value) in map) {} 9 | -------------------------------------------------------------------------------- /code/kotlin/collections/12_Repeat.kt: -------------------------------------------------------------------------------- 1 | repeat(3) { 2 | println("Hello") 3 | } -------------------------------------------------------------------------------- /code/kotlin/collections/13_Comparators.kt: -------------------------------------------------------------------------------- 1 | val personsSorted = listOf( 2 | Pair("Bob C.", 5), Pair("Alex G.", 10), Pair("Alex G.", 12) 3 | ).sortedWith( 4 | compareByDescending<Pair<String, Int>> { it.second } 5 | .thenBy { it. first } 6 | ) -------------------------------------------------------------------------------- /code/kotlin/concurrency/01.kt: -------------------------------------------------------------------------------- 1 | suspend fun loadAndCombine(name1: String, name2: String): Image = 2 | coroutineScope { 3 | val deferred1 = async { loadImage(name1) } 4 | val deferred2 = async { loadImage(name2) } 5 | combineImages(deferred1.await(), deferred2.await()) 6 | } -------------------------------------------------------------------------------- /code/kotlin/concurrency/02.kt: -------------------------------------------------------------------------------- 1 | suspend fun <T> inTransaction( 2 | f: suspend (Connection) -> T): T { 3 | try { 4 | this.sendQuery("BEGIN") 5 | val result = f(this) 6 | this.sendQuery("COMMIT") 7 | return result 8 | } catch (e: Throwable) { 9 | this.sendQuery("ROLLBACK") 10 | throw e 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /code/kotlin/concurrency/03.kt: -------------------------------------------------------------------------------- 1 | val DummyService.asyncContent: Deferred<ContentDuration> 2 | get() = async(CommonPool) { content } 3 | 4 | var results = runBlocking { 5 | arrayOf("Service A", "Service B", "Service C") 6 | .map { DummyService(it) } 7 | .map { it.asyncContent } 8 | .map { it.await() } 9 | } 10 | -------------------------------------------------------------------------------- /code/kotlin/delegated-properties/lazy.kt: -------------------------------------------------------------------------------- 1 | val lazyValue: String by lazy { 2 | "Something" 3 | } -------------------------------------------------------------------------------- /code/kotlin/delegated-properties/map.kt: -------------------------------------------------------------------------------- 1 | class User(val map: Map<String, Any?>) { 2 | val name: String by map 3 | val age: Int by map 4 | } -------------------------------------------------------------------------------- /code/kotlin/delegated-properties/observable.kt: -------------------------------------------------------------------------------- 1 | class User { 2 | var name: String by Delegates.observable("") { 3 | prop, old, new -> 4 | println("$old -> $new") 5 | } 6 | } -------------------------------------------------------------------------------- /code/kotlin/dsl/01.kt: -------------------------------------------------------------------------------- 1 | val dslSandwich = sandwich { 2 | with type "toasted" 3 | bread = "baguette" 4 | 5 | filling("cheese") 6 | filling("ham") 7 | filling("tomato") 8 | 9 | dressings { 10 | +"Basil" 11 | +"Pepper" 12 | } 13 | 14 | sideOrders { 15 | side("French Fries") 16 | } 17 | 18 | } 19 | 20 | println(dslSandwich.receipt()) 21 | 22 | // https://www.kotlindays.com/2019/12/02/days-of-kotlin-a-dsl-for-everyone/index.html -------------------------------------------------------------------------------- /code/kotlin/dsl/02.kt: -------------------------------------------------------------------------------- 1 | val result = html { 2 | head { 3 | title { +"HTML encoding with Kotlin" } 4 | } 5 | body { 6 | h1 { +"HTML encoding with Kotlin" } 7 | p { 8 | +"this format can be used as an" 9 | +"alternative markup to HTML" 10 | } 11 | 12 | // an element with attributes and text content 13 | a(href = "http://jetbrains.com/kotlin") { +"Kotlin" } 14 | 15 | // mixed content 16 | p { 17 | +"This is some" 18 | b { +"mixed" } 19 | +"text. For more see the" 20 | a(href = "http://jetbrains.com/kotlin") { 21 | +"Kotlin" 22 | } 23 | +"project" 24 | } 25 | p { 26 | +"some text" 27 | ul { 28 | for (i in 1..5) 29 | li { +"${i}*2 = ${i*2}" } 30 | } 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /code/kotlin/dsl/03.kt: -------------------------------------------------------------------------------- 1 | verticalLayout { 2 | val name = editText() 3 | button("Say Hello") { 4 | onClick { toast("Hello, ${name.text}!") } 5 | } 6 | } -------------------------------------------------------------------------------- /code/kotlin/dsl/04.kt: -------------------------------------------------------------------------------- 1 | override fun onCreate(savedInstanceState: Bundle?) { 2 | super.onCreate(savedInstanceState) 3 | 4 | verticalLayout { 5 | padding = dip(30) 6 | editText { 7 | hint = "Name" 8 | textSize = 24f 9 | } 10 | editText { 11 | hint = "Password" 12 | textSize = 24f 13 | } 14 | button("Login") { 15 | textSize = 26f 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /code/kotlin/dsl/05.kt: -------------------------------------------------------------------------------- 1 | application.install(Routing) { 2 | get("/") { 3 | call.respondText("Hello, World!") 4 | } 5 | get("/bye") { 6 | call.respondText("Good bye, World!") 7 | } 8 | } -------------------------------------------------------------------------------- /code/kotlin/error-prone/01-equals.kt: -------------------------------------------------------------------------------- 1 | var first = String() + "abc" 2 | var second = String() + "abc" 3 | 4 | var eq = first.equals(second) // true 5 | var refEq = first === second // false -------------------------------------------------------------------------------- /code/kotlin/functions/functions-02.kt: -------------------------------------------------------------------------------- 1 | fun hello(name: String = "World") { 2 | println("Hello, $name!") 3 | } 4 | -------------------------------------------------------------------------------- /code/kotlin/functions/functions-03.kt: -------------------------------------------------------------------------------- 1 | fun hasItems() = true 2 | -------------------------------------------------------------------------------- /code/kotlin/functions/functions-04.kt: -------------------------------------------------------------------------------- 1 | fun cube(x: Double) : Double = x * x * x 2 | -------------------------------------------------------------------------------- /code/kotlin/functions/functions-05.kt: -------------------------------------------------------------------------------- 1 | fun sum(vararg x: Int) { } 2 | -------------------------------------------------------------------------------- /code/kotlin/functions/functions-07.kt: -------------------------------------------------------------------------------- 1 | fun main() { 2 | openFile("file.txt", true) 3 | } 4 | 5 | fun openFile(filename: String, readOnly: Boolean) : File { } 6 | -------------------------------------------------------------------------------- /code/kotlin/functions/functions-08.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array<String>) { 2 | createFile("file.txt") 3 | 4 | createFile("file.txt", true) 5 | createFile("file.txt", appendDate = true) 6 | 7 | createFile("file.txt", true, false) 8 | createFile("file.txt", appendDate = true, executable = true) 9 | 10 | createFile("file.txt", executable = true) 11 | } 12 | 13 | fun createFile(filename: String, appendDate: Boolean = false, 14 | executable: Boolean = false): File { } 15 | -------------------------------------------------------------------------------- /code/kotlin/functions/functions-09.kt: -------------------------------------------------------------------------------- 1 | fun init() { 2 | val module = createList<String>("net") 3 | val moduleInferred = createList("net") 4 | } 5 | 6 | fun <T> createList(item: T): List<T> { } 7 | -------------------------------------------------------------------------------- /code/kotlin/functions/functions-10.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array<String>) { 2 | val book = createBook(); 3 | // or 4 | val (title, author) = createBook() 5 | 6 | println(book) 7 | println("Title: $title") 8 | } 9 | 10 | fun createBook() : Book{ 11 | return Book("title_01", "author_01") 12 | } 13 | 14 | data class Book(val title: String, val author: String) 15 | -------------------------------------------------------------------------------- /code/kotlin/io/read-file-to-string.kt: -------------------------------------------------------------------------------- 1 | File("doc.txt").readText() -------------------------------------------------------------------------------- /code/kotlin/scoping-functions/also.kt: -------------------------------------------------------------------------------- 1 | val original = "abc" 2 | 3 | original.also { 4 | println("The original String is $it") // "abc" 5 | }.also { 6 | println("The reverse String is ${it.reversed()}") // "cba" 7 | }.also { 8 | println("The length of the String is ${it.length}") // 3 9 | } -------------------------------------------------------------------------------- /code/kotlin/scoping-functions/apply.kt: -------------------------------------------------------------------------------- 1 | fun createInstance(args: Bundle) = MyFragment().apply { arguments = args } -------------------------------------------------------------------------------- /code/kotlin/scoping-functions/let-apply.kt: -------------------------------------------------------------------------------- 1 | fun makeDir(path: String) = 2 | path.let { File(it) } 3 | .also { it.mkdirs() } -------------------------------------------------------------------------------- /code/kotlin/scoping-functions/let.kt: -------------------------------------------------------------------------------- 1 | val original = "abc" 2 | // Map the value and send to the next chain 3 | original.let { 4 | println("The original String is $it") // "abc" 5 | it.reversed() // map it as parameter to send to next let 6 | }.let { 7 | println("The reverse String is $it") // "cba" 8 | it.length // can be evolved to other type 9 | }.let { 10 | println("The length of the String is $it") // 3 11 | } -------------------------------------------------------------------------------- /code/kotlin/scoping-functions/run.kt: -------------------------------------------------------------------------------- 1 | webview.settings?.run { 2 | javaScriptEnabled = true 3 | databaseEnabled = true 4 | } -------------------------------------------------------------------------------- /code/kotlin/scoping-functions/takeIf.kt: -------------------------------------------------------------------------------- 1 | fun displaySubstringPosition(input: String, sub: String) { 2 | input.indexOf(sub).takeIf { it >= 0 }?.let { 3 | println("The substring $sub is found in $input.") 4 | println("Its start position is $it.") 5 | } 6 | } -------------------------------------------------------------------------------- /code/kotlin/scoping-functions/with.kt: -------------------------------------------------------------------------------- 1 | val numbers = mutableListOf("one", "two", "three") 2 | with(numbers) { 3 | val firstItem = first() 4 | val lastItem = last() 5 | println("First item: $firstItem, last item: $lastItem") 6 | } -------------------------------------------------------------------------------- /code/kotlin/spring-framework/spring-framework-00.kt: -------------------------------------------------------------------------------- 1 | @ImmutableConfigurationProperties(prefix = "dependency.updates") 2 | @Validated 3 | data class AppProperties( 4 | val locations: List<Location>, 5 | val git: Git, 6 | val limit: Int, 7 | @field:NotBlank 8 | val cron: String, 9 | @field:NotBlank 10 | val priority: String, 11 | @field:NotBlank 12 | val dirPath: String 13 | ) 14 | 15 | @Validated 16 | data class Location( 17 | @field:NotBlank 18 | val project: String, 19 | @field:NotBlank 20 | val repository: String 21 | ) 22 | 23 | @Validated 24 | data class Git( 25 | @field:NotBlank 26 | val domain: String, 27 | val credentials: Credentials 28 | ) 29 | 30 | @Validated 31 | data class Credentials( 32 | @field:NotBlank 33 | val username: String, 34 | @field:NotBlank 35 | val password: String 36 | ) -------------------------------------------------------------------------------- /concurrency.html: -------------------------------------------------------------------------------- 1 | 2 | Kotlin vs Java

Kotlin vs Java

I
Java
public CompletableFuture<SomeOtherResult> loadAndCombine(String name1, String name2){
 3 |         loadImageAsync(name1)
 4 |             .thenAcceptBoth(loadImageAsync(name2)) ((one, two) -> {
 5 |                 combineImages(one, two)
 6 |         )}
 7 | }
 8 | 
Kotlin
suspend fun loadAndCombine(name1: String, name2: String): Image =
 9 |         coroutineScope {
10 |             val deferred1 = async { loadImage(name1) }
11 |             val deferred2 = async { loadImage(name2) }
12 |             combineImages(deferred1.await(), deferred2.await())
13 |         }
II
Java
public <T> CompletableFuture<T> inTransaction(DatabaseFunction<T> fun) {
14 |     return CompletableFuture
15 |         .runAsync(() -> this.sendQuery("BEGIN"))
16 |         .thenApply(ignored -> fun.execute(this))
17 |         .thenApply(s -> {
18 |             this.sendQuery("COMMIT");
19 |             return s;
20 |         }).exceptionally(throwable -> {
21 |             this.sendQuery("ROLLBACK");
22 |             return null;
23 |         });
24 | }
25 | 
Kotlin
suspend fun <T> inTransaction(
26 |     f: suspend (Connection) -> T): T {
27 |     try {
28 |         this.sendQuery("BEGIN")
29 |         val result = f(this)
30 |         this.sendQuery("COMMIT")
31 |         return result
32 |     } catch (e: Throwable) {
33 |         this.sendQuery("ROLLBACK")
34 |         throw e
35 |     }
36 | }
37 | 
38 | 
III
Java
final var callables = Stream.of("Service A", "Service B", "Service C")
39 |         .map(DummyService::new)
40 |         .map(service -> (Callable<ContentDuration>) service::getContent)
41 |         .collect(Collectors.toList());
42 | 
43 | final var executor = Executors.newWorkStealingPool();
44 | final var results = executor.invokeAll(callables).stream()
45 |     .map(
46 |         future -> {
47 |              try {
48 |                  return future.get();
49 |              } catch (InterruptedException | ExecutionException e) {
50 |                  throw new RuntimeException(e);
51 |              }
52 |         }
53 |     ).collect(Collectors.toList());
54 | 
Kotlin
val DummyService.asyncContent: Deferred<ContentDuration>
55 |     get() = async(CommonPool) { content }
56 | 
57 | var results = runBlocking {
58 |     arrayOf("Service A", "Service B", "Service C")
59 |         .map { DummyService(it) }
60 |         .map { it.asyncContent }
61 |         .map { it.await() }
62 | }
63 | 
64 | -------------------------------------------------------------------------------- /css/highlightjs-night-owl.css: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Night Owl for highlight.js (c) Carl Baxter 4 | 5 | An adaptation of Sarah Drasner's Night Owl VS Code Theme 6 | https://github.com/sdras/night-owl-vscode-theme 7 | 8 | Copyright (c) 2018 Sarah Drasner 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | 28 | */ 29 | 30 | .hljs { 31 | display: block; 32 | overflow-x: auto; 33 | padding: 0.5em; 34 | background: #011627; 35 | color: #d6deeb; 36 | } 37 | 38 | /* General Purpose */ 39 | .hljs-keyword { 40 | color: #c792ea; 41 | font-style: italic; 42 | } 43 | .hljs-built_in { 44 | color: #addb67; 45 | font-style: italic; 46 | } 47 | .hljs-type { 48 | color: #82aaff; 49 | } 50 | .hljs-literal { 51 | color: #ff5874; 52 | } 53 | .hljs-number { 54 | color: #F78C6C; 55 | } 56 | .hljs-regexp { 57 | color: #5ca7e4; 58 | } 59 | .hljs-string { 60 | color: #ecc48d; 61 | } 62 | .hljs-subst { 63 | color: #d3423e; 64 | } 65 | .hljs-symbol { 66 | color: #82aaff; 67 | } 68 | .hljs-class { 69 | color: #ffcb8b; 70 | } 71 | .hljs-function { 72 | color: #82AAFF; 73 | } 74 | .hljs-title { 75 | color: #DCDCAA; 76 | font-style: italic; 77 | } 78 | .hljs-params { 79 | color: #7fdbca; 80 | } 81 | 82 | /* Meta */ 83 | .hljs-comment { 84 | color: #637777; 85 | font-style: italic; 86 | } 87 | .hljs-doctag { 88 | color: #7fdbca; 89 | } 90 | .hljs-meta { 91 | color: #82aaff; 92 | } 93 | .hljs-meta-keyword { 94 | color: #82aaff; 95 | } 96 | .hljs-meta-string { 97 | color: #ecc48d; 98 | } 99 | 100 | /* Tags, attributes, config */ 101 | .hljs-section { 102 | color: #82b1ff; 103 | } 104 | .hljs-tag, 105 | .hljs-name, 106 | .hljs-builtin-name { 107 | color: #7fdbca; 108 | } 109 | .hljs-attr { 110 | color: #7fdbca; 111 | } 112 | .hljs-attribute { 113 | color: #80cbc4; 114 | } 115 | .hljs-variable { 116 | color: #addb67; 117 | } 118 | 119 | /* Markup */ 120 | .hljs-bullet { 121 | color: #d9f5dd; 122 | } 123 | .hljs-code { 124 | color: #80CBC4; 125 | } 126 | .hljs-emphasis { 127 | color: #c792ea; 128 | font-style: italic; 129 | } 130 | .hljs-strong { 131 | color: #addb67; 132 | font-weight: bold; 133 | } 134 | .hljs-formula { 135 | color: #c792ea; 136 | } 137 | .hljs-link { 138 | color: #ff869a; 139 | } 140 | .hljs-quote { 141 | color: #697098; 142 | font-style: italic; 143 | } 144 | 145 | /* CSS */ 146 | .hljs-selector-tag { 147 | color: #ff6363; 148 | } 149 | 150 | .hljs-selector-id { 151 | color: #fad430; 152 | } 153 | 154 | .hljs-selector-class { 155 | color: #addb67; 156 | font-style: italic; 157 | } 158 | 159 | .hljs-selector-attr, 160 | .hljs-selector-pseudo { 161 | color: #c792ea; 162 | font-style: italic; 163 | } 164 | 165 | /* Templates */ 166 | .hljs-template-tag { 167 | color: #c792ea; 168 | } 169 | .hljs-template-variable { 170 | color: #addb67; 171 | } 172 | 173 | /* diff */ 174 | .hljs-addition { 175 | color: #addb67ff; 176 | font-style: italic; 177 | } 178 | 179 | .hljs-deletion { 180 | color: #EF535090; 181 | font-style: italic; 182 | } 183 | -------------------------------------------------------------------------------- /css/style.css: -------------------------------------------------------------------------------- 1 | html { 2 | height: 100vh; 3 | } 4 | 5 | body { 6 | font-family: "Helvetica Neue", sans-serif; 7 | height: 100vh; 8 | background-color: #414141; 9 | } 10 | 11 | .list-group { 12 | color: #232323; 13 | } 14 | 15 | .list-group-item { 16 | color: #232323; 17 | } 18 | 19 | .section .list-group-item { 20 | background-color: #232323; 21 | } 22 | 23 | .title { 24 | font-size: 2em; 25 | color: white; 26 | font-weight: 100; 27 | letter-spacing: 0.2em; 28 | text-align: center; 29 | padding-bottom: 0.5em; 30 | } 31 | 32 | .case {} 33 | 34 | .name { 35 | font-size: 3em; 36 | color: white; 37 | font-weight: 300; 38 | text-align: center; 39 | padding-top: 0.5em; 40 | margin: 0.5em; 41 | } 42 | 43 | .lang { 44 | font-size: 1.25em; 45 | color: white; 46 | font-weight: 200; 47 | letter-spacing: 0.07em; 48 | } 49 | 50 | .code { 51 | font-size: 1em; 52 | } 53 | 54 | #fork-me { 55 | position: absolute; 56 | right: 0; 57 | } 58 | 59 | .header-title { 60 | font-size: 2em; 61 | color: #fff; 62 | background: #414141; 63 | font-weight: 300; 64 | letter-spacing: 0.05em; 65 | } 66 | 67 | .header { 68 | background-color: #414141; 69 | font-size: 1.5em; 70 | } 71 | 72 | .header a { 73 | color: white; 74 | } 75 | 76 | li a:hover { 77 | background-color: #3c3c3c; 78 | } 79 | 80 | .selected { 81 | background-color: #4e4e4e; 82 | } 83 | 84 | footer { 85 | background-color: #414141; 86 | font-size: 1em; 87 | color: white; 88 | } 89 | 90 | nav { 91 | background-color: #414141; 92 | font-size: 1em; 93 | } 94 | 95 | nav .active { 96 | background-color: #4e4e4e; 97 | } -------------------------------------------------------------------------------- /delegated-properties.html: -------------------------------------------------------------------------------- 1 | 2 | Kotlin vs Java

Kotlin vs Java

Lazy
Java
private String value;
 3 | 
 4 | public synchronized String getValue() {
 5 |     if (value == null) {
 6 |         value = "Something";
 7 |     }
 8 |     return value;
 9 | }
Kotlin
val lazyValue: String by lazy {
10 |     "Something"
11 | }
Map
Java
final ObjectMapper mapper = new ObjectMapper(); // jackson's objectmapper
12 | final MyPojo pojo = mapper.convertValue(map, MyPojo.class);
13 | 
14 | // https://stackoverflow.com/a/16430704/2441104
Kotlin
class User(val map: Map<String, Any?>) {
15 |     val name: String by map
16 |     val age: Int     by map
17 | }
Observable
Java
public class PCLNewsAgency {
18 |     private String news;
19 | 
20 |     private PropertyChangeSupport support;
21 | 
22 |     public PCLNewsAgency() {
23 |         support = new PropertyChangeSupport(this);
24 |     }
25 | 
26 |     public void addPropertyChangeListener(PropertyChangeListener pcl) {
27 |         support.addPropertyChangeListener(pcl);
28 |     }
29 | 
30 |     public void removePropertyChangeListener(PropertyChangeListener pcl) {
31 |         support.removePropertyChangeListener(pcl);
32 |     }
33 | 
34 |     public void setNews(String value) {
35 |         support.firePropertyChange("news", this.news, value);
36 |         this.news = value;
37 |     }
38 | }
Kotlin
class User {
39 |     var name: String by Delegates.observable("") {
40 |         prop, old, new ->
41 |         println("$old -> $new")
42 |     }
43 | }
44 | -------------------------------------------------------------------------------- /dsl.html: -------------------------------------------------------------------------------- 1 | 2 | Kotlin vs Java

Kotlin vs Java

I
Java
final var order = Sandwich()
  3 |     .type("toastie")
  4 |     .bread("white")
  5 |     .fillings(List.of("Cheese", "Ham"))
  6 |     .dressings(List.of("Pepper", "Worcestershire sauce"))
  7 |     .side("Crisps")
  8 |     .makeMeASandwich()
  9 | 
 10 | System.out.println(ooSandwich.receipt())
 11 | 
 12 | // https://www.kotlindays.com/2019/12/02/days-of-kotlin-a-dsl-for-everyone/index.html
Kotlin
val dslSandwich = sandwich {
 13 |     with type "toasted"
 14 |     bread = "baguette"
 15 | 
 16 |     filling("cheese")
 17 |     filling("ham")
 18 |     filling("tomato")
 19 | 
 20 |     dressings {
 21 |         +"Basil"
 22 |         +"Pepper"
 23 |     }
 24 | 
 25 |     sideOrders {
 26 |         side("French Fries")
 27 |     }
 28 | 
 29 | }
 30 | 
 31 | println(dslSandwich.receipt())
 32 | 
 33 | // https://www.kotlindays.com/2019/12/02/days-of-kotlin-a-dsl-for-everyone/index.html
II
Java
Kotlin
val result = html {
 34 |     head {
 35 |         title { +"HTML encoding with Kotlin" }
 36 |     }
 37 |     body {
 38 |         h1 { +"HTML encoding with Kotlin" }
 39 |         p {
 40 |             +"this format can be used as an"
 41 |             +"alternative markup to HTML"
 42 |         }
 43 | 
 44 |         // an element with attributes and text content
 45 |         a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
 46 | 
 47 |         // mixed content
 48 |         p {
 49 |             +"This is some"
 50 |             b { +"mixed" }
 51 |             +"text. For more see the"
 52 |             a(href = "http://jetbrains.com/kotlin") {
 53 |                 +"Kotlin"
 54 |             }
 55 |             +"project"
 56 |         }
 57 |         p {
 58 |             +"some text"
 59 |             ul {
 60 |                 for (i in 1..5)
 61 |                     li { +"${i}*2 = ${i*2}" }
 62 |             }
 63 |         }
 64 |     }
 65 | }
III
Java
final var act = this;
 66 | final var layout = new LinearLayout(act);
 67 | layout.setOrientation(LinearLayout.VERTICAL);
 68 | final var name = new EditText(act);
 69 | final var button = new Button(act);
 70 | button.setText("Say Hello");
 71 | button.setOnClickListener(
 72 |     () -> {
 73 |         Toast.makeText(act, "Hello, " + name.text + "!", Toast.LENGTH_SHORT)
 74 |            .show()
 75 | }
 76 | layout.addView(name);
 77 | layout.addView(button);
Kotlin
verticalLayout {
 78 |     val name = editText()
 79 |     button("Say Hello") {
 80 |         onClick { toast("Hello, ${name.text}!") }
 81 |     }
 82 | }
IV
Java
Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
 83 |     super.onCreate(savedInstanceState)
 84 | 
 85 |     verticalLayout {
 86 |         padding = dip(30)
 87 |         editText {
 88 |             hint = "Name"
 89 |             textSize = 24f
 90 |         }
 91 |         editText {
 92 |             hint = "Password"
 93 |             textSize = 24f
 94 |         }
 95 |         button("Login") {
 96 |             textSize = 26f
 97 |         }
 98 |     }
 99 | }
V
Java
Kotlin
application.install(Routing) {
100 |     get("/") {
101 |         call.respondText("Hello, World!")
102 |     }
103 |     get("/bye") {
104 |         call.respondText("Good bye, World!")
105 |     }
106 | }
107 | -------------------------------------------------------------------------------- /end.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /error-prone.html: -------------------------------------------------------------------------------- 1 | 2 | Kotlin vs Java

Kotlin vs Java

Equals
Java
var first = new String("abc");
 3 | var second = new String("abc");
 4 | 
 5 | var eq = first.equals(second) // true
 6 | var refEq = first == second // false
 7 | 
Kotlin
var first = String() + "abc"
 8 | var second = String() + "abc"
 9 | 
10 | var eq = first.equals(second) // true
11 | var refEq = first === second // false
12 | -------------------------------------------------------------------------------- /favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/favicon-16x16.png -------------------------------------------------------------------------------- /favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/favicon-32x32.png -------------------------------------------------------------------------------- /favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/favicon.ico -------------------------------------------------------------------------------- /functions.html: -------------------------------------------------------------------------------- 1 | 2 | Kotlin vs Java

Kotlin vs Java

Default and non-null parameters
Java
public void hello(String name) {
  3 |   if (name == null) {
  4 |     name = "World";
  5 |   }
  6 | 
  7 |   System.out.print("Hello, " + name + "!");
  8 | }
  9 | 
Kotlin
fun hello(name: String = "World") {
 10 |     println("Hello, $name!")
 11 | }
 12 | 
Single-statement functions
Java
public double cube(double x) {
 13 |   return x * x * x;
 14 | }
 15 | 
Kotlin
fun cube(x: Double) : Double = x * x * x
 16 | 
Vararg
Java
public int sum(int... numbers) { }
 17 | 
Kotlin
fun sum(vararg x: Int) { }
 18 | 
Public functions
Java
public class Main {
 19 |     public static void main(final String[] args){
 20 |         openFile("file.txt", true);
 21 |     }
 22 | }
 23 | 
 24 | class FileUtils {
 25 |     public static File openFile(String filename, boolean readOnly) { }
 26 | }
 27 | 
Kotlin
fun main() {
 28 |   openFile("file.txt", true)
 29 | }
 30 | 
 31 | fun openFile(filename: String, readOnly: Boolean) : File { }
 32 | 
Default parameters
Java
public static void main(String[]args){
 33 |   createFile("file.txt");
 34 | 
 35 |   createFile("file.txt", true);
 36 | 
 37 |   createFile("file.txt", true, false);
 38 | 
 39 |   createExecutableFile("file.txt");
 40 | }
 41 | 
 42 | public static File createFile(String filename) { }
 43 | 
 44 | public static File createFile(String filename, boolean appendDate) { }
 45 | 
 46 | public static File createFile(String filename, boolean appendDate,
 47 |                               boolean executable) { }
 48 | 
 49 | public static File createExecutableFile(String filename) { }
 50 | 
Kotlin
fun main(args: Array<String>) {
 51 |   createFile("file.txt")
 52 | 
 53 |   createFile("file.txt", true)
 54 |   createFile("file.txt", appendDate = true)
 55 | 
 56 |   createFile("file.txt", true, false)
 57 |   createFile("file.txt", appendDate = true, executable = true)
 58 | 
 59 |   createFile("file.txt", executable = true)
 60 | }
 61 | 
 62 | fun createFile(filename: String, appendDate: Boolean = false,
 63 |                executable: Boolean = false): File { }
 64 | 
Generic methods
Java
public void init() {
 65 |   var moduleInferred = createList("net");
 66 | }
 67 | 
 68 | public <T> List<T> createList(T item) { }
 69 | 
Kotlin
fun init() {
 70 |   val module = createList<String>("net")
 71 |   val moduleInferred = createList("net")
 72 | }
 73 | 
 74 | fun <T> createList(item: T): List<T> { }
 75 | 
Data classes
Java
public static void main(String[]args) {
 76 |     val book = createBook();
 77 |     System.out.println(book);
 78 |     System.out.println("Title: " + book.title);
 79 | }
 80 | 
 81 | public static Book createBook(){
 82 |     return new Book("title_01", "author_01");
 83 | }
 84 | 
 85 | @Data // Lombok
 86 | public class Book {
 87 |     private final String title;
 88 |     private final String author;
 89 | }
 90 | 
Kotlin
fun main(args: Array<String>) {
 91 |     val book = createBook();
 92 |     // or
 93 |     val (title, author) = createBook()
 94 | 
 95 |     println(book)
 96 |     println("Title: $title")
 97 | }
 98 | 
 99 | fun createBook() : Book{
100 |     return Book("title_01", "author_01")
101 | }
102 | 
103 | data class Book(val title: String, val author: String)
104 | 
105 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | Kotlin vs Java

Kotlin vs Java

Hello world
Java
public static void main(final String[] args) {
 3 |     System.out.println("Hello world!");
 4 | }
Kotlin
fun main() {
 5 |     println("Hello world!")
 6 | }
 7 | 
 8 | fun main(args: Array<String>) {
 9 |     println("Hello World!")
10 | }
Variables I
Java
var x; // compile-time error
11 | final var xx; // compile-time error
12 | final var y = 1;
13 | 
Kotlin
val x: Int // compile-time error
14 | val y // compile-time error
15 | val z = 2
16 | 
Variables II
Java
int w;
17 | int z = 2;
18 | z = 3;
19 | w = 1;
20 | 
Kotlin
var q // compile-time error
21 | var w: Int // compile-time error
22 | var z = 2
23 | 
Null I
Java
final String name = null;
24 | 
25 | String lastName;
26 | lastName = null;
27 | 
Kotlin
val name: String? = null
28 | 
29 | var lastName: String?
30 | lastName = null
31 | 
32 | var firstName: String
33 | firstName = null // Compilation error!!
34 | 
Null II
Java
final Integer length =
35 |     bob != null
36 |         && bob.department != null
37 |         && bob.department.text != null
38 |     ? bob.department.text.length()
39 |     : null;
40 | 
Kotlin
val length = bob?.department?.text?.length
41 | 
Elvis operator
Java
final var result = Optional.ofNullable(nullableVariable)
42 |     .flatMap(v -> Optional.ofNullable(v.someNullableMethodCall()))
43 |     .orElseGet(() -> fallbackIfNullMethodCall())
44 | 
Kotlin
val result = nullableVariable?.someNullableMethodCall() ?: fallbackIfNullMethodCall()
Strings I
Java
final var name = "John";
45 | final var lastName = "Smith";
46 | final var text = "My name is: " + name + " " + lastName;
47 | final var otherText = "My name is: " + name.substring(2);
48 | 
Kotlin
val name = "John"
49 | val lastName = "Smith"
50 | val text = "My name is: $name $lastName"
51 | val otherText = "My name is: ${name.substring(2)}"
52 | 
Strings II
Java
final var text = "First Line\n" +
53 |               "Second Line\n" +
54 |               "Third Line";
55 | 
Kotlin
val text = """
56 |         |First Line
57 |         |Second Line
58 |         |Third Line
59 | """.trimMargin()
60 | 
Ternary operator
Java
final var text = x > 5 ? "x > 5" : "x <= 5";
61 | 
Kotlin
val text = if (x > 5) "x > 5" else "x <= 5"
62 | 
Arrays
Java
var size = 1;
63 | 
64 | var numbers = new int[size];
65 | numbers[0] = 1;
66 | 
67 | var bigNumbers = new long[size];
68 | 
69 | var strings = new String[size];
70 | 
Kotlin
val size = 1
71 | 
72 | val numbers = IntArray(size)
73 | numbers[0] = 1
74 | 
75 | val bigNumbers = LongArray(size)
76 | 
77 | val strings = Array(size) { String() }
78 | 
Use
Java
var file = new File("contents.txt");
79 | try {
80 |     var fileReader = new FileReader(file);
81 |     try (var bufferedReader = new BufferedReader(fileReader)) {
82 |         // the bufferedReader is closed automatically
83 |     }
84 | } catch (IOException ex) {
85 |     // handle the exception
86 | }
Kotlin
File("contents.txt")
87 |     .bufferedReader()
88 |     .use {
89 |         // the bufferedReader is closed automatically
90 |     }
91 | -------------------------------------------------------------------------------- /io.html: -------------------------------------------------------------------------------- 1 | 2 | Kotlin vs Java

Kotlin vs Java

Read file contents into a string
Java
String content = null;
3 | try {
4 |     content = Files.readString(filePath);
5 | } catch (IOException e) {
6 |     e.printStackTrace();
7 | }
8 | 
Kotlin
File("doc.txt").readText()
9 | -------------------------------------------------------------------------------- /js/highlight.9.16.2.js: -------------------------------------------------------------------------------- 1 | /*! highlight.js v9.16.2 | BSD3 License | git.io/hljslicense */ 2 | !function(e){var n="object"==typeof window&&window||"object"==typeof self&&self;"undefined"==typeof exports||exports.nodeType?n&&(n.hljs=e({}),"function"==typeof define&&define.amd&&define([],function(){return n.hljs})):e(exports)}(function(a){var f=[],i=Object.keys,b={},u={},n=/^(no-?highlight|plain|text)$/i,l=/\blang(?:uage)?-([\w-]+)\b/i,t=/((^(<[^>]+>|\t|)+|(?:\n)))/gm,r={case_insensitive:"cI",lexemes:"l",contains:"c",keywords:"k",subLanguage:"sL",className:"cN",begin:"b",beginKeywords:"bK",end:"e",endsWithParent:"eW",illegal:"i",excludeBegin:"eB",excludeEnd:"eE",returnBegin:"rB",returnEnd:"rE",variants:"v",IDENT_RE:"IR",UNDERSCORE_IDENT_RE:"UIR",NUMBER_RE:"NR",C_NUMBER_RE:"CNR",BINARY_NUMBER_RE:"BNR",RE_STARTERS_RE:"RSR",BACKSLASH_ESCAPE:"BE",APOS_STRING_MODE:"ASM",QUOTE_STRING_MODE:"QSM",PHRASAL_WORDS_MODE:"PWM",C_LINE_COMMENT_MODE:"CLCM",C_BLOCK_COMMENT_MODE:"CBCM",HASH_COMMENT_MODE:"HCM",NUMBER_MODE:"NM",C_NUMBER_MODE:"CNM",BINARY_NUMBER_MODE:"BNM",CSS_NUMBER_MODE:"CSSNM",REGEXP_MODE:"RM",TITLE_MODE:"TM",UNDERSCORE_TITLE_MODE:"UTM",COMMENT:"C",beginRe:"bR",endRe:"eR",illegalRe:"iR",lexemesRe:"lR",terminators:"t",terminator_end:"tE"},_="",m={classPrefix:"hljs-",tabReplace:null,useBR:!1,languages:void 0},c="of and for in not or if then".split(" ");function C(e){return e.replace(/&/g,"&").replace(//g,">")}function E(e){return e.nodeName.toLowerCase()}function o(e){return n.test(e)}function s(e){var n,t={},r=Array.prototype.slice.call(arguments,1);for(n in e)t[n]=e[n];return r.forEach(function(e){for(n in e)t[n]=e[n]}),t}function g(e){var a=[];return function e(n,t){for(var r=n.firstChild;r;r=r.nextSibling)3===r.nodeType?t+=r.nodeValue.length:1===r.nodeType&&(a.push({event:"start",offset:t,node:r}),t=e(r,t),E(r).match(/br|hr|img|input/)||a.push({event:"stop",offset:t,node:r}));return t}(e,0),a}function d(e,n,t){var r=0,a="",i=[];function c(){return e.length&&n.length?e[0].offset!==n[0].offset?e[0].offset"}function l(e){a+=""}function o(e){("start"===e.event?u:l)(e.node)}for(;e.length||n.length;){var s=c();if(a+=C(t.substring(r,s[0].offset)),r=s[0].offset,s===e){for(i.reverse().forEach(l);o(s.splice(0,1)[0]),(s=c())===e&&s.length&&s[0].offset===r;);i.reverse().forEach(u)}else"start"===s[0].event?i.push(s[0].node):i.pop(),o(s.splice(0,1)[0])}return a+C(t.substr(r))}function R(n){return n.v&&!n.cached_variants&&(n.cached_variants=n.v.map(function(e){return s(n,{v:null},e)})),n.cached_variants?n.cached_variants:function e(n){return!!n&&(n.eW||e(n.starts))}(n)?[s(n,{starts:n.starts?s(n.starts):null})]:[n]}function v(e){if(r&&!e.langApiRestored){for(var n in e.langApiRestored=!0,r)e[n]&&(e[r[n]]=e[n]);(e.c||[]).concat(e.v||[]).forEach(v)}}function p(n,r){var a={};return"string"==typeof n?t("keyword",n):i(n).forEach(function(e){t(e,n[e])}),a;function t(t,e){r&&(e=e.toLowerCase()),e.split(" ").forEach(function(e){var n=e.split("|");a[n[0]]=[t,function(e,n){return n?Number(n):function(e){return-1!=c.indexOf(e.toLowerCase())}(e)?0:1}(n[0],n[1])]})}}function O(r){function s(e){return e&&e.source||e}function f(e,n){return new RegExp(s(e),"m"+(r.cI?"i":"")+(n?"g":""))}function a(a){var i,e,c={},u=[],l={},t=1;function n(e,n){c[t]=e,u.push([e,n]),t+=function(e){return new RegExp(e.toString()+"|").exec("").length-1}(n)+1}for(var r=0;r')+n+(t?"":_)}function l(){R+=null!=g.sL?function(){var e="string"==typeof g.sL;if(e&&!b[g.sL])return C(v);var n=e?x(g.sL,v,!0,d[g.sL]):B(v,g.sL.length?g.sL:void 0);return 0")+'"');if("end"===n.type){var r=function(e){var n=e[0],t=c(g,n);if(t){var r=g;for(r.skip?v+=n:(r.rE||r.eE||(v+=n),l(),r.eE&&(v=n));g.cN&&(R+=_),g.skip||g.sL||(p+=g.relevance),(g=g.parent)!==t.parent;);return t.starts&&(t.endSameAsBegin&&(t.starts.eR=t.eR),o(t.starts)),r.rE?0:n.length}}(n);if(null!=r)return r}return v+=t,t.length}var E=S(e);if(!E)throw new Error('Unknown language: "'+e+'"');O(E);var r,g=n||E,d={},R="";for(r=g;r!==E;r=r.parent)r.cN&&(R=u(r.cN,"",!0)+R);var v="",p=0;try{for(var M,N,h=0;g.t.lastIndex=h,M=g.t.exec(a);)N=t(a.substring(h,M.index),M),h=M.index+N;for(t(a.substr(h)),r=g;r.parent;r=r.parent)r.cN&&(R+=_);return{relevance:p,value:R,i:!1,language:e,top:g}}catch(e){if(e.message&&-1!==e.message.indexOf("Illegal"))return{i:!0,relevance:0,value:C(a)};throw e}}function B(t,e){e=e||m.languages||i(b);var r={relevance:0,value:C(t)},a=r;return e.filter(S).filter(T).forEach(function(e){var n=x(e,t,!1);n.language=e,n.relevance>a.relevance&&(a=n),n.relevance>r.relevance&&(a=r,r=n)}),a.language&&(r.second_best=a),r}function M(e){return m.tabReplace||m.useBR?e.replace(t,function(e,n){return m.useBR&&"\n"===e?"
":m.tabReplace?n.replace(/\t/g,m.tabReplace):""}):e}function N(e){var n,t,r,a,i,c=function(e){var n,t,r,a,i=e.className+" ";if(i+=e.parentNode?e.parentNode.className:"",t=l.exec(i))return S(t[1])?t[1]:"no-highlight";for(n=0,r=(i=i.split(/\s+/)).length;n/g,"\n"):n=e,i=n.textContent,r=c?x(c,i,!0):B(i),(t=g(n)).length&&((a=document.createElementNS("http://www.w3.org/1999/xhtml","div")).innerHTML=r.value,r.value=d(t,g(a),i)),r.value=M(r.value),e.innerHTML=r.value,e.className=function(e,n,t){var r=n?u[n]:t,a=[e.trim()];return e.match(/\bhljs\b/)||a.push("hljs"),-1===e.indexOf(r)&&a.push(r),a.join(" ").trim()}(e.className,c,r.language),e.result={language:r.language,re:r.relevance},r.second_best&&(e.second_best={language:r.second_best.language,re:r.second_best.relevance}))}function h(){if(!h.called){h.called=!0;var e=document.querySelectorAll("pre code");f.forEach.call(e,N)}}function S(e){return e=(e||"").toLowerCase(),b[e]||b[u[e]]}function T(e){var n=S(e);return n&&!n.disableAutodetect}return a.highlight=x,a.highlightAuto=B,a.fixMarkup=M,a.highlightBlock=N,a.configure=function(e){m=s(m,e)},a.initHighlighting=h,a.initHighlightingOnLoad=function(){addEventListener("DOMContentLoaded",h,!1),addEventListener("load",h,!1)},a.registerLanguage=function(n,e){var t=b[n]=e(a);v(t),t.rawDefinition=e.bind(null,a),t.aliases&&t.aliases.forEach(function(e){u[e]=n})},a.listLanguages=function(){return i(b)},a.getLanguage=S,a.autoDetection=T,a.inherit=s,a.IR=a.IDENT_RE="[a-zA-Z]\\w*",a.UIR=a.UNDERSCORE_IDENT_RE="[a-zA-Z_]\\w*",a.NR=a.NUMBER_RE="\\b\\d+(\\.\\d+)?",a.CNR=a.C_NUMBER_RE="(-?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)",a.BNR=a.BINARY_NUMBER_RE="\\b(0b[01]+)",a.RSR=a.RE_STARTERS_RE="!|!=|!==|%|%=|&|&&|&=|\\*|\\*=|\\+|\\+=|,|-|-=|/=|/|:|;|<<|<<=|<=|<|===|==|=|>>>=|>>=|>=|>>>|>>|>|\\?|\\[|\\{|\\(|\\^|\\^=|\\||\\|=|\\|\\||~",a.BE=a.BACKSLASH_ESCAPE={b:"\\\\[\\s\\S]",relevance:0},a.ASM=a.APOS_STRING_MODE={cN:"string",b:"'",e:"'",i:"\\n",c:[a.BE]},a.QSM=a.QUOTE_STRING_MODE={cN:"string",b:'"',e:'"',i:"\\n",c:[a.BE]},a.PWM=a.PHRASAL_WORDS_MODE={b:/\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|they|like|more)\b/},a.C=a.COMMENT=function(e,n,t){var r=a.inherit({cN:"comment",b:e,e:n,c:[]},t||{});return r.c.push(a.PWM),r.c.push({cN:"doctag",b:"(?:TODO|FIXME|NOTE|BUG|XXX):",relevance:0}),r},a.CLCM=a.C_LINE_COMMENT_MODE=a.C("//","$"),a.CBCM=a.C_BLOCK_COMMENT_MODE=a.C("/\\*","\\*/"),a.HCM=a.HASH_COMMENT_MODE=a.C("#","$"),a.NM=a.NUMBER_MODE={cN:"number",b:a.NR,relevance:0},a.CNM=a.C_NUMBER_MODE={cN:"number",b:a.CNR,relevance:0},a.BNM=a.BINARY_NUMBER_MODE={cN:"number",b:a.BNR,relevance:0},a.CSSNM=a.CSS_NUMBER_MODE={cN:"number",b:a.NR+"(%|em|ex|ch|rem|vw|vh|vmin|vmax|cm|mm|in|pt|pc|px|deg|grad|rad|turn|s|ms|Hz|kHz|dpi|dpcm|dppx)?",relevance:0},a.RM=a.REGEXP_MODE={cN:"regexp",b:/\//,e:/\/[gimuy]*/,i:/\n/,c:[a.BE,{b:/\[/,e:/\]/,relevance:0,c:[a.BE]}]},a.TM=a.TITLE_MODE={cN:"title",b:a.IR,relevance:0},a.UTM=a.UNDERSCORE_TITLE_MODE={cN:"title",b:a.UIR,relevance:0},a.METHOD_GUARD={b:"\\.\\s*"+a.UIR,relevance:0},a});hljs.registerLanguage("java",function(e){var a="false synchronized int abstract float private char boolean var static null if const for true while long strictfp finally protected import native final void enum else break transient catch instanceof byte super volatile case assert short package default double public try this switch continue throws protected public private module requires exports do",t={cN:"number",b:"\\b(0[bB]([01]+[01_]+[01]+|[01]+)|0[xX]([a-fA-F0-9]+[a-fA-F0-9_]+[a-fA-F0-9]+|[a-fA-F0-9]+)|(([\\d]+[\\d_]+[\\d]+|[\\d]+)(\\.([\\d]+[\\d_]+[\\d]+|[\\d]+))?|\\.([\\d]+[\\d_]+[\\d]+|[\\d]+))([eE][-+]?\\d+)?)[lLfF]?",relevance:0};return{aliases:["jsp"],k:a,i:/<\/|#/,c:[e.C("/\\*\\*","\\*/",{relevance:0,c:[{b:/\w+@/,relevance:0},{cN:"doctag",b:"@[A-Za-z]+"}]}),e.CLCM,e.CBCM,e.ASM,e.QSM,{cN:"class",bK:"class interface",e:/[{;=]/,eE:!0,k:"class interface",i:/[:"\[\]]/,c:[{bK:"extends implements"},e.UTM]},{bK:"new throw return else",relevance:0},{cN:"function",b:"([À-ʸa-zA-Z_$][À-ʸa-zA-Z_$0-9]*(<[À-ʸa-zA-Z_$][À-ʸa-zA-Z_$0-9]*(\\s*,\\s*[À-ʸa-zA-Z_$][À-ʸa-zA-Z_$0-9]*)*>)?\\s+)+"+e.UIR+"\\s*\\(",rB:!0,e:/[{;=]/,eE:!0,k:a,c:[{b:e.UIR+"\\s*\\(",rB:!0,relevance:0,c:[e.UTM]},{cN:"params",b:/\(/,e:/\)/,k:a,relevance:0,c:[e.ASM,e.QSM,e.CNM,e.CBCM]},e.CLCM,e.CBCM]},t,{cN:"meta",b:"@[A-Za-z]+"}]}});hljs.registerLanguage("kotlin",function(e){var t={keyword:"abstract as val var vararg get set class object open private protected public noinline crossinline dynamic final enum if else do while for when throw try catch finally import package is in fun override companion reified inline lateinit init interface annotation data sealed internal infix operator out by constructor super tailrec where const inner suspend typealias external expect actual trait volatile transient native default",built_in:"Byte Short Char Int Long Boolean Float Double Void Unit Nothing",literal:"true false null"},a={cN:"symbol",b:e.UIR+"@"},n={cN:"subst",b:"\\${",e:"}",c:[e.CNM]},c={cN:"variable",b:"\\$"+e.UIR},r={cN:"string",v:[{b:'"""',e:'"""',c:[c,n]},{b:"'",e:"'",i:/\n/,c:[e.BE]},{b:'"',e:'"',i:/\n/,c:[e.BE,c,n]}]};n.c.push(r);var i={cN:"meta",b:"@(?:file|property|field|get|set|receiver|param|setparam|delegate)\\s*:(?:\\s*"+e.UIR+")?"},l={cN:"meta",b:"@"+e.UIR,c:[{b:/\(/,e:/\)/,c:[e.inherit(r,{cN:"meta-string"})]}]},s={cN:"number",b:"\\b(0[bB]([01]+[01_]+[01]+|[01]+)|0[xX]([a-fA-F0-9]+[a-fA-F0-9_]+[a-fA-F0-9]+|[a-fA-F0-9]+)|(([\\d]+[\\d_]+[\\d]+|[\\d]+)(\\.([\\d]+[\\d_]+[\\d]+|[\\d]+))?|\\.([\\d]+[\\d_]+[\\d]+|[\\d]+))([eE][-+]?\\d+)?)[lLfF]?",relevance:0},b=e.C("/\\*","\\*/",{c:[e.CBCM]}),o={v:[{cN:"type",b:e.UIR},{b:/\(/,e:/\)/,c:[]}]},d=o;return d.v[1].c=[o],o.v[1].c=[d],{aliases:["kt"],k:t,c:[e.C("/\\*\\*","\\*/",{relevance:0,c:[{cN:"doctag",b:"@[A-Za-z]+"}]}),e.CLCM,b,{cN:"keyword",b:/\b(break|continue|return|this)\b/,starts:{c:[{cN:"symbol",b:/@\w+/}]}},a,i,l,{cN:"function",bK:"fun",e:"[(]|$",rB:!0,eE:!0,k:t,i:/fun\s+(<.*>)?[^\s\(]+(\s+[^\s\(]+)\s*=/,relevance:5,c:[{b:e.UIR+"\\s*\\(",rB:!0,relevance:0,c:[e.UTM]},{cN:"type",b://,k:"reified",relevance:0},{cN:"params",b:/\(/,e:/\)/,endsParent:!0,k:t,relevance:0,c:[{b:/:/,e:/[=,\/]/,eW:!0,c:[o,e.CLCM,b],relevance:0},e.CLCM,b,i,l,r,e.CNM]},b]},{cN:"class",bK:"class interface trait",e:/[:\{(]|$/,eE:!0,i:"extends implements",c:[{bK:"public protected internal private constructor"},e.UTM,{cN:"type",b://,eB:!0,eE:!0,relevance:0},{cN:"type",b:/[,:]\s*/,e:/[<\(,]|$/,eB:!0,rE:!0},i,l]},r,{cN:"meta",b:"^#!/usr/bin/env",e:"$",i:"\n"},s]}}); -------------------------------------------------------------------------------- /make.coffee: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env coffee 2 | project = 'from-java-to-kotlin' 3 | 4 | require 'shelljs/make' 5 | path = require 'path' 6 | mission = require 'mission' 7 | listFiles = require 'list-files' 8 | combineFiles = require 'combine-files' 9 | 10 | check = (file) -> file != './cirru/header.cirru' && file != './cirru/footer.cirru' 11 | checkGen = (file) -> file.includes('/generated') 12 | checkNotGen = (file) -> !checkGen(file) 13 | sleep = (ms) -> new Promise (resolve) -> setTimeout(resolve, ms) 14 | 15 | mission.time() 16 | 17 | cirru = (file, data) -> 18 | mission.cirruHtml 19 | files: [file] 20 | from: 'cirru/generated' 21 | to: './' 22 | extname: '.html' 23 | data: data 24 | 25 | target.dev = -> 26 | listFiles( 27 | (files) -> 28 | for file in files.filter(check).filter(checkNotGen) 29 | combineFiles(['cirru/header.cirru', file, 'cirru/footer.cirru'], 'cirru/generated/' + file.substring(8)) 30 | , 31 | { 32 | name: 'cirru', 33 | exclude: 'node_modules' 34 | } 35 | ) 36 | sleep(300).then( 37 | () -> 38 | listFiles( 39 | (files) -> 40 | for file in files.filter(check).filter(checkGen) 41 | cirru(file.substring(18),{inDev: yes, "#{file.substring(18, file.indexOf('.', 10)).replace("-", "_")}": yes}) 42 | , 43 | { 44 | name: 'cirru', 45 | exclude: 'node_modules' 46 | } 47 | ) 48 | ); 49 | 50 | 51 | target.watch = -> 52 | station = mission.reload() 53 | 54 | mission.watch 55 | files: ['cirru/', 'code/'] 56 | trigger: (filepath, extname) -> 57 | cirru inDev: yes 58 | station.reload project 59 | 60 | target.patch = -> 61 | mission.bump 62 | file: 'package.json' 63 | options: 64 | at: 'patch' 65 | -------------------------------------------------------------------------------- /mstile-150x150.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/mstile-150x150.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kotlin-vs-java", 3 | "version": "0.0.1", 4 | "description": "Kotlin vs Java ------", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "jiyinyiyong, nilhcem, fabiomsr, driver733", 10 | "license": "MIT", 11 | "dependencies": { 12 | "mission": "0.0.8-1", 13 | "shelljs": "0.8.4", 14 | "list-files": "1.1.4", 15 | "combine-files": "1.1.8" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/driver733/kotlin-vs-java.git" 20 | }, 21 | "keywords": [ 22 | "java", 23 | "kotlin" 24 | ], 25 | "bugs": { 26 | "url": "https://github.com/driver733/kotlin-vs-java/issues" 27 | }, 28 | "homepage": "https://github.com/driver733/kotlin-vs-java" 29 | } 30 | -------------------------------------------------------------------------------- /safari-pinned-tab.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 7 | 8 | Created by potrace 1.11, written by Peter Selinger 2001-2013 9 | 10 | 12 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /scoping-functions.html: -------------------------------------------------------------------------------- 1 | 2 | Kotlin vs Java

Kotlin vs Java

Also
Java
final var original = "abc"
 3 | 
 4 | System.out.println(original);
 5 | System.out.println(new StringBuilder(input).reverse().toString());
 6 | System.out.println(original.length());
Kotlin
val original = "abc"
 7 | 
 8 | original.also {
 9 |     println("The original String is $it") // "abc"
10 | }.also {
11 |     println("The reverse String is ${it.reversed()}") // "cba"
12 | }.also {
13 |     println("The length of the String is ${it.length}") // 3
14 | }
Apply
Java
public MyFragment createInstance(Bundle args) {
15 |     final var fragment = new MyFragment();
16 |     fragment.setArguments(args);
17 |     return fragment;
18 | }
19 | 
Kotlin
fun createInstance(args: Bundle) = MyFragment().apply { arguments = args }
Let
Java
final var original = "abc"
20 | // Map the value and send to the next chain
21 | 
22 | Optional.of(original)
23 |         .map(
24 |             str -> {
25 |                 System.out.println("The original String is " + str); // "abc"
26 |                 val result = new StringBuilder(input).reverse().toString();
27 |                 return result;
28 |             }
29 |         ).map(
30 |             reversed -> {
31 |                 System.out.println("The reverse String is " + reversed); // "cba"
32 |                 val result = reversed.length();
33 |                 return result;
34 |             }
35 |         ).map(
36 |             length -> {
37 |                 System.out.println("The length of the String is " + length); // 3
38 |                 return length;
39 |             }
40 |         )
41 | 
Kotlin
val original = "abc"
42 | // Map the value and send to the next chain
43 | original.let {
44 |     println("The original String is $it") // "abc"
45 |     it.reversed() // map it as parameter to send to next let
46 | }.let {
47 |     println("The reverse String is $it") // "cba"
48 |     it.length  // can be evolved to other type
49 | }.let {
50 |     println("The length of the String is $it") // 3
51 | }
Let and apply
Java
public File makeDir(String path) {
52 |     final var result = new File(path);
53 |     result.mkdirs();
54 |     return result;
55 | }
Kotlin
fun makeDir(path: String) =
56 |         path.let { File(it) }
57 |             .also { it.mkdirs() }
Run
Java
if (webview.settings != null) {
58 |     final var settings = webview.settings;
59 |     settings.javaScriptEnabled = true;
60 |     settings.databaseEnabled = true;
61 | }
Kotlin
webview.settings?.run {
62 |     javaScriptEnabled = true
63 |     databaseEnabled = true
64 | }
TakeIf
Java
public void displaySubstringPosition(final String input, final String sub) {
65 |     final var index = input.indexOf(sub);
66 |     if (index >= 0) {
67 |         System.out.println("The substring " + sub + "is found in " + input + ".");
68 |         System.out.println("Its start position is " + index + ".");
69 |     }
70 | }
Kotlin
fun displaySubstringPosition(input: String, sub: String) {
71 |     input.indexOf(sub).takeIf { it >= 0 }?.let {
72 |         println("The substring $sub is found in $input.")
73 |         println("Its start position is $it.")
74 |     }
75 | }
With
Java
val numbers = List.of("one", "two", "three");
76 | with(numbers) {
77 |     final var firstItem = numbers.get(0);
78 |     final var lastItem = numbers.get(numbers.size() - 1);
79 |     System.out.println("First item: " + firstItem + ", last item: " + lastItem);
80 | }
Kotlin
val numbers = mutableListOf("one", "two", "three")
81 | with(numbers) {
82 |     val firstItem = first()
83 |     val lastItem = last()
84 |     println("First item: $firstItem, last item: $lastItem")
85 | }
86 | -------------------------------------------------------------------------------- /site.webmanifest: -------------------------------------------------------------------------------- 1 | { 2 | "name": "", 3 | "short_name": "", 4 | "icons": [ 5 | { 6 | "src": "/android-chrome-192x192.png", 7 | "sizes": "192x192", 8 | "type": "image/png" 9 | }, 10 | { 11 | "src": "/android-chrome-512x512.png", 12 | "sizes": "512x512", 13 | "type": "image/png" 14 | } 15 | ], 16 | "theme_color": "#ffffff", 17 | "background_color": "#ffffff", 18 | "display": "standalone" 19 | } 20 | -------------------------------------------------------------------------------- /spring-framework.html: -------------------------------------------------------------------------------- 1 | 2 | Kotlin vs Java

Kotlin vs Java

Immutable Configuration properties
Java
@ImmutableConfigurationProperties(prefix = "dependency.updates")
 3 | @Validated
 4 | @RequiredArgsConstructor // Lombok
 5 | @Data // Lombok
 6 | public class AppProperties {
 7 |     private final List<Location> location;
 8 |     private final Git git;
 9 |     private final Integer limit;
10 |     @NotBlank
11 |     private final String cron;
12 |     @NotBlank
13 |     private final String priority;
14 |     @NotBlank
15 |     private final String dirPath;
16 | }
17 | 
18 | @Validated
19 | @Data // Lombok
20 | public class Location {
21 |     @NotBlank
22 |     private final String project;
23 |     @NotBlank
24 |     private final String repository;
25 | }
26 | 
27 | @Validated
28 | @Data // Lombok
29 | public class Git {
30 |     @NotBlank
31 |     private final String domain;
32 |     private final Credentials credentials;
33 | }
34 | 
35 | @Validated
36 | @Data // Lombok
37 | public class Credentials {
38 |     @NotBlank
39 |     private final String username;
40 |     @NotBlank
41 |     private final String password;
42 | }
Kotlin
@ImmutableConfigurationProperties(prefix = "dependency.updates")
43 | @Validated
44 | data class AppProperties(
45 |     val locations: List<Location>,
46 |     val git: Git,
47 |     val limit: Int,
48 |     @field:NotBlank
49 |     val cron: String,
50 |     @field:NotBlank
51 |     val priority: String,
52 |     @field:NotBlank
53 |     val dirPath: String
54 | )
55 | 
56 | @Validated
57 | data class Location(
58 |     @field:NotBlank
59 |     val project: String,
60 |     @field:NotBlank
61 |     val repository: String
62 | )
63 | 
64 | @Validated
65 | data class Git(
66 |     @field:NotBlank
67 |     val domain: String,
68 |     val credentials: Credentials
69 | )
70 | 
71 | @Validated
72 | data class Credentials(
73 |     @field:NotBlank
74 |     val username: String,
75 |     @field:NotBlank
76 |     val password: String
77 | )
78 | -------------------------------------------------------------------------------- /start-body.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /start.html: -------------------------------------------------------------------------------- 1 | 2 | --------------------------------------------------------------------------------