Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
- Dart Programming
- Flutter Tutorial
- Numerical Methods Tutorials
- Flutter Tutorials
- Kotlin Tutorial
Practical Paper
Industrial Training
Nullable Non Nullable Types
Kotlin null safety is a procedure to eliminate the risk of null reference from the code. Kotlin compiler throws NullPointerException immediately if it found any null argument is passed without executing any other statements.
Kotlin's type system is aimed to eliminate NullPointerException form the code. NullPointerException can only possible on following causes:
- An forcefully call to throw NullPointerException();
- An uninitialized of this operator which is available in a constructor passed and used somewhere.
- Use of external java code as Kotlin is Java interoperability.
Kotlin Nullable Types and Non-Nullable Types
Kotlin types system differentiates between references which can hold null (nullable reference) and which cannot hold null (non null reference). Normally,types of String are not nullable. To make string which holds null value, we have to explicitly define them by putting a ? behind the String as: String?
Nullable Types
Nullable types are declared by putting a ? behind the String as:
var str1: String? = "hello" str1 = null // ok var str1: String? = "hello" str1 = null // ok
Kotlin example of nullable types
fun main(args: Array< String>){ var str: String? = "Hello" // variable is declared as nullable str = null print(str) } fun main(args: Array< String>){ var str: String? = "Hello" // variable is declared as nullable str = null print(str) }
Output:
null
Non Nullable Types
Non nullable types are normal strings which are declared as String types as:
val str: String = null // compile error str = "hello" // compile error Val cannot be reassign var str2: String = "hello" str2 = null // compile error val str: String = null // compile error str = "hello" // compile error Val cannot be reassign var str2: String = "hello" str2 = null // compile error
What happens when we assign null value to non nullable string?
fun main(args: Array< String>){ var str: String = "Hello" str = null // compile error print(str) } fun main(args: Array< String>){ var str: String = "Hello" str = null // compile error print(str) }
Output:
It will generate a compile time error.
Error:(3, 11) Kotlin: Null can not be a value of a non-null type String
Checking for null in conditions
Kotlin's If expression is used for checking condition and returns value.
fun main(args: Array< String>){ var str: String? = "Hello" // variable is declared as nullable var len = if(str!=null) str.length else -1 println("str is : $str") println("str length is : $len") str = null println("str is : $str") len = if(str!=null) str.length else -1 println("b length is : $len") } fun main(args: Array< String>){ var str: String? = "Hello" // variable is declared as nullable var len = if(str!=null) str.length else -1 println("str is : $str") println("str length is : $len") str = null println("str is : $str") len = if(str!=null) str.length else -1 println("b length is : $len") }
Output:
str is : Hello str length is : 5 str is : null b length is : -1