Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - NUMBERS


As we have seen in the 'Data Types' topic, there are three types of Numbers supported by Kotlin. Integers and floating point numbers.


Integers are the whole numbers (i.e. 7 or 5 or 2.14).


Any whole number(Be it '-4' or '45') can be represented by the 'Int' datatype


The Int data type is used to hold whole numbers ranging from -2147483648 to 2147483647:


  1. Int Data Type



    The 'Data Type' for a whole number is 'Int'.

    The Int data type is used to store whole numbers from -2147483648 to 2147483647

    Example :



    fun main(){
    	
        var x: Int = 5
        println(x)
    }
    


    Output :



     5

  2. Long Data Type



    Say, we have a large value '3372036854775808'.

    Well! In this case 'Int' data type will not be able to hold that value.

    And in this scenario, we need a 'Long data type'.

    A Long data type is used to store whole numbers from -9223372036854775808 to 9223372036854775808.

    It is almost same as Float data type. Just that it can hold a floating point number of a larger size.

    For example 3372036854775808.

    Example :



    fun main(){
       		
        var x: Long = 3372036854775808
        println(x) 
    }	
    


    Output :



     3372036854775808


    So, in the above example, we have created a variable 'x' of type 'Long'. And assigned a large value '3372036854775808' to it.

    Note : You can also use suffix 'L' with the value '3372036854775808', i.e. '3372036854775808L' to denote the value is of long data type. However, it is completely optional.

  3. Byte Data Type



    The 'Byte' Data Type is used to hold whole numbers between -128 to 127.

    Say, there is a small number i.e. '5'. So, to save space we can use 'Byte' Data Type.

    Example :



    fun main(){
    	
        var x: Byte = 5
        println(x)
    }	
    


    Output :



     5

  4. Short Data Type



    The 'Short' Data Type is used to hold whole numbers between -32768 to 32767.

    Say, there is a number i.e. '2768'. So, to save space we can use 'Short' Data Type.

    Example :



    fun main(){
    	
        var x: Short = 2768
        println(x)
    }	
    


    Output :



     2768


    Note : We can still use 'Int' with numbers '5' and '2768'.

  5. Float Data Type



    A float data type is used to hold floating point numbers. And is used to hold the floating point numbers of 32 bit.

    For example 5.987.

    Example :



    fun main(){
       		
        var x: Float = 5.987F
        println(x) 
    }	
    


    Output :



     5.987


    So, in the above example, we have created a variable 'x' of type 'Float'. And assigned the value '5.987' to it.

    var x: Float = 5.987F


    Just note, we have mentioned '5.987F' instead of the number '5.987'. This is an additional step you need to write to tell Kotlin that '5.987' is a Floating point number.

  6. Double Data Type



    A Double data type is used to hold floating point numbers. And is used to hold the floating point numbers of 64 bit.

    It is almost same as Float data type. Just that it can hold a floating point number of a larger size.

    For example 5.987434343434343434343.

    Example :



    fun main(){
       		
        var x: Double = 5.987434862157319
        println(x) 
    }		
    


    Output :



     5.987434862157319


    So, in the above example, we have created a variable 'x' of type 'Double'. And assigned the value '5.987434862157319' to it.

    var x: Double = 5.987434862157319