Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

367
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!