Type Checking और Casting
if (value is Type) {
// value is smart-cast to Type here
}
val casted = value as Type // ClassCastException if wrong
val safe = value as? Type // null if wrong
Checking Types with is
is operator runtime पर यह जांचता है कि कोई value किसी दिए गए type का instance है या नहीं, true या false return करते हुए, Java में instanceof जैसा।
उदाहरण: Checking Types with is
// Define the function `describe` taking `value`
fun describe(value: Any) {
// Check whether `value is String`
if (value is String) {
// Print "It is a String of length ${value.length}" to the console, with a trailing newline
println("It is a String of length ${value.length}")
// Otherwise, run this branch
} else {
// Print "Not a String" to the console, with a trailing newline
println("Not a String")
}
}
// Entry point: execution of the program starts here
fun main() {
describe("Kotlin")
describe(42)
}
Login to try C/C++/Java/PHP code in the editor
Smart Casts
एक बार जब कोई is check किसी value का type पुष्टि कर दे, Kotlin अपने आप उस scope के बाकी हिस्से के लिए इसे उस type के रूप में treat करता है -- किसी manual cast की ज़रूरत नहीं, जिसे smart cast कहा जाता है।
उदाहरण: Smart Casts
fun printLength(value: Any) {
if (value is String) {
// value is smart-cast to String here
println("Length: ${value.length}")
}
}
fun main() {
printLength("Hello")
}
Login to try C/C++/Java/PHP code in the editor
Unsafe Cast with as
as operator किसी value को ज़बरदस्ती किसी दिए गए type में cast करता है, अगर value असल में उस type के साथ compatible न हो तो runtime पर एक ClassCastException फेंकते हुए।
as का उपयोग सिर्फ तभी करें जब आपको type के बारे में निश्चितता हो; अन्यथा as? को प्राथमिकता दें।उदाहरण: Unsafe Cast with as
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `value` (a `Any`), set to `"Kotlin"`
val value: Any = "Kotlin"
// Declare a read-only variable `text`, set to `value as String`
val text = value as String
// Print "Cast succeeded: $text" to the console, with a trailing newline
println("Cast succeeded: $text")
}
Login to try C/C++/Java/PHP code in the editor
Safe Cast with as?
as? operator वही cast करने की कोशिश करता है लेकिन fail होने पर throw करने के बजाय null return करता है, जो type के अनिश्चित होने पर कहीं ज़्यादा सुरक्षित है।
उदाहरण: Safe Cast with as?
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `value` (a `Any`), set to `42`
val value: Any = 42
// Declare a read-only variable `text`, set to `value as? String`
val text = value as? String
// Print "Safe cast result: $text" to the console, with a trailing newline
println("Safe cast result: $text")
}
Login to try C/C++/Java/PHP code in the editor
- किसी value पर unsafe cast
asउपयोग करना जो असल में वह type नहीं हो सकती, runtime पर एकClassCastExceptionका कारण बनते हुए। - किसी
ischeck के बाद smart casting उपयोग न करना, और फिर भी manually cast करना, ज़रूरत से ज़्यादा verbose code पैदा करते हुए। - यह भूल जाना कि
as?throw करने के बजायnullreturn करता है, और उसnullresult को handle न करना।
isruntime पर यह जांचता है कि कोई value एक खास type है या नहीं, एकBooleanreturn करते हुए।- Kotlin के smart casts किसी
if (x is Type)block के अंदर बिना किसी manual cast के किसी value को अपने आप checked type के रूप में treat करते हैं। asएक unsafe cast करता है जो value असल में वह type न होने परClassCastExceptionफेंकता है।as?एक safe cast करता है जो cast संभव न होने पर throw करने के बजायnullreturn करता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: