Hello developers, In today’s tutorial we will be unlocking the power of Data types in Kotlin. Learn more about Kotlin’s basic data type, an essential programming language pillar. We will practically implement each data type example in a code snapshot so you will understand how they are defined and how we can assign value to variable data types.
Complete Guide to Kotlin Data Types with Examples for Beginners:
What Are Data Types?
Data types are a fundamental concept in every programming language it defines a variable holding data type. Like what type of data a variable can store. Data means values like integer, String, Boolean, etc.
Difference Between Signed and Unsigned Integers:
While I was preparing content for this tutorial a question struck my mind which is what are Signed and Unsigned integers? Because as a beginner we all want to explore. So I am putting their difference in simplest term. “Signed Integers can store both Posite and Negate numbers starting from -2,147,483,648 to 2,147,483,647 ” & “Unsigned integers can store only Positive numbers starting from 0 to 4,294,967,295 “.
Basic Data Types in Kotlin:
1. Signed Integers in Kotlin
- Byte: 8-bit signed integer.
12val byteValue: Byte = 127 // Max value: 127val negativeByte: Byte = -128 // Min value: -128 - Short: 16-bit signed integer.
12val shortValue: Short = 32767 // Max value: 32,767val negativeShort: Short = -32768 // Min value: -32,768 - Int: 32-bit signed integer.
12val intValue: Int = 2147483647 // Max value: 2,147,483,647val negativeInt: Int = -2147483648 // Min value: -2,147,483,648 - Long: 64-bit signed integer.
12val longValue: Long = 9223372036854775807L // Max value: 9,223,372,036,854,775,807val negativeLong: Long = -9223372036854775808L // Min value: -9,223,372,036,854,775,808
2. Unsigned Integers in Kotlin:
- UByte: 8-bit unsigned integer.
1val uByteValue: UByte = 255u // Max value: 255 - UShort: 16-bit unsigned integer.
1val uShortValue: UShort = 65535u // Max value: 65,535 - UInt: 32-bit unsigned integer.
1val uIntValue: UInt = 4294967295u // Max value: 4,294,967,295 - ULong: 64-bit signed integer.
1val uLongValue: ULong = 18446744073709551615u // Max value: 18,446,744,073,709,551,615
3. Floating Point Numbers:
- Float: 32-bit floating point number.
1val floatValue: Float = 3.14f // Max value: Approximately 3.4028235E38 - Double: 64-bit floating point number.
1val doubleValue: Double = 3.141592653589793 // Max value: Approximately 1.7976931348623157E308
4. Boolean: The Boolean data type variable can store values in true and false format.
1 |
val isOpenFlag: Boolean = true |
5. Character: A char variable can store a single character like ‘A’, or ‘B’.
1 |
val charValue: Char = 'K' |
6. String: A String variable can store multiple characters.
1 |
val helloWorld: String = "Hello, Kotlin Devs!" |
I have tried my best to explain all the data types in Kotlin, Happy coding.