Java Interop क्या है
In this page:
import java.util.ClassName
val javaObject = ClassName()
javaObject.method(argument)
javaObject.property = value // calls setProperty(...)
Calling Java Standard Library Classes
Kotlin किसी भी Java class को सीधे उपयोग कर सकता है, पूरी Java standard library सहित, क्योंकि Kotlin उसी JVM bytecode में compile होता है जो Java करता है।
उदाहरण: Calling Java Standard Library Classes
// Import `java.util.ArrayList`
import java.util.ArrayList
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `javaList`, set to `ArrayList<String>()`
val javaList = ArrayList<String>()
// Add `"Kotlin"` to `javaList`
javaList.add("Kotlin")
// Add `"Java"` to `javaList`
javaList.add("Java")
// Print `javaList` to the console, with a trailing newline
println(javaList)
}
Login to try C/C++/Java/PHP code in the editor
Platform Types from Java
किसी nullability annotation के बिना Java code से आने वाली value को Kotlin एक 'platform type' मानता है, जिसे आंतरिक रूप से String! दिखाया जाता है, जो आपके इसे treat करने का तरीका तय करने तक सख्त compile-time null checking टाल देता है।
उदाहरण: Platform Types from Java
fun getJavaStyleValue(): String? {
// Simulating a Java method with unknown nullability
val value: String? = if (System.currentTimeMillis() % 2 == 0L) "value" else null
return value
}
fun main() {
val result = getJavaStyleValue()
println("Result: ${result ?: "was null"}")
}
Login to try C/C++/Java/PHP code in the editor
Using Java Getters and Setters as Properties
Kotlin आपको किसी Java class के getX()/setX() methods को Kotlin से call करते समय property-style syntax का उपयोग करके call करने देता है, जैसे obj.getX() के बजाय obj.x।
उदाहरण: Using Java Getters and Setters as Properties
// Import `java.util.Calendar`
import java.util.Calendar
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `calendar`, set to `Calendar.getInstance()`
val calendar = Calendar.getInstance()
// Declare a read-only variable `year`, set to `calendar.get(Calendar.YEAR)`
val year = calendar.get(Calendar.YEAR)
// Print "Current year (via Java API): $year" to the console, with a trailing newline
println("Current year (via Java API): $year")
}
Login to try C/C++/Java/PHP code in the editor
Kotlin Functions Called from Java
एक top-level Kotlin function अपनी file के नाम पर अपने आप generated एक class पर एक static method में compile होता है (जैसे Utils.kt में functions UtilsKt पर static methods बन जाते हैं), जो वह तरीका है जिससे Java code उन्हें call करता है।
उदाहरण: Kotlin Functions Called from Java
fun greet(name: String): String = "Hello, $name!"
fun main() {
// Java code would call this as UtilsKt.greet("Kotlin")
println(greet("Kotlin"))
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि एक plain (non-annotated) reference type return करने वाला Java method Kotlin को एक 'platform type' के रूप में दिखाई देता है, जो आपके अपने जोड़ने तक Kotlin की null-safety checks टाल देता है।
- यह मान लेना कि हर Java naming convention बिना बदले आगे आती है; Kotlin में Java getters/setters (property syntax) के लिए कुछ special call syntax है जो Java methods में natively नहीं है।
- यह न समझना कि Java से call होने पर एक Kotlin top-level function एक अपने आप generated class (जैसे
FileNameKt) पर एक static method में compile होता है।
- Kotlin code सीधे Java classes और methods को call कर सकता है, और Java code सीधे Kotlin classes और functions को call कर सकता है, क्योंकि दोनों JVM bytecode में compile होते हैं।
- बिना nullability annotation के एक Java method का return type Kotlin में एक 'platform type' बन जाता है, जो compile-time null checks को bypass करता है।
- Kotlin property-style syntax (
obj.x) का उपयोग करके Java getter/setter pairs (getX()/setX()) को access कर सकता है। - Java से call होने पर Kotlin top-level functions file के नाम पर नामित एक generated class पर static methods बन जाते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: