← Back to Kotlin Course | Chapter 2: Variables & Types | Lesson 3 of 7

बेसिक Types

Basic types Kotlin की समझी जाने वाली सरल values की अलग-अलग kinds हैं, जैसे whole numbers, decimals, true/false, और single letters।
Syntax
kotlin
val name: Int = 42
val name: Long = 10000000000L
val name: Double = 3.14
val name: Float = 1.5f
val name: Boolean = true
val name: Char = 'c'

Integer Types

Int whole numbers के लिए default type है और लगभग 2.1 billion तक की values रखता है। Long कहीं बड़े whole numbers रखता है लेकिन literals को L suffix के साथ खत्म होना चाहिए, जैसे 10000000000L।

उदाहरण: Integer Types

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `small` (an Int), set to `42`
    val small: Int = 42
    // Declare a read-only variable `big` (a Long), set to `10000000000L`
    val big: Long = 10000000000L
    // Print "Int: $small, Long: $big" to the console, with a trailing newline
    println("Int: $small, Long: $big")
}

Floating-Point Types

Double उच्च precision वाले decimal numbers के लिए default type है, जबकि Float कम memory लेकिन कम precision उपयोग करता है और इसके literals पर f suffix चाहिए।

उदाहरण: Floating-Point Types

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `pi` (a Double), set to `3.14159`
    val pi: Double = 3.14159
    // Declare a read-only variable `ratio` (a Float), set to `1.5f`
    val ratio: Float = 1.5f
    // Print "Double: $pi, Float: $ratio" to the console, with a trailing newline
    println("Double: $pi, Float: $ratio")
}

Boolean and Char

Boolean सिर्फ true या false हो सकता है, जो आमतौर पर conditions में उपयोग होता है। Char single quotes में wrapped बिल्कुल एक character रखता है, जो double quotes उपयोग करने वाले String से अलग है।

उदाहरण: Boolean and Char

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `isKotlinFun` (a `Boolean`), set to `true`
    val isKotlinFun: Boolean = true
    // Declare a read-only variable `grade` (a `Char`), set to `'A'`
    val grade: Char = 'A'
    // Print "Fun: $isKotlinFun, Grade: $grade" to the console, with a trailing newline
    println("Fun: $isKotlinFun, Grade: $grade")
}

Integer Division Truncates

दो Int values को / से divide करना integer division करता है, round करने के बजाय किसी भी remainder को discard करते हुए। एक fractional result पाने के लिए, कम से कम एक operand को Double या Float होना चाहिए।

Note: किसी integer division का remainder पाने के लिए % operator का उपयोग करें।

उदाहरण: Integer Division Truncates

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `a`, set to `7`
    val a = 7
    // Declare a read-only variable `b`, set to `2`
    val b = 2
    // Print "Integer division: ${a / b}" to the console, with a trailing newline
    println("Integer division: ${a / b}")
    // Print "Double division: ${a / b.toDouble()}" to the console, with a trailing newline
    println("Double division: ${a / b.toDouble()}")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि Int और Long interchangeable हैं; बड़ी values को Long चाहिए और एक literal को L suffix चाहिए।
  2. Char (single character, single quotes) को String (text, double quotes) के साथ confuse करना।
  3. यह भूल जाना कि दो Int values को divide करना integer division करता है, किसी भी decimal हिस्से को truncate करते हुए।
चैप्टर सारांश
  • Kotlin के core numeric types में Int, Long, Double, और Float शामिल हैं, हर एक का अलग size और precision है।
  • Boolean true या false रखता है, और Char single quotes में एक character रखता है।
  • String double quotes में text रखता है और Char से एक अलग type है।
  • दो Int values के बीच integer division decimal हिस्से को truncate करता है; fractional result पाने के लिए Double operands का उपयोग करें।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.