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

366
Vistas
Why does println() throw an error in one case but not in another?

Why is it that

println(Int.MIN_VALUE + " " + Int.MAX_VALUE)

throws an error, while

println("" + Int.MIN_VALUE + " " + Int.MAX_VALUE)

does not?

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

0

As mentioned in the comments, your two statements call different plus functions. The plus function is a binary operator function, which means that it can be called using the infix notation a + b. This will call the operator function on a, with b as the argument. In your example, this means that

"" + Int.MIN_VALUE

calls the plus defined by String which has String.plus(other: Any?) as its method signature. As mentioned by the documentation, it

Returns a string obtained by concatenating this string with the string representation of the given other object.

This means that it will get the String representation of Int.MIN_VALUE, by calling the toString() method.

On the other hand,

Int.MIN_VALUE + ""

calls the plus defined by Int which limits the type of its argument and cannot be applied to a String, and thus Kotlin will throw an error.


As a side remark, you should probably use string templates anyway:

"${Int.MIN_VALUE} ${Int.MAX_VALUE}"
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