Search code examples
javaobjectcastingprimitive

Does case of Java IntegerTypeSuffix actually matter?


As claimed here, placing an L after an integer constant turns it into a Long object whereas using l would supposedly turn it into its primitive counterpart long, but Oracle claims here that "An integer literal is of type long if it is suffixed with an ASCII letter L or l".

So is the former making things up, or is the latter lying to me?

And if Oracle is lying to me, would the memory and / or performance difference of Long vs long ever actually matter or even be detectable?

In other words, does the case of a Java IntegerTypeSuffix actually matter?


Solution

  • Placing the suffix l or L on the end of a numeric literal indicates a long literal, and not a Long object.

    Section 3.10.1 of the JLS specifies the data type of a numeric literal with an l or L suffix:

    An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1).

    The type can matter when specifying literals of large quantity that are too large to fit in an int literal.

    // 10 billion is illegal; too big to fit in an int,
    // even if later assigned to a long
    long tooBigInt = 10000000000;  
    
    // 10 billion here is a legal long literal
    long justRight = 10000000000L;