I want to declare a big constant. But java shows an error, that my constant is too big for int. But I want a long constant. What to do?
public static final long MAXMONEY = 1000000000000000000;
I want to declare a big constant. But java shows an error, that my constant is too big for int. But I want a long constant. What to do?
public static final long MAXMONEY = 1000000000000000000;
Put an L
on the end of it.
public static final long MAXMONEY = 1000000000000000000L;
From JLS section 3.10.1:
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.
And yes, this value is within range for a long
.
There is a nice little summary of syntax for constant values here.
In order to write a long literal you need to add an L
to the end of the number. Try
public static final long MAXMONEY = 1000000000000000000L;
I did not test, if this number is small enough for a long.
By default all non floating point literals are treated as integers and max value of integer is 2147483647
, so 1000000000000000000
is incorrect here (it is out of integers range).
If you want to create long
literal you need to specify it by adding l
or L
at the end like
1000000000000000000l
1000000000000000000L
Preferred way is to add L
because l
looks like 1
and cause confusion.
But if you really are going to operate on big numbers consider using BinInteger
or BigDecimal
classes to avoid integer overflow. You can instantiate them with
new BigInteger("1000000000000000000");