You are here
Home > Kotlin

Range operator and NULL Safety

kotlin range operator and NULL safety

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;

Val aToz=”a”..”z”;
fun main(args:Array<String>)
//first way
var onetonine= 1..9;
var aToz=”a”..”Z”;
var is5=5 in onetonine;
println(“5 is in:”+is5);
var isr=”r” in aToz;
println(“r is in:”+isr);

var reser=onetonine.reversed();
println(“5 is in range=”+(5 in reser));

//second way
var downto=100.downTo(0);
var rangeto=10.rangeTo(20);
var is21=21 in downto;
println(“21 is in:”+is21);
var is9=9 in rangeto;




NULL Safety

  • 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.


fun main(args:Array<String>)
var name:String?;
println(“name clould have null value=”+name);
println(“name clould not have null value=”+name!!);



Type Hierarchy

  • 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 ().

Keep Visiting for more upcoming top technical articles and also see my new blog Mag4info.


You can also by SQL 70-761 book write by me from Amazon.

You can also by SQL 70-761 book write by me from Amazon.
You can also by Kotlin Book write by me from Amazon.

Leave a Reply