Kotlin Training Program

DOWNLOAD APP

FEEDBACK

Data Types

Programming is all about working with data. Data can be of various types : numerical, textual, boolean (true/false) or custom data type. In Kotlin, we have the following data types :

Numerical

 fun main() {
	val byte: Byte = 1      // Size = 1B, Range = -128..127
	val short: Short = 1000 // Size = 2B, Range = -32k..32k
		
	val int = 5             // Size = 4B, Range = -2e9..2e9
	val long = 5L           // Size = 8B, Range = -9e18..9e18
		
	val float = 3.14f       // Size = 4B, upto 7 decimal digits
	val double = 3.14       // Size = 8B, upto 16 decimal deigits
}
 

Note that L is used to define Long, f is used to define Float and decimal point number without f is inferred as Double.

Textual

 fun main() {
		val char = '#' // Size = 2B
		val string = "INDIA" // Size = 5 * 2B = 10B
}
 

Note that Char is enclosed in single quotes ‘’ while String is enclosed in double quotes “”.

Boolean

Boolean is used to represent a 0/1 or true/false value :

 fun main() {
	val bool = true
	val bool2 = false
}
 

Collections

Collections are used to hold multiple values of similar type. Examples include :

We will learn more about Collections in Collections chapter.

User defined

Kotlin supports Object Oriented Programming. So using Classes, we can define our own data types. We shall learn more about OOPs in the OOPs chapter.

Unit

Unit is a special data type in Kotlin which holds no data.

Why do we need a data type that holds no data?

It is needed as a placeholder.

The most common usecase for Unit is in function return types. When a function returns no output, it actually returns Unit. All functions which have no return type defined, default to returning Unit.

Examples -