Java Shot #4: Writing readable long numbers in Java
If you are someone who works with Long numbers and BigIntegers, then you can make use of these two tricks to make these Long and BigIntegers more readable.
Avoid using lower case l
and use upper case L
to represent a Long value.
public class LongTest {
public static void main(String[] args) {
// 'l' resembles '1' in certain fonts,
// making this confusing
var a = 1000l;
// Using 'L' makes the number more readable
var b = 1000L;
}
}
Code showing the significance of using 'L' over 'l'
Did you know that you can use _
in numbers in Java?
public class IntTest {
public static void main (String args[]) {
// Tell me, is this a one lakh or 10 lakhs
var a = 100000;
// Now answer the same question.
var b = 1_00_000;
}
}
Code showing the significance of using '_' to enhance readability and avoid errors.