बेसिक Types
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
// 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")
}
Login to try C/C++/Java/PHP code in the editor
Floating-Point Types
Double उच्च precision वाले decimal numbers के लिए default type है, जबकि Float कम memory लेकिन कम precision उपयोग करता है और इसके literals पर f suffix चाहिए।
उदाहरण: Floating-Point Types
// 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")
}
Login to try C/C++/Java/PHP code in the editor
Boolean and Char
Boolean सिर्फ true या false हो सकता है, जो आमतौर पर conditions में उपयोग होता है। Char single quotes में wrapped बिल्कुल एक character रखता है, जो double quotes उपयोग करने वाले String से अलग है।
उदाहरण: Boolean and Char
// 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")
}
Login to try C/C++/Java/PHP code in the editor
Integer Division Truncates
दो Int values को / से divide करना integer division करता है, round करने के बजाय किसी भी remainder को discard करते हुए। एक fractional result पाने के लिए, कम से कम एक operand को Double या Float होना चाहिए।
% operator का उपयोग करें।उदाहरण: Integer Division Truncates
// 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()}")
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
IntऔरLonginterchangeable हैं; बड़ी values कोLongचाहिए और एक literal कोLsuffix चाहिए। Char(single character, single quotes) कोString(text, double quotes) के साथ confuse करना।- यह भूल जाना कि दो
Intvalues को divide करना integer division करता है, किसी भी decimal हिस्से को truncate करते हुए।
- Kotlin के core numeric types में
Int,Long,Double, औरFloatशामिल हैं, हर एक का अलग size और precision है। Booleantrueयाfalseरखता है, औरCharsingle quotes में एक character रखता है।Stringdouble quotes में text रखता है औरCharसे एक अलग type है।- दो
Intvalues के बीच integer division decimal हिस्से को truncate करता है; fractional result पाने के लिएDoubleoperands का उपयोग करें।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: