-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// program that illustrates null safety in java | ||
public class NullSafety { | ||
public static void main(String[] args) { | ||
@NotNull | ||
String name; | ||
name = null; // | ||
String name2; | ||
name2 = null; | ||
System.out.println(name2.length()); // compile error | ||
System.out.println(name2 == null ? 0 : name2.length()); | ||
} | ||
|
||
public static int getLength2(String name) { | ||
if (name == null) | ||
return 0; | ||
return name.length(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
fun main() { | ||
var name: String | ||
name = null // compile error | ||
val name2: String? | ||
name2 = null | ||
println(name2.length) // compile error | ||
print(name?.length ?: 0) | ||
} | ||
|
||
fun getLength2(name: String?): Int { | ||
if (name == null) return 0 | ||
return name.length | ||
} |