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

368
Visualizações
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 Respostas
Responde à pergunta

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 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