Home > OS >  Noob Question: What is the _ in number literals in Kotlin. Like what the hell is 1_100?
Noob Question: What is the _ in number literals in Kotlin. Like what the hell is 1_100?

Time:03-06

I am just asking because I have spotted a few of these number literals in the wild. What the hell is this: '1_5_9_'? I have also spotted this in some JavaScript code. I think this is not Kotlin specific..

CodePudding user response:

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.

https://docs.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html

Looks like Kotlin just inherited this feature from Java.

  • Related