How do you get the type of a primitive literal in Java?

110 Views Asked by At

I just learned about using "L" after long values to signify that they're long types, just like when you use "f" after float types. I was wondering what happens when you write;

long value = 3;

instead of

long value = 3L;

So I was wondering;

Does the compiler consider 3 an integer and then just convert it implicitly to a long?

Then I tried to check the type of the value "3" by using instanceof (but I can't use it since it is a primitive type) or reflection (getClass(), but obviously I can't because it isn't a reference type). Is there anyway to check what type the value "3" is or is this impossible since Java is statically typed?

Thanks, Bernard

1

There are 1 best solutions below

4
Jörg W Mittag On

According to section 3.10.1 Integer Literals of the Java 14 Language Specification:

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).