Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Which is better? Integer.valueOf(readLine()) or readLine()!!.toInt()

I am learning Kotlin using codeacademy. In the Madlibs project I was asked to read an number input using readLine() from the user and then convert it to an integer. I want to know which of the two solutions is better moving forward.

This was my solution:
distance = readLine()!!.toInt()

This was the given solution:
distance = Integer.valueOf(readLine())

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

toInt() is the better solution. It’s in the Kotlin standard library, and Integer.parseInt is in the Java library. Always prefer Kotlin-only solutions over Java solutions, where available.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading