For the long data type, I can suffix a number with L to make the compiler know it is long. How about byte and short?
As motivation, the following yields a type-mismatch error:
List<Short> a = Arrays.asList(1, 2, 3, 4);
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.
What you are actually talking about is an integer literal (
1) versus a long literal (1L). There is actually no such thing as a short or byte literal in Java. But it usually doesn’t matter, because there is an implicit conversion from integer literals to the typesbyte,shortandchar. Thus:The implicit conversion is only allowed if the literal is in the required range. If it isn’t you need a type cast; e.g.
There are other cases where an explicit conversion is needed; e.g. to disambiguate method overloads, or to force a specific interpretation in an expression. In such cases you need to use a cast to do the conversion.
Your example is another of those cases.
But the bottom line is that there is no Java syntax for expressing
byteorshortliterals.