Mushroom

Underscores in Java and Python numbers

In some areas in the world people use commas as thousand separators. I sometimes use points to separate them. Most of the time I do not use separators at all. In Python and Java the convention is to use underscores as separators. In this post, I investigate underscores in Java and Python numbers.

Underscores in Java numbers

The following example is the base example. Print the number one million on the console or terminal.

public class Test {
    public void ItCompiles(){
        int million = 1000000;
        System.out.println("The number is: " + million);
    }

    public static void main(String []args){
        Test test = new Test();
        test.ItCompiles();
    }
}

This compiles and gives as output:

The number is: 1000000
It works

In java it is possible to have underscores in numbers to make the number easier to read.

public void ItCompiles(){
    int million = 1_000_000;
    System.out.println("The number is: " + million);
}

This example compiles. The output is the same as the first example. The underscores are not printed out. Java allows underscores in numbers to help the programmer with reading his own code.

An underscore at the beginning or end is not possible in Java. It is also not possible to have an underscore before and after a decimal point. The following statements are all invalid.

int million = _1000000;
int million1 = 1000000_;
int million2 = 1000._0;
int million3 = 1000_.00;

I can write some strange things in Java. The following example compiles:

int million = 1_0_0_0_0_0_0;

That is not very readable. Me as a tester want to see what happens if I place more than one underscore. The following code, will it compile?

int ten = 1____________0;

Yes, it compiles and returns 10 as output. This is the strange behavior of java.

Python numbers

In python there are also underscores allowed in numbers according to PEP 515. The following example is valid python code.

thousand = 1_000

The following examples are all invalid. Like Java, an underscore at the beginning or end is not possible in Python. It is also not possible to have an underscore before and after a decimal point.

million = _1000000
million1 = 1000000_
million2 = 1000._0
million3 = 1000_.00

A number with a lot of underscores is also valid Python code.

million = 1_0_0_0_0_0_0

Will our last example also valid python code? More than one underscore?

ten = 1____________0

This code results in a SyntaxError. It is not valid in Python. That is the only difference with underscores in number that I found between Java and Python. For now at least.

About the author

I currently work as a Test Automation Consultant at b.ignited. Here I work for different clients in different industries to help them start and speed up their testing cycles

I’ve been testing software since 2000 when I became involved in testing telephone applications and hardware. Since then, I’ve been able to expand my experience by testing a variety of embedded, web, mobile and desktop applications. I have used various development methodologies from waterfall to agile.

I consider myself to be a lifelong learner.