Range operator and NULL Safety
In this post we will discuss Range operator and NULL safety in kotlin with example.
- The range operator has an interval it has start and end value.
- It is define using .. operator.
- The in operator is used to check whether a given value is there in the range or not.
- For reversing the range use reversed() function. Once we created a range and we have to modify it use step() function.
- Ex: val 1To9=1..9;
var onetonine= 1..9;
var is5=5 in onetonine;
println(“5 is in:”+is5);
var isr=”r” in aToz;
println(“r is in:”+isr);
println(“5 is in range=”+(5 in reser));
var is21=21 in downto;
println(“21 is in:”+is21);
var is9=9 in rangeto;
- In kotlin we cannot assign value of variable as NULL directly we have to use ? after its declaration if variable accept a null value.
- Ex: val name:String?=null;
- If we not use ? sign it not allow to assign null to variable.
- If we assign a null value and we have to make sure somewhere this variable not accept null value use !! sign after variable name.
println(“name clould have null value=”+name);
println(“name clould not have null value=”+name!!);
- Same as all other languages kotlin also has uppermost type called In kotlin all class by default extends the Any superclass. It has three methods tostring(), equals(), hashcode().
- In java if function not return any value we use void type and in kotlin we use Unit The difference between them is void is not any data type, but it is case that is used to indicate complier function not return any value. where Unit is a data type, with singleton instance. It is also referred to Unit or ().