Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

300
Visualizações
What is "private lateinit var lookupKey: String" translated in Java

I am following a tutorial on Android Developer site. It uses Kotlin in the Java example. I have found this post on stack overflow about an equivalent but I do not understand the answers.

The original code is:

// Defines the selection clause
private static final String SELECTION = Data.LOOKUP_KEY + " = ?";
// Defines the array to hold the search criteria
private String[] selectionArgs = { "" };
/*
 * Defines a variable to contain the selection value. Once you
 * have the Cursor from the Contacts table, and you've selected
 * the desired row, move the row's LOOKUP_KEY value into this
 * variable.
 */
private lateinit var lookupKey: String

I have rewritten it as follows:

// Defines the selection clause
private static final String SELECTION = ContactsContract.Data.LOOKUP_KEY + " = ?";

// Defines the array to hold the search criteria
private String[] selectionArgs = { "" };
/*
 * Defines a variable to contain the selection value. Once you
 * have the Cursor from the Contacts table, and you've selected
 * the desired row, move the row's LOOKUP_KEY value into this
 * variable.
 */
private String lookupKey; 

Is this answer too simple? Or is there a more complex translation to java?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

lateinit in Kotlin is just something that plays with the nullability of a variable. Since Java does not have such property, you cannot literally convert lateinit to Java. Well, you can force its type but you won't be able to apply @NonNull/@Nullable.

Lateinit and Lazy are great topics of Kotlin to read about, and in depth. I hope you'll keep learning about those.

The answer was correct: just use private String lookupKey; and... that's it.

BTW, lateinit just creates an if condition in bytecode that will throw if null. You don't have lateinit in Java so that code would need to be make by hand. It's just another nice feature Kotlin has over Java.

over 4 years ago · Santiago Trujillo Relatório

0

The lateinit var lookupKey in Kotlin defined a property without a value set directly. The value is set to the property later. The compiler takes care to add assertions to make sure it is not possible to read the value before it is not initialized. https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties-and-variables

The lateinit plays nicely with nullability in Kotlin. So you may define a non-nullable variable without a value instead of having a nullable variable and null checks. https://kotlinlang.org/docs/reference/null-safety.html

The Java code version is not equivalent to the Kotlin version - it misses the check assertion and allows null values

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda