{"action":"created","comment":{"url":"https://api.github.com/repos/se-edu/learningresources/pulls/comments/179933129","pull_request_review_id":110272276,"id":179933129,"diff_hunk":"@@ -0,0 +1,142 @@\n+# Null Safety in Kotlin\n+\n+Author: [Pan Haozhe](https://github.com/Haozhe321)\n+\n+>“I call it my billion-dollar mistake. It was the invention of the null reference…My goal was to ensure that all use of references should be absolutely safe, with **checking performed automatically by the compiler**. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement.”  \n+-Tony Hoare\n+\n+This document explains Kotlin's null safety feature . For an overview of Kotlin, see [here](https://github.com/se-edu/learningresources/blob/master/contents/kotlin/kotlin.md).\n+\n+# What is Null Safety?\n+Null Safety (or void safety) is the guarantee that no object reference will have a `null` value.\n+In object-oriented languages, access to objects is achieved through references. A typical function call is of the form `object.func()`; `object` denotes a reference to a certain object, and `func` denotes a function call. At execution time, the reference to `object` can be `void`, leading to run-time exceptions (In the case of Java, a NullPointerException) and often abnormal termination of the program.\n+\n+## NullPointerException\n+ When developing Android applications in Java, [NullPointerException(NPE)](https://docs.oracle.com/javase/9/docs/api/java/lang/NullPointerException.html) was a big problem. In fact, About [one third of app crashes can be attributed to NPE](https://image.slidesharecdn.com/droidcon-bugsense-130408170720-phpapp01/95/droid-con-bugsense-16-638.jpg?cb=1365440918). To see how it happens, let's take a look at the piece of Java code below:\n+\n+```java\n+String a = null;\n+if(a.length > 5) {\n+    //do something\n+}\n+```\n+When the above code is ran, a NPE will be thrown on line 2 because a `null` object has no methods. To prevent an object from taking on a `null` value, programmers typically resort to doing additional checks like this:\n+```java\n+String a = null;\n+if(a != null && a.length > 5) {\n+        //do something\n+}\n+```\n+And of course, that's all fine, until we want to do something more complex. Say Bob belongs to a department, and we want to get the name of the department manager. That will look like this:\n+```java\n+String managerName = bob.department.manager.name;\n+```\n+\n+Because each variable can be `null`, to prevent the NPE we put the code in the following code block:\n+```java\n+if(bob != null) {\n+    Department department = bob.department;\n+    if(department != null) {\n+        Employee manager = department.manager;\n+        if(manager != null) {\n+            String name = manager.name;\n+            if(name != null) {\n+                //do something\n+            }\n+        }\n+    }\n+}\n+```\n+The deep-nested `if` statement adds to the verbosity of our code.\n+\n+The other way is to use [Java Optionals](http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html).\n+For the first example above, we can do\n+``` java\n+a.ifPresent(this::doSomething);\n+```\n+\n+And for the deeply-nested `if` statements, the verbosity can be reduced with\n+``` java\n+bob.map(Person::getDepartment)\n+    .map(Person::getManager)\n+    .map(Person::getName)\n+    .ifPresent(Person::doSomething);\n+```\n+\n+Let's see how Kotlin deals with this issue while maintaining a simple and readable syntax.\n+\n+# How does Kotlin handle Null Safety?\n+## Nullable and Non-nullable type\n+In Kotlin, a type can be nullable or non-nullable, determined by the presence of a `?`. For example, an object of type `String` is non-nullable, while an object of type `String?` is nullable.  \n+\n+As the compiler catches `null` assignments to non-nullable objects, the following would result in compilation error.\n+```Kotlin\n+var firstString: String = \"hello world\"\n+firstString = null //compilation error\n+```\n+In comparison, the following assignment to a nullable type is allowed\n+```Kotlin\n+var secondString: String? = \"hello world\"\n+secondString = null //okay\n+```\n+\n+In the first case, we can safely call `firstString.length` without having to worry about a NPE because `firstString` can never be `null`.   \n+In the second case, `secondString` can potentially be `null`, so `secondString.length` will result in a compilation error as the compiler see the danger of such statement and blocks it early.\n+\n+\n+## Operators in Kotlin","path":"contents/kotlin/NullSafety.md","position":87,"original_position":87,"commit_id":"80a67e3f7814f3a42550056265d92311552753bc","original_commit_id":"80a67e3f7814f3a42550056265d92311552753bc","user":{"login":"damithc","id":1673303,"avatar_url":"https://avatars3.githubusercontent.com/u/1673303?v=4","gravatar_id":"","url":"https://api.github.com/users/damithc","html_url":"https://github.com/damithc","followers_url":"https://api.github.com/users/damithc/followers","following_url":"https://api.github.com/users/damithc/following{/other_user}","gists_url":"https://api.github.com/users/damithc/gists{/gist_id}","starred_url":"https://api.github.com/users/damithc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/damithc/subscriptions","organizations_url":"https://api.github.com/users/damithc/orgs","repos_url":"https://api.github.com/users/damithc/repos","events_url":"https://api.github.com/users/damithc/events{/privacy}","received_events_url":"https://api.github.com/users/damithc/received_events","type":"User","site_admin":false},"body":"This heading is too general. `Operators used for Null Safety` or something like that?","created_at":"2018-04-08T02:32:47Z","updated_at":"2018-04-08T02:41:34Z","html_url":"https://github.com/se-edu/learningresources/pull/45#discussion_r179933129","pull_request_url":"https://api.github.com/repos/se-edu/learningresources/pulls/45","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/se-edu/learningresources/pulls/comments/179933129"},"html":{"href":"https://github.com/se-edu/learningresources/pull/45#discussion_r179933129"},"pull_request":{"href":"https://api.github.com/repos/se-edu/learningresources/pulls/45"}}},"pull_request":{"url":"https://api.github.com/repos/se-edu/learningresources/pulls/45","id":173734279,"html_url":"https://github.com/se-edu/learningresources/pull/45","diff_url":"https://github.com/se-edu/learningresources/pull/45.diff","patch_url":"https://github.com/se-edu/learningresources/pull/45.patch","issue_url":"https://api.github.com/repos/se-edu/learningresources/issues/45","number":45,"state":"open","locked":false,"title":"Null Safety in Kotlin","user":{"login":"Haozhe321","id":26651556,"avatar_url":"https://avatars2.githubusercontent.com/u/26651556?v=4","gravatar_id":"","url":"https://api.github.com/users/Haozhe321","html_url":"https://github.com/Haozhe321","followers_url":"https://api.github.com/users/Haozhe321/followers","following_url":"https://api.github.com/users/Haozhe321/following{/other_user}","gists_url":"https://api.github.com/users/Haozhe321/gists{/gist_id}","starred_url":"https://api.github.com/users/Haozhe321/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Haozhe321/subscriptions","organizations_url":"https://api.github.com/users/Haozhe321/orgs","repos_url":"https://api.github.com/users/Haozhe321/repos","events_url":"https://api.github.com/users/Haozhe321/events{/privacy}","received_events_url":"https://api.github.com/users/Haozhe321/received_events","type":"User","site_admin":false},"body":"This pull request adds a page on null safety in Kotlin to the resources. ","created_at":"2018-03-08T13:22:51Z","updated_at":"2018-04-08T02:41:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"2de0f6e517f86f1cc45b85a8e4e23a576aba576a","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/se-edu/learningresources/pulls/45/commits","review_comments_url":"https://api.github.com/repos/se-edu/learningresources/pulls/45/comments","review_comment_url":"https://api.github.com/repos/se-edu/learningresources/pulls/comments{/number}","comments_url":"https://api.github.com/repos/se-edu/learningresources/issues/45/comments","statuses_url":"https://api.github.com/repos/se-edu/learningresources/statuses/80a67e3f7814f3a42550056265d92311552753bc","head":{"label":"Haozhe321:master","ref":"master","sha":"80a67e3f7814f3a42550056265d92311552753bc","user":{"login":"Haozhe321","id":26651556,"avatar_url":"https://avatars2.githubusercontent.com/u/26651556?v=4","gravatar_id":"","url":"https://api.github.com/users/Haozhe321","html_url":"https://github.com/Haozhe321","followers_url":"https://api.github.com/users/Haozhe321/followers","following_url":"https://api.github.com/users/Haozhe321/following{/other_user}","gists_url":"https://api.github.com/users/Haozhe321/gists{/gist_id}","starred_url":"https://api.github.com/users/Haozhe321/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Haozhe321/subscriptions","organizations_url":"https://api.github.com/users/Haozhe321/orgs","repos_url":"https://api.github.com/users/Haozhe321/repos","events_url":"https://api.github.com/users/Haozhe321/events{/privacy}","received_events_url":"https://api.github.com/users/Haozhe321/received_events","type":"User","site_admin":false},"repo":{"id":124329782,"name":"learningresources","full_name":"Haozhe321/learningresources","owner":{"login":"Haozhe321","id":26651556,"avatar_url":"https://avatars2.githubusercontent.com/u/26651556?v=4","gravatar_id":"","url":"https://api.github.com/users/Haozhe321","html_url":"https://github.com/Haozhe321","followers_url":"https://api.github.com/users/Haozhe321/followers","following_url":"https://api.github.com/users/Haozhe321/following{/other_user}","gists_url":"https://api.github.com/users/Haozhe321/gists{/gist_id}","starred_url":"https://api.github.com/users/Haozhe321/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Haozhe321/subscriptions","organizations_url":"https://api.github.com/users/Haozhe321/orgs","repos_url":"https://api.github.com/users/Haozhe321/repos","events_url":"https://api.github.com/users/Haozhe321/events{/privacy}","received_events_url":"https://api.github.com/users/Haozhe321/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Haozhe321/learningresources","description":"A collection of learning resources for Software Engineering students","fork":true,"url":"https://api.github.com/repos/Haozhe321/learningresources","forks_url":"https://api.github.com/repos/Haozhe321/learningresources/forks","keys_url":"https://api.github.com/repos/Haozhe321/learningresources/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Haozhe321/learningresources/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Haozhe321/learningresources/teams","hooks_url":"https://api.github.com/repos/Haozhe321/learningresources/hooks","issue_events_url":"https://api.github.com/repos/Haozhe321/learningresources/issues/events{/number}","events_url":"https://api.github.com/repos/Haozhe321/learningresources/events","assignees_url":"https://api.github.com/repos/Haozhe321/learningresources/assignees{/user}","branches_url":"https://api.github.com/repos/Haozhe321/learningresources/branches{/branch}","tags_url":"https://api.github.com/repos/Haozhe321/learningresources/tags","blobs_url":"https://api.github.com/repos/Haozhe321/learningresources/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Haozhe321/learningresources/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Haozhe321/learningresources/git/refs{/sha}","trees_url":"https://api.github.com/repos/Haozhe321/learningresources/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Haozhe321/learningresources/statuses/{sha}","languages_url":"https://api.github.com/repos/Haozhe321/learningresources/languages","stargazers_url":"https://api.github.com/repos/Haozhe321/learningresources/stargazers","contributors_url":"https://api.github.com/repos/Haozhe321/learningresources/contributors","subscribers_url":"https://api.github.com/repos/Haozhe321/learningresources/subscribers","subscription_url":"https://api.github.com/repos/Haozhe321/learningresources/subscription","commits_url":"https://api.github.com/repos/Haozhe321/learningresources/commits{/sha}","git_commits_url":"https://api.github.com/repos/Haozhe321/learningresources/git/commits{/sha}","comments_url":"https://api.github.com/repos/Haozhe321/learningresources/comments{/number}","issue_comment_url":"https://api.github.com/repos/Haozhe321/learningresources/issues/comments{/number}","contents_url":"https://api.github.com/repos/Haozhe321/learningresources/contents/{+path}","compare_url":"https://api.github.com/repos/Haozhe321/learningresources/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Haozhe321/learningresources/merges","archive_url":"https://api.github.com/repos/Haozhe321/learningresources/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Haozhe321/learningresources/downloads","issues_url":"https://api.github.com/repos/Haozhe321/learningresources/issues{/number}","pulls_url":"https://api.github.com/repos/Haozhe321/learningresources/pulls{/number}","milestones_url":"https://api.github.com/repos/Haozhe321/learningresources/milestones{/number}","notifications_url":"https://api.github.com/repos/Haozhe321/learningresources/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Haozhe321/learningresources/labels{/name}","releases_url":"https://api.github.com/repos/Haozhe321/learningresources/releases{/id}","deployments_url":"https://api.github.com/repos/Haozhe321/learningresources/deployments","created_at":"2018-03-08T03:14:39Z","updated_at":"2018-04-07T16:03:53Z","pushed_at":"2018-04-07T16:03:52Z","git_url":"git://github.com/Haozhe321/learningresources.git","ssh_url":"git@github.com:Haozhe321/learningresources.git","clone_url":"https://github.com/Haozhe321/learningresources.git","svn_url":"https://github.com/Haozhe321/learningresources","homepage":null,"size":1203,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"se-edu:master","ref":"master","sha":"a412fc4e03d1277b14b06c5bec9dad4bc20ec3b2","user":{"login":"se-edu","id":20763652,"avatar_url":"https://avatars2.githubusercontent.com/u/20763652?v=4","gravatar_id":"","url":"https://api.github.com/users/se-edu","html_url":"https://github.com/se-edu","followers_url":"https://api.github.com/users/se-edu/followers","following_url":"https://api.github.com/users/se-edu/following{/other_user}","gists_url":"https://api.github.com/users/se-edu/gists{/gist_id}","starred_url":"https://api.github.com/users/se-edu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/se-edu/subscriptions","organizations_url":"https://api.github.com/users/se-edu/orgs","repos_url":"https://api.github.com/users/se-edu/repos","events_url":"https://api.github.com/users/se-edu/events{/privacy}","received_events_url":"https://api.github.com/users/se-edu/received_events","type":"Organization","site_admin":false},"repo":{"id":80505681,"name":"learningresources","full_name":"se-edu/learningresources","owner":{"login":"se-edu","id":20763652,"avatar_url":"https://avatars2.githubusercontent.com/u/20763652?v=4","gravatar_id":"","url":"https://api.github.com/users/se-edu","html_url":"https://github.com/se-edu","followers_url":"https://api.github.com/users/se-edu/followers","following_url":"https://api.github.com/users/se-edu/following{/other_user}","gists_url":"https://api.github.com/users/se-edu/gists{/gist_id}","starred_url":"https://api.github.com/users/se-edu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/se-edu/subscriptions","organizations_url":"https://api.github.com/users/se-edu/orgs","repos_url":"https://api.github.com/users/se-edu/repos","events_url":"https://api.github.com/users/se-edu/events{/privacy}","received_events_url":"https://api.github.com/users/se-edu/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/se-edu/learningresources","description":"A collection of learning resources for Software Engineering students","fork":false,"url":"https://api.github.com/repos/se-edu/learningresources","forks_url":"https://api.github.com/repos/se-edu/learningresources/forks","keys_url":"https://api.github.com/repos/se-edu/learningresources/keys{/key_id}","collaborators_url":"https://api.github.com/repos/se-edu/learningresources/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/se-edu/learningresources/teams","hooks_url":"https://api.github.com/repos/se-edu/learningresources/hooks","issue_events_url":"https://api.github.com/repos/se-edu/learningresources/issues/events{/number}","events_url":"https://api.github.com/repos/se-edu/learningresources/events","assignees_url":"https://api.github.com/repos/se-edu/learningresources/assignees{/user}","branches_url":"https://api.github.com/repos/se-edu/learningresources/branches{/branch}","tags_url":"https://api.github.com/repos/se-edu/learningresources/tags","blobs_url":"https://api.github.com/repos/se-edu/learningresources/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/se-edu/learningresources/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/se-edu/learningresources/git/refs{/sha}","trees_url":"https://api.github.com/repos/se-edu/learningresources/git/trees{/sha}","statuses_url":"https://api.github.com/repos/se-edu/learningresources/statuses/{sha}","languages_url":"https://api.github.com/repos/se-edu/learningresources/languages","stargazers_url":"https://api.github.com/repos/se-edu/learningresources/stargazers","contributors_url":"https://api.github.com/repos/se-edu/learningresources/contributors","subscribers_url":"https://api.github.com/repos/se-edu/learningresources/subscribers","subscription_url":"https://api.github.com/repos/se-edu/learningresources/subscription","commits_url":"https://api.github.com/repos/se-edu/learningresources/commits{/sha}","git_commits_url":"https://api.github.com/repos/se-edu/learningresources/git/commits{/sha}","comments_url":"https://api.github.com/repos/se-edu/learningresources/comments{/number}","issue_comment_url":"https://api.github.com/repos/se-edu/learningresources/issues/comments{/number}","contents_url":"https://api.github.com/repos/se-edu/learningresources/contents/{+path}","compare_url":"https://api.github.com/repos/se-edu/learningresources/compare/{base}...{head}","merges_url":"https://api.github.com/repos/se-edu/learningresources/merges","archive_url":"https://api.github.com/repos/se-edu/learningresources/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/se-edu/learningresources/downloads","issues_url":"https://api.github.com/repos/se-edu/learningresources/issues{/number}","pulls_url":"https://api.github.com/repos/se-edu/learningresources/pulls{/number}","milestones_url":"https://api.github.com/repos/se-edu/learningresources/milestones{/number}","notifications_url":"https://api.github.com/repos/se-edu/learningresources/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/se-edu/learningresources/labels{/name}","releases_url":"https://api.github.com/repos/se-edu/learningresources/releases{/id}","deployments_url":"https://api.github.com/repos/se-edu/learningresources/deployments","created_at":"2017-01-31T09:10:16Z","updated_at":"2018-04-08T01:25:38Z","pushed_at":"2018-04-08T02:22:06Z","git_url":"git://github.com/se-edu/learningresources.git","ssh_url":"git@github.com:se-edu/learningresources.git","clone_url":"https://github.com/se-edu/learningresources.git","svn_url":"https://github.com/se-edu/learningresources","homepage":null,"size":2568,"stargazers_count":14,"watchers_count":14,"language":null,"has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":44,"mirror_url":null,"archived":false,"open_issues_count":11,"license":null,"forks":44,"open_issues":11,"watchers":14,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/se-edu/learningresources/pulls/45"},"html":{"href":"https://github.com/se-edu/learningresources/pull/45"},"issue":{"href":"https://api.github.com/repos/se-edu/learningresources/issues/45"},"comments":{"href":"https://api.github.com/repos/se-edu/learningresources/issues/45/comments"},"review_comments":{"href":"https://api.github.com/repos/se-edu/learningresources/pulls/45/comments"},"review_comment":{"href":"https://api.github.com/repos/se-edu/learningresources/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/se-edu/learningresources/pulls/45/commits"},"statuses":{"href":"https://api.github.com/repos/se-edu/learningresources/statuses/80a67e3f7814f3a42550056265d92311552753bc"}},"author_association":"NONE"}}
{"actor":{"display_login":"damithc"}}