Search code examples
javaandroidkotlinandroid-camerax

Android Kotlin Statement to Java Convert


I am building an App using CameraX API. I'm following android codelab example. In Codelab, The project is written on Kotlin, but in my project, I am using Java. I can't understand how do I convert these statements to java? Even I don't know, what is this line does.

// Get a stable reference of the modifiable image capture use case
val imageCapture = imageCapture ?: return

Solution

  • Koltin's null-safety: https://kotlinlang.org/docs/null-safety.html

    The ?: in Kotlin is called elvis-operator and it replaces Java's:

    if (imageCapture == null) {
        return
    }
    

    The code is checking if imageCapture is null and if so it returns and does not continue to the code below.