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

133
Views
Kotlin does not allow charAt

Why java compiles when kotlin not? And how to invoke charAt in kotlin?

Java:

import java.nio.CharBuffer;

public class Test {
    public static void test() {
        CharBuffer buffer = CharBuffer.wrap("asd");
        buffer.charAt(0);
    }
}

Kotlin:

import java.nio.CharBuffer

class TestKotlin {
    fun test() {
        val buffer = CharBuffer.wrap("asd")
        buffer.charAt(0)
    }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I am theorizing from looking at this old issue that the Kotlin developers at one time accidentally treated CharBuffer.charAt() and CharBuffer.get() as equivalent and hid the charAt() method in Kotlin and mapped it to get(). They might have done this in an effort to promote the use of array-access syntax (brackets) and avoid supposed redundancy.

And perhaps later when the above issue was fixed, they missed unhiding the method.

buffer.charAt(i) would be buffer[buffer.position() + i] in Kotlin. You could write an extension function so you can continue to use it:

fun CharBuffer.charAt(index: Int) = this[position() + index]

The error message if you choose an out-of-bounds index will be slightly less informative than the one in the original method.

Maybe someone should open an issue on YouTrack for this...

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!