Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

307
Vistas
Is there a way to hide Java methods in Kotlin?

I'm learning Kotlin and I'm reading this days Kotlin in Action. Reading the chapter about extension functions and standard library improvement, I read about different behaviour exposed by String.split overloads between Java and Kotlin: I think it's a very good idea to move toward an explicit and more type-guided separation between delimiters-based and regex-based overloads.

About this, the book I've mentioned above says that Kotlin hides the confusing method and provides as replacements several overloaded extensions named split that have different arguments.

Extension functions respond to the questione "How can I add method to an existing class"; on the other hand, I can't figure out how it's possibile to hide methods provided by the Java Standard Library.

I did some attempts writing simple code, and I noticed that

  1. val ks = "Pietro Martinelli" has ks::class.qualifiedName == "kotlin.String"
  2. val ks = "Pietro Martinelli" has ks::javaClass.name == "java.lang.String"
  3. passing the above value ks to a Java method, the received parameter x has x.getClass().getName() == "java.lang.String" (as expected)
  4. String js = "Pietro Martinelli" (in Java code) has js.getClass().getName() == "java.lang.String", as expected
  5. passing the above js value to a Kotlin method, the received parameter y has y::class.qualifiedName == "kotlin.String" and y.getClass().getName() == "java.lang.String" (as so far expected)

So: it seems that (let me say) something magic occurred when I use String values in Kotlin and back and forth from Kotlin to Java. If I undestand correctly, String literals in Kotlin are instances of kotlin.String but they are bounded to a Java type that is transparently used for Java method calls. This way Kotlin library can enhance String experience without affecting java.lang.String: usual Java [java.lang.]String.split methods are hidden to Kotlin code in the sense that Kotlin developers see [kotlin.]String instances and their methods.

So, two (related) question: 1. is my understanding right? And, more interesting 2. it's only a kind of magic put in place by the compiler, which knows Strings as instances of a special type and wraps them behind instances of a different class, or is there some more general approach/mechanic that permits to someway hide some method of a class the same way we can add methods through the extension function mechanic? It can be very dangerous, so I think it's only a matter of the compiler, but it can be powerful, too, so I think it's worth the question.

Thank in advance for hints and feedbacks.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Unfortunately, this is compiler magic. These are called mapped types, and the compiler gives them special treatment to make it so that they are visible to Kotlin code as the Kotlin types that have modified interfaces, even though at runtime they are instances of the regular Java types when compiled to the JVM.

Other than String, the most notable examples perhaps are the collections, which have two layers of completely new interfaces applied to them, for the read-only and mutable variants.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda