To initialize long you need to append "L" to the end. It can be either uppercase or lowercase. All the numeric values are by default int . Even when you do any operation of byte with any integer, byte is first promoted to int and then any operations are performed.
To specify a numeric literal as a long instead of an int , add an L (for long ) to the end of the literal. Either capital or lowercase will work, but a lowercase 'l' can easily be confused with the numeral '1', especially in monospace fonts.
You can declare and initialize a Long variable by assigning it a decimal literal, a hexadecimal literal, an octal literal, or (starting with Visual Basic 2017) a binary literal. If the integer literal is outside the range of Long (that is, if it is less than Int64. MinValue or greater than Int64.
The long is a numeric data type in Java. This is also the primitive type. The long type takes 64 bits of memory. The maximum value that a long type variable can store is 9,223,372,036,854,775,807L. The minimum value is -9,223,372,036,854,775,808L.
L
: long i = 12345678910L;
.BTW: it doesn't have to be an upper case L, but lower case is confused with 1
many times :).
You need to add the L
character to the end of the number to make Java recognize it as a long.
long i = 12345678910L;
Yes.
See Primitive Data Types which says "An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int."
You need to add uppercase L
at the end like so
long i = 12345678910L;
Same goes true for float with 3.0f
Which should answer both of your questions
To initialize long you need to append "L" to the end.
It can be either uppercase or lowercase.
All the numeric values are by default int
. Even when you do any operation of byte
with any integer, byte
is first promoted to int
and then any operations are performed.
Try this
byte a = 1; // declare a byte
a = a*2; // you will get error here
You get error because 2
is by default int
.
Hence you are trying to multiply byte
with int
.
Hence result gets typecasted to int
which can't be assigned back to byte
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With