← Back to Kotlin Course | Chapter 14: JVM Interop & Best Practices | Lesson 1 of 7

Java Interop क्या है

Java interop का मतलब है कि Kotlin और Java code सीधे एक-दूसरे को call कर सकते हैं, क्योंकि वे दोनों आखिरकार एक ही JVM पर चलते हैं।
Syntax
kotlin
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

markup
// 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)
}

Platform Types from Java

किसी nullability annotation के बिना Java code से आने वाली value को Kotlin एक 'platform type' मानता है, जिसे आंतरिक रूप से String! दिखाया जाता है, जो आपके इसे treat करने का तरीका तय करने तक सख्त compile-time null checking टाल देता है।

Note: बिना nullability annotations वाले Java APIs से आने वाली values के चारों ओर हमेशा एक explicit null check जोड़ें।

उदाहरण: Platform Types from Java

markup
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"}")
}

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

markup
// 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")
}

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

markup
fun greet(name: String): String = "Hello, $name!"

fun main() {
    // Java code would call this as UtilsKt.greet("Kotlin")
    println(greet("Kotlin"))
}
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. यह भूल जाना कि एक plain (non-annotated) reference type return करने वाला Java method Kotlin को एक 'platform type' के रूप में दिखाई देता है, जो आपके अपने जोड़ने तक Kotlin की null-safety checks टाल देता है।
  2. यह मान लेना कि हर Java naming convention बिना बदले आगे आती है; Kotlin में Java getters/setters (property syntax) के लिए कुछ special call syntax है जो Java methods में natively नहीं है।
  3. यह न समझना कि 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:

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.