Are you required to define a Long variable as
Long myUserId = 1L; ?
How come you can’t just do Long myUserId = 1; ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
does not work, because
1is an int.It will get auto-boxed to:
It will also get widened to:
but not both.
So, yes, you have to say
which is a
longthat can get autoboxed into aLong.As to why it works that way (or rather does not work in this case): Most likely because auto-boxing was added later (in Java5), and had to be absolutely backwards-compatible. That limited how “smooth” they could make it.